1

I have a content type called ip_range and i was trying to create a node programmatically . I tried the follows :

use \Drupal\node\Entity\Node;
use \Drupal\file\Entity\File;
    $data = array(
      'type' => 'ip_range', 
      'title' => '192.168.7.100/24', 
      'field_ip_range_gw' => '192.168.7.100', 
      'field_ip_range_hidden' => '', 
      'field_ip_range_blocked' => '192.168.7.200', 
      'field_ip_range_access_type' => 'Blocked', 
      'field_ip_threshold' => '20', 
      'field_ip_range_sec_zone' => 'C2', 
      'field_ip_range_vlan_name' => 'Network58', 
      'field_ip_range_vlan_tag' => '4054', 
      'field_ip_range_comment' => 'Ip ranges from 192.168.7.100 to 192.168.7.255', 
    );
    $node = Drupal::entityManager()
      ->getStorage('node')
      ->create($data);

    $node->save();

But its not creating any . Any idea ?

UPDATE


Its creating the node . But its creating as anonymous user . So i cant see it in front end . That was the problem . How to create it as a admin user ?

4
  • Where did you put this code? In a .module, a .theme?
    – LamaDelRay
    Jan 17, 2017 at 10:15
  • this code in drupal root folder and executing this script using drush
    – KTM
    Jan 17, 2017 at 10:22
  • use \Drupal\node\Entity\Node; $node = Node::create([ 'type' => 'ip_range', . . 'field_ip_range_comment' => 'Ip ranges from 192.168.7.100 to 192.168.7.255', ]); $node->save(); Jan 17, 2017 at 10:24
  • Have you tried creating a node afterwards to see if the id has been incremented?
    – LamaDelRay
    Jan 17, 2017 at 10:24

1 Answer 1

3

Try this Code in MigrateContentController:

<?php

namespace Drupal\migrate_contents\Controller;
use Drupal\taxonomy\Entity\Term;
use Drupal\Core\Controller\ControllerBase;
use Drupal\node\Entity\Node;

class MigrateContentController extends ControllerBase {

 function migrateContent() {
 $updated = 0;
 $data = array(
   'type' => 'ip_range', 
   'title' => '192.168.7.100/24', 
   'field_ip_range_gw' => '192.168.7.100', 
   'field_ip_range_hidden' => '', 
   'field_ip_range_blocked' => '192.168.7.200', 
   'field_ip_range_access_type' => 'Blocked', 
   'field_ip_threshold' => '20', 
   'field_ip_range_sec_zone' => 'C2', 
   'field_ip_range_vlan_name' => 'Network58', 
   'field_ip_range_vlan_tag' => '4054', 
   'field_ip_range_comment' => 'Ip ranges from 192.168.7.100 to 192.168.7.255', 
 );
 $node = Node::create($data);
 $node->save();
 $updated++;

 return array(
   '#markup' => $updated,
 );
 }
}

Inside your_module.routing.yml

migrate_contents.migrate_node:
  path: '/migrate-contents'
    defaults:
    _controller: 'Drupal\migrate_contents\Controller\MigrateContentController::migrateContent'
    _title: 'Migrate Contents'
  options:
    _admin_route: TRUE

Note: Clear the cache before proceeding.

4
  • Not working though ! , and the script has no errors :(
    – KTM
    Jan 17, 2017 at 10:50
  • 1
    can you update the question as what you have tried? Jan 17, 2017 at 10:56
  • Thanks ,man , actually its creating. But its not listing because am creating the node as anonymous user . DO you know how create it as a admin user ?
    – KTM
    Jan 17, 2017 at 11:06
  • okay , i got it working , don't know why its worked because used Node::create($data); instead of Drupal::entityManager() ->getStorage('node') ->create($data); , although am accepting your answer :)
    – KTM
    Jan 20, 2017 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.