3

In template_preprocess_page() in my MYTHEME.theme file, I'm trying to validate if node has specific value. When I dpm($variables) I get:

Array
(
    [node] => stdClass Object
    (
        [__CLASS__] => Drupal\node\Entity\Node
        [in_preview] => 
        [values] => Array
        (
            [vid] => Array
                (
                    [x-default] => 2
                )
...
    [page] => Array
    (
...

When I dpm($variables['node']) I get:

stdClass Object
(
    [__CLASS__] => Drupal\node\Entity\Node
    [in_preview] => 
    [values] => Array
        (
...

When I dpm($variables['node']->values) I get empty result.

Why so?

  • Maybe the array is empty? – Clive Jul 15 '16 at 14:17
  • Just edit my question. You can now see a values chunk. And no, it's not empty. – Vlad Stratulat Jul 15 '16 at 14:24
  • It's difficult to understand where this object is coming from - nodes are typed objects in D8, how come you're working with a stdClass? – Clive Jul 15 '16 at 14:27
  • Well, I just started with D8 so I was expecting it's working the same as in D7. How otherwise you'd get object values in case of D8? – Vlad Stratulat Jul 15 '16 at 14:29
  • Just so that you know, I didn't add any custom modules. Just core and devel module. – Vlad Stratulat Jul 15 '16 at 14:30
4

you can use :

dpm($variables['node']->toArray());

  • It does work this way, but my question is why it doesn't work with an object. – Vlad Stratulat Jul 15 '16 at 14:26
  • 1
    because the values property is a protected property of ContentEntityBase class and Node extends ContentEntityBase api.drupal.org/api/drupal/… – izus Jul 15 '16 at 14:34
  • 1
    In which case php would be throwing a cannot access protected warning...if you're in a dev environment, make sure your error reporting is such that you'll see warnings like this, it'll be less painful to debug. – Clive Jul 15 '16 at 14:40
0

And for better debug view I advise you to use "kint" instead! You can iterate better through your objects.

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.