0

I am getting an object from views_get_view_result().

Array
(
 [0] => Drupal\views\ResultRow Object
    (
        [_entity] => Drupal\node\Entity\Node Object
            (
                [in_preview] => 
                [values:protected] => Array
                    (
                        [vid] => Array
                            (
                                [x-default] => 9
                            )

                        [langcode] => Array
                            (
                                [x-default] => en
                            )

                        [revision_uid] => Array
                            (
                                [x-default] => 1
                            )

                        [revision_timestamp] => Array
                            (
                                [x-default] => 1508904645
                            )

                        [revision_log] => Array
                            (
                                [x-default] => 
                            )

How do I convert it to an array? My objective is to get the content types fields values. In an entity object there is toArray() to achieve this, but it doesn't work for what views_get_view_result() returns.

1 Answer 1

0

This is the entity you are looking for:

$node = $row->_entity;

And the field values:

$values = $node->toArray();

These are all of the values from the node, however if you want the results from the fields configured in the Views UI, see Get result view with formatter programmattically

4
  • It doesn't work. There isn't such thing as toArray() Commented Oct 27, 2017 at 8:35
  • It's hard to debug your code without knowing it, but suppose you have the rows in $rows, then loop over them with foreach ($rows as $row) and then you should be able to find the entity of each row in $row.
    – 4uk4
    Commented Oct 27, 2017 at 9:04
  • It works. Is there any way to do this without looping the object array? Commented Oct 27, 2017 at 9:41
  • Yes, you can use array functions, but this is more a php question.
    – 4uk4
    Commented Oct 27, 2017 at 9:44

Your Answer

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

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