0

When I try to print out the possible options of Node::load(10)

I just get null. But I know that there are options as Node::load(10)->field_name->value gives the value of the field.

I have tried using dpm as well as json_encode.

examples:

dpm(Node::load(10)->getFieldDefinitions());
dpm(Node::load(10)->toArray());
json_encode(Node::load(10))
1

1 Answer 1

1

I tried the following command, and it returns the expected output.

drush php-eval 'var_export(\Drupal\node\Entity\Node::load(1));'

The only case where I get NULL as output is when I pass to Node::load() a node ID that doesn't exist.

The fact Node::load() loads the data from the database doesn't matter; in fact, the method still returns an object of the Node class.

Drupal\node\Entity\Node::__set_state(array(
   'in_preview' => NULL,
   'values' => 
  array (
    'vid' => 
    array (
      'x-default' => '1',
    ),
    'langcode' => 
    array (
      'x-default' => 'en',
    ),
    'revision_timestamp' => 
    array (
      'x-default' => '1465363710',
    ),
    'revision_uid' => 
    array (
      'x-default' => '1',
    ),
    'revision_log' => 
    array (
      'x-default' => NULL,
    ),
    'nid' => 
    array (
      'x-default' => '1',
    ),
    'type' => 
    array (
      'x-default' => 'page',
    ),
    'uuid' => 
    array (
      'x-default' => '88E2D589-473B-4FE9-9A5A-F1F768A5E4E8',
    ),
    'isDefaultRevision' => 
    array (
      'x-default' => '1',
    ),
    'title' => 
    array (
      'x-default' => 'Test',
    ),
    'uid' => 
    array (
      'x-default' => '1',
    ),
    'status' => 
    array (
      'x-default' => '1',
    ),
    'created' => 
    array (
      'x-default' => '1465363684',
    ),
    'changed' => 
    array (
      'x-default' => '1465363710',
    ),
    'promote' => 
    array (
      'x-default' => '0',
    ),
    'sticky' => 
    array (
      'x-default' => '0',
    ),
    'revision_translation_affected' => 
    array (
      'x-default' => '1',
    ),
    'default_langcode' => 
    array (
      'x-default' => '1',
    ),
    'body' => 
    array (
      'x-default' => 
      array (
        0 => 
        array (
          'value' => '<p>Test.</p>
',
          'summary' => '',
          'format' => 'basic_html',
        ),
      ),
    ),
  ),
   'fields' => 
  array (
  ),
   'fieldDefinitions' => NULL,
   'languages' => NULL,
   'langcodeKey' => 'langcode',
   'defaultLangcodeKey' => 'default_langcode',
   'activeLangcode' => 'x-default',
   'defaultLangcode' => 'en',
   'translations' => 
  array (
    'x-default' => 
    array (
      'status' => 1,
    ),
  ),
   'translationInitialize' => false,
   'newRevision' => false,
   'isDefaultRevision' => '1',
   'entityKeys' => 
  array (
    'bundle' => 'page',
    'id' => '1',
    'revision' => '1',
    'uuid' => '88E2D589-473B-4FE9-9A5A-F1F768A5E4E8',
  ),
   'translatableEntityKeys' => 
  array (
    'label' => 
    array (
      'x-default' => 'Test',
    ),
    'langcode' => 
    array (
      'x-default' => 'en',
    ),
    'status' => 
    array (
      'x-default' => '1',
    ),
    'uid' => 
    array (
      'x-default' => '1',
    ),
    'default_langcode' => 
    array (
      'x-default' => '1',
    ),
  ),
   'validated' => false,
   'validationRequired' => false,
   'entityTypeId' => 'node',
   'enforceIsNew' => NULL,
   'typedData' => NULL,
   'cacheContexts' => 
  array (
  ),
   'cacheTags' => 
  array (
  ),
   'cacheMaxAge' => -1,
   '_serviceIds' => 
  array (
  ),
))

Even running drush php-eval 'var_export(\Drupal\node\Entity\Node::load(1)->toArray());' (i.e. using the toArray() method you tried) gave me an output different from NULL. In this case, using a node ID that doesn't exist, you get a Call to a member function toArray() on a non-object error.

array (
  'nid' => 
  array (
    0 => 
    array (
      'value' => '1',
    ),
  ),
  'uuid' => 
  array (
    0 => 
    array (
      'value' => '994535DB-7C4E-42F2-94CE-576DA3E9FC0B',
    ),
  ),
  'vid' => 
  array (
    0 => 
    array (
      'value' => '1',
    ),
  ),
  'langcode' => 
  array (
    0 => 
    array (
      'value' => 'en',
    ),
  ),
  'type' => 
  array (
    0 => 
    array (
      'target_id' => 'page',
    ),
  ),
  'title' => 
  array (
    0 => 
    array (
      'value' => 'Test',
    ),
  ),
  'uid' => 
  array (
    0 => 
    array (
      'target_id' => '1',
    ),
  ),
  'status' => 
  array (
    0 => 
    array (
      'value' => '1',
    ),
  ),
  'created' => 
  array (
    0 => 
    array (
      'value' => '1466285938',
    ),
  ),
  'changed' => 
  array (
    0 => 
    array (
      'value' => '1466285973',
    ),
  ),
  'promote' => 
  array (
    0 => 
    array (
      'value' => '0',
    ),
  ),
  'sticky' => 
  array (
    0 => 
    array (
      'value' => '0',
    ),
  ),
  'revision_timestamp' => 
  array (
    0 => 
    array (
      'value' => '1466285973',
    ),
  ),
  'revision_uid' => 
  array (
    0 => 
    array (
      'target_id' => '1',
    ),
  ),
  'revision_log' => 
  array (
  ),
  'revision_translation_affected' => 
  array (
    0 => 
    array (
      'value' => '1',
    ),
  ),
  'default_langcode' => 
  array (
    0 => 
    array (
      'value' => '1',
    ),
  ),
  'path' => 
  array (
  ),
  'body' => 
  array (
    0 => 
    array (
      'value' => '<p>Testing a Drupal function.</p>
',
      'summary' => '',
      'format' => 'basic_html',
    ),
  ),
)

Also, remember to fully qualify the class with its namespace, or add a use \Drupal\node\Entity\Node; line before using the class.

Using drush php-eval 'print json_encode(\Drupal\node\Entity\Node::load(1));' I just get {"in_preview":null} because json_encode() doesn't prints out all the object properties.

1
  • While techncially correct, the data you receive with this pretty useless and huge, due to injected objects and references. Just use $node->toArray() and dump that. See drupal.stackexchange.com/questions/200046/… and similar questions
    – Berdir
    Jun 18, 2016 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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