Score:1

Programmatically load the same view multiple times with different contextual filters values

cn flag

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.

in flag
This may be related to [this question](https://drupal.stackexchange.com/questions/307728/render-view-inside-block-with-caching), 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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.