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.


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');
  • 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 '18 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 '18 at 23:47

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.