0

Example a view with show fields:

enter image description here

I want retrive array result like this:

[
  [title,
   date_start
   data_end
   ...
  ]
]

3 Answers 3

2

Use $view->result to get the filtered rows, $view-field for the fields, on which you can use the methods getValue() for the field value and render() for the formatted field as render array.

An example:

use Drupal\views\Views;

  $values = [];
  $view = views::getview('view_example');
  if ($view) {
    $view->execute();
    foreach ($view->result as $rid => $row) {
      foreach ($view->field as $fid => $field ) {
        $values[$rid][$fid . '-value'] = $field->getValue($row);
        $values[$rid][$fid . '-render'] = $field->render($row);
      }
    }
  }
2
  • ! i try, THZ :D
    – BOES
    Commented Nov 3, 2016 at 13:23
  • 1
    This worked for me. One thing to note is that tokens and some field formaters don't work with render, $field->advancedRender() works though
    – lukedekker
    Commented Nov 29, 2018 at 23:57
0

You can use the field "global text" and add all fields inside of this field for show all fields that you have in one field

I hope can help you

1
  • Unfortunately no, I need the fields in code formatted... it is impossible that you can not do
    – BOES
    Commented Nov 2, 2016 at 22:14
0

Does this work?

  1. Exclude all the fields from display except the last one.
  2. Be sure to have the Token Module installed and enabled.
  3. Rewrite the output of the last field with the tokens of the needed fields and your array formatting: [[title],...] The available patterns should be visible below.
  4. Adjust the output to your needs and through the rewrite of the last field with the tokens or the formatting of the excluded fields (perhaps necessary for the format of dates and strings).
5
  • No, i want use result in php. I use View:getView for retrive THE view
    – BOES
    Commented Nov 2, 2016 at 23:00
  • What are you "getting" in PHP so far. So what happens when you print it?
    – pehka
    Commented Nov 3, 2016 at 7:45
  • Return entities, not formatted fields. I remember correctly or drupal 7 returned formatted files wit view_get_view ?
    – BOES
    Commented Nov 3, 2016 at 7:48
  • No, but it's possible to get the view's result. Drupal 8 comes with "views_get_view_result" api.drupal.org/api/drupal/core!modules!views!views.module/… Does that help? This should do the same.
    – pehka
    Commented Nov 7, 2016 at 20:56
  • Haven't seen the answer above, sorry.
    – pehka
    Commented Nov 7, 2016 at 20:58

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.