I'm using hook_views_pre_render to change the output of a view. The view outputs an entity_reference field of a paragraph called field_date. This paragraph contains two date fields, a begin date and an end date (I'm not using the daterange module of drupal 8 core).

The field_date in the view is [hidden]. Multiple field settings is set to display all values in same row with a simple seperator. It is used in a Custom text field to output with other fields like so:

<a href="{{ path }}" class="card card__agenda col-4 month-{{ field_begin_datum_1 }}">
<div class="wrapper__image">
    <div class="wrapper__tags">
        <div class="tag date">{{ field_date }}</div>
        <div class="tag discipline">{{ field_discipline }}</div>
    <div class="image">{{ field_afbeelding }}</div>
<div class="wrapper__text">
    <div class="title">{{ title }}</div>
    <div class="body">{{ field_korte_omschrijving }}</div>
    <div class="wrapper__tags">
        <div class="tag locatie">{{ field_event_locatie }}</div>

The date output looks like this:

Wed, 01/17/2018 - 20:00
Wed, 01/17/2018 - 23:00,
Mon, 02/19/2018 - 20:00
Mon, 02/19/2018 - 21:00

With the code below I can read the target_id of a multi field entity_reference to a paragraph.

foreach ($view->result as $i => $val) {
  $dateArray = $val->_entity->get('field_date')->getValue();

This code will output all target_id of the results the view gives. In one particular case the ouput is.

target_id: 41 target_id: 52

So two fields, with these ids I load the paragraph to see what values they have and find out I don't want to output the target_id: 52 field, I only want to show the date in January so target_id: 41. How can I exclude it or hide it from this hook without changing the values for good?

My complete hook is:

function hook_views_pre_render($view) {
    if ($view->id() == "view_id" && $view->current_display == 'view_display') {
        // Get a node storage object.
        $node_storage = \Drupal::entityTypeManager()->getStorage('paragraph');
        $dateArray = $val->_entity->get('field_date')->getValue();
        foreach ($dateArray as $iDate => $valDate) {
            //Get target id
           $targetId = $valDate['target_id'];
           //Load target paragraph
           $targetParagraph = $node_storage->load($targetId);
           //Get this month
           $resultMonth = explode('-', $targetParagraph->get('field_begin_datum')->value)[1];
           //If result month is not same as group month, hide output
           if($resultMonth != $month){
               //don't output this paragraph
               //output this paragraph

Thanks in advance

  • Please provide a little more detail about your view ... without this hook is the paragraph displayed ? how ? – GiorgosK Jan 17 '18 at 21:15
  • Hello Giorgos, thanks for your reply, I updated my question with more detailed information about the view. – Joost Jan 17 '18 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.