3

I can get the value of the field from the view object like this:

foreach ($view['#view']->result as $row){
      $field_slide_banner = $row->_entity->get('field_slider_banner')->getValue();     
    }

However, in this case I get the target_id of the referenced image file, but I need to get the full rendered output data, which depends on format that I set for this field at the view display, for example, that should be the URL of the image in my case.

In other words, I want to be able to get final output data or HTML markup programmatically for any view field from the given row of executed view object.

1

1 Answer 1

3

Use the following code.

foreach ($view->result as $row) {
  // Change the full view with your view mode.
  $render_array = $row->_entity->field_slider_banner->view('full');
  $field_slider_banner_markup = \Drupal::service('renderer')->renderRoot($render_array);
}

Alternatively, you can use the following code.

foreach ($view->result as $id => $row) {
  $my_field_value = $view->style_plugin->getFieldValue($id, 'field_name');
}
6
  • 1
    Thanks, it works and I can see the full output of the field. Also, I have found other solution, where it is possible to get field value without surrounding markup like this: $result = $view->style_plugin->getFieldValue($id, $field); where id is the row number and field is the name of the field.
    – JetSet
    Dec 29, 2018 at 23:41
  • i'm glad that help, i will add the second way to my answer, in case someone needs it. thanks .
    – berramou
    Dec 29, 2018 at 23:47
  • Pretty sure this is not the answer; this is how to get a field from the primary views entity. i.e. an entity field; not a views field.
    – liquidcms
    Dec 15, 2021 at 18:05
  • If you are sure itsn't the answer add one ;)
    – berramou
    Dec 16, 2021 at 10:54
  • 1
    Here you go: drupal.stackexchange.com/questions/308755/… - please don't go around down-voting my answers just because you don't understand them. :(
    – liquidcms
    Dec 16, 2021 at 21:21

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.