2

I have created a taxonomy-term view. I have added two fields in a view: term-id and custom-text field.

I want to check if term-id has children and depending upon the result, I want to set markup inside custom-text field.

I am able to get term-id in hook_views_pre_render with following code, but I am not able to set the value of custom-text field

function mymodule_views_pre_render(\Drupal\views\ViewExecutable $view) {
 if ($view->id() == 'test_view' && $view->current_display == 'block_2') {
    foreach($view->result as $value){
        $tid = $value->_entity->get('tid')->value;
        kint($tid);

        //trying to set value for custom text field, with foll. line of code 
        //'nothing' is the field-name you get for any custom-text field 
        $value->_entity->set('nothing', '<h1>This is my custom mark-up</h1>');
    }
 }
}

I referred the code from these following 2 sources, but they are not dealing with custom-text field:

  1. How to change views field value using a hook

  2. https://api.drupal.org/comment/62573#comment-62573

Or should I use template_preprocess_views_view_fields(), if yes then how can I get term-id value to check if it has children terms.

Any help will be appreciated.

2 Answers 2

2

enter image description here

You can't set a value for "nothing" aka "Global: Custom text" in hook_views_pre_render() because _entity is the entity object. The entity (Taxonomy Term in your case, and Node in my test case) does not contain this field.

Therefore, you need to use template_preprocess_views_view_field

function THENAME_preprocess_views_view_field(&$vars) {
  if(isset($vars['view']) && ($vars['view']->id() == 'test_view') && ($vars['view']->current_display == 'block_2')) {
    if (isset($vars['view']->field) && (count($vars['view']->field) > 0)) {
      if ($vars['field']->field == 'nothing') {
        $tid = $vars['row']->_entity->get('tid')->value; // get tid
        if ($vars['field']->field == 'nothing') {
          $vars['output'] = [
            '#markup' => "<h1>This is my custom mark-up with term id: $tid</h1>",
          ];
        }
      }
    }
  }
}
1
  • 1
    just for the reference I would like to share one more solution, which I figured out; hope that will be useful for someone. But your solution looks more appropriate. Thanks.
    – Vik Durve
    Nov 24, 2019 at 15:31
0

I referred the code from this source https://drupal.stackexchange.com/a/262718/97205 which was also helpful and figured out following solution:

function THEMENAME_preprocess_views_view_fields(&$variables) {
  $view = $variables['view'];
  $field = $variables['field'];

  if ($view->id() == 'test_view' && $view->current_display == 'block_2') {
    foreach ($view->field as $id => $field) {

      if ($field->field == 'nothing') {
        $tid = $variables['row']->tid; //get tid
        $variables['fields']['nothing']->content = "<h1>Custom markup goes here $tid</h1>";
        //kint($tid);
      }
    }
  }
}

But solution from @NoSssweat is more appropriate.

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.