1

I'm trying to load a view X times but with a different contextual filter value each time. My code generates the correct amount of view blocks, but they are all the exact same. They all correspond to the first value passed. Here's my code (in hook_preprocess_node) :

  if ($node->getType() == 'parent'){
    $parent= $node->field_datas_taxonomy->getValue()[0]['target_id'];
    $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadChildren($parent);
    foreach($terms as $term){
      $pname = $term->get('name')->value;
      $tid =  $term->get('tid')->value;
      $args = [$tid];
      $view = \Drupal\views\Views::getView('vue_datas');
      if (is_object($view)) {
        $view->setArguments($args);
        $view->setDisplay('block_a');
        $view->preExecute();
        $view->execute();
        $content = $view->buildRenderable('block_a', $args);
        $variables['view_datas'][] = $content;
      }
    }
  }

If this issue is tied to a contextual filters restriction, I'm ok with using a filter instead, I just don't know how.

Any advice? Thanks.

1
  • This may be related to this question, although I'm not sure. I'm struggling with something similar, where I'm rendering the same view several times, with different displays and arguments. In each case, only the first values are used for each subsequent block.
    – Steve
    Apr 21, 2022 at 20:11

0

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.

Browse other questions tagged or ask your own question.