1

I have a view that is showing event content types. Users can apply for these events. I am using a hook to alter the 'view applicants' so I can show how many applications already excist.

The following code works and shows the values but is somehow skipping the first row and printing all the values one row later.

function frontview_general_preprocess_views_view_field(&$vars) {

  $view = $vars['view'];

  if(isset($vars['view']) && ($vars['view']->id() == 'evenementen') && ($vars['view']->current_display == 'accreditaties_overzicht')) {

    if($vars['field']->options['id'] ==='nothing_1') {

      // Getting the values
      $accreditaties = $vars['row']->_entity->get('field_accreditaties');
      $accreditaties = $accreditaties->getValue('list');
      $amount = count($accreditaties);   

      $vars['field']->options['alter']['text'] = 'View Applicants ('. $amount .')';
    }

  }
}

This is my current output, the first row still shows the normal values without the hook but the first row values are printed on the second row, second row on third row, ...

current output

This is the output that I need

enter image description here

I'm not sure why it's doing that, when I dd() my code it looks fine.

Thanks in advance!

*Drupal 8

1

Got it to work with the following code. I needed to output the changed value in the $vars['output']. This would override the url in the nothing_1 field so I then had to create a new link and use that to alter the original value.

/**
 * Implements hook_preprocess_views_view_field().
 */
function frontview_general_preprocess_views_view_field(&$vars) {

  $view = $vars['view'];

  if(isset($vars['view']) && ($vars['view']->id() == 'evenementen') && ($vars['view']->current_display == 'accreditaties_overzicht')) {

    if($vars['field']->options['id'] ==='nothing_1') {
      $accreditaties = $vars['row']->_entity->get('field_accreditaties');
      $accreditaties = $accreditaties->getValue('list');
      $amount = count($accreditaties);   

      // $vars['field']->options['alter']['text'] = 'View Applicants ('. $amount .')';

      $nid_value = $vars['row']->nid;
      $url = Link::fromTextAndUrl(t('View Applicants ('. $amount .')'), Url::fromRoute('view.evenementen.page_2', ['arg_0' => $nid_value]))->toString();

      $vars['output'] = $url;
    }
  }
}
| improve this answer | |

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.