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.