0

I'm trying to pass same arguments to views. Here is my code:

$view = Views::getView('taxonomy_articles');
            if (!empty($age)) {
              $view->setDisplay('block_6');
              $view->setArguments([$term->tid, $age_args]);
              /*
               * override term name with age param
               */
              $termURL = Url::fromRoute('entity.taxonomy_term.canonical', array('taxonomy_term' => $term->tid, 'age' => $age));
              $termName = \Drupal::l(t($term->name), $termURL);
            }
            else {
              $view->setDisplay('block_1');
              $view->setArguments([$term->tid]);
            }

My else part gets executed and array of term ids goes to block_1, my view block machine name.

What I want is similar like:

              else {
                  $view->setDisplay(['block_1','block_7']);
                  $view->setArguments([$term->tid]);
                }

Is this possible? Can we pass array to $view->setDisplay ???

1 Answer 1

1

While rendering a view you can only use on display. So it is only possible to loop through your display array and generate the view within that loop like:

          else {
              foreach (['block_1','block_7'] as $display) {
                $view->setDisplay($display);
                $view->setArguments([$term->tid]);
                // You need to render here.
                // The loop will overwrite the first display.
              }
            }
2
  • I've had trouble with this approach in the past and found I needed a new View object to get the second display working properly. It was a long time ago though so it may well have been fixed/improved since then (if it was even broken and not just me doing something else wrong). Thought it was worth a mention just in case
    – Clive
    Commented Apr 20, 2017 at 9:29
  • 1
    @Clive It is possible that you had trouble, but in this scenario both the view and the argument are the same, so rendering different displays should be possible. Just store them in an array or concatenate them as rendered strings (or something) within the loop.
    – absoludo
    Commented Apr 20, 2017 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.