1

Hi I am trying to do similar in Drupal 8 :

Can we write php conditions in view field templates?

Is that possible to fetch fields related to node such as terms, comments count and comment form link, revisions, voting count and voting widget and edit link in one field template. If I create template file of title field like How can we override view field template in drupal 8 and need to fetch other fields related to node in that same template, But we should not add other fields in view itself but fetch other fields related to node in preprocess or by some other method.

Please suggest, I am trying but not able to conclude exact approach.

enter image description here

File made template based on above which is in action for title field everywhere :

views-view-field--title.html.twig

Is that possible to fetch other fields related to node in this template without adding fields in view ?

Thanks!

2
  • Can you share your progress made so far Mar 28, 2018 at 12:59
  • I am sure if there is way to fetch such fields, related to my progress I have uploaded screenshot where it shows title is displayed in view and twig debug suggestion. I created template file in my theme with field name which works but I need to fetch rest of field there without adding fields in view, is that possible ? Based on this now I got more detailed theme suggestions drupal.stackexchange.com/questions/227709/…
    – jas
    Mar 28, 2018 at 13:16

1 Answer 1

1

In my opinion it's better to write a template preprocess for your view in yourtheme.theme file. hook_preprocess_views_view()

function yourtheme_preprocess_views_view(array &$variables) { 
  if($variables['view']->id() == "your_view_id") {
    if (isset($variables['rows'][0]['#rows'])) {
      $nodeValues = [];
      foreach ($variables['rows'][0]['#rows'] as $value) {
        $nodeValue['taxonomyValues'] = $value['#row']->_entity->field_taxonomy->entity;
          :
          :
        $nodeValues[] = $nodeValue;
      }
      $variables['node_values'] = $nodeValues;
    }
  }
}

Above, is a sample code for fetching a taxonomy reference field. Fetch required fields and pass it in your own variable to views-view.html.twig file

To further give template suggestion for views-view.html.twig use

function yourtheme_theme_suggestions_views_view_alter(array &$suggestions, array $variables)

In your views-view.html.twig

{% for value in node_values %}
   {{value.taxonomyValues}}
{% endfor %}
10
  • Thanks for answer +1, Is that kind of similar drupal.stackexchange.com/questions/251824/… , do we need to pass taxonomyTermReference to variable like $variables['custom_terms'] = $taxonomyTermReference; , when I tried to var_dump($variables) it didnt show any thing, please suggest !
    – jas
    Mar 28, 2018 at 13:54
  • I simply tried to pass $variables['custom_terms'] = "test"; in hook you suggested and passed {{ custom_terms }} in views-view.html.twig file just to test but it did not work, is that something I am doing incorrect ?
    – jas
    Mar 28, 2018 at 13:58
  • Update it worked but only for last row of each view but not for all rows? what can we do to make it working for each row !
    – jas
    Mar 28, 2018 at 14:01
  • 1
    To get $title = $value['#row']->_entity->title->value, $body = $value['#row']->_entity->body->value, $imageUrl = $value['#row']->_entity->field_image->entity->url(). I dont know whether there is a better way to get these values. Mar 29, 2018 at 6:51
  • 1
    This is also a interesting approach using Display modes to display set of links together. drupalize.me/blog/201403/exploring-new-drupal-8-display-modes just sharing another approach !
    – jas
    Mar 29, 2018 at 7:24

Your Answer

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

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