0

According to my requirements I have to use views preprocess and views-view.html.twig

function MYTHEME_preprocess_views_view(array &$variables) {


  $aliasManager = \Drupal::service('path.alias_manager');

    if (isset($variables['rows'][0]['#rows'])) {
      $nodeValues = [];
      foreach ($variables['rows'][0]['#rows'] as $value) {

        $nodeValue['created'] = $value['#row']->_entity->created->value;
        $nodeValue['external_url'] = $value['#row']->_entity->field_url->uri;
        $nodeValue['title'] = $title = $value['#row']->_entity->title->value;
        $nodeValue['nid'] = $title = $value['#row']->_entity->nid->value;
        $nodeValue['tid'] = $value['#row']->_entity->field_topic->entity->tid->value;

        // section referenced to node
        $nodeValue['section'] = $value['#row']->_entity->field_topic->entity->name->value;

        $topics = "";
        foreach($value['#row']->_entity->field_topic2 as $val){
          $topics != "" && $topics .= ", ";
          $topics .= $val->entity->name->value;
        }

        $nodeValue['topics'] = $topics;

        $nodeValues[] = $nodeValue;


      }

      $variables['node_values'] = $nodeValues;
    }

 }

In twig file :

{% for value in node_values %}
        <div class="link_full">
            <div class="title"><a href="{{ value.external_url }}"> {{ value.title }} </a>
            </div>
            {% if logged_in %}
                {% if 'premium' in user.getroles %}
                    <div class="link_details">
                        <div class="timestamp"><a href="{{ path('entity.node.canonical', {'node': value.nid}) }}">{{ value.created|date(format='y-m-d H:i') }}</a>
                        </div>
                        <div class="topics">{{ value.topics }}</div>
                    </div>
                {% endif %}
            {% endif %}
        </div>
    {% endfor %}

Out put is like below :

enter image description here

Everything is working fine but I need to create Links of terms in above screenshot. And in code its TOPICS in preprocess function.

I have tried various ways to create it as link but that does not work as it just print a tags as html but not links. Please suggest.

Thanks!

1 Answer 1

0

I have solved this like below :

It was always considered as string so I have to send topics in from of array like below:

$topics = array();
foreach($value['#row']->_entity->field_topic2 as $val){
  $topics[$val->entity->tid->value]= $val->entity->name->value;
}

And then in Twig I made links from them and it worked.

<div class="topics">
    {% for key,item in value.topics %}
        <a href="{{ path('entity.taxonomy_term.canonical', {'taxonomy_term': key}) }}">{{item}}</a>
        {% if not loop.last %},{% endif %} # for adding commas after each term
    {% endfor %}
</div>

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.