3

I am executing a View in code of a paragraph preprocess to allow dynamic listings. The user selects which content types they would like to show, and the View should return nodes of those types with the same taxonomy selected. The idea is they can add multiple paragraphs with different configuration, leveraging the same View just with different arguments.

In this view, Content Type is an exposed filter, set to allow multiple. Here is the code in the preprocess_paragraph hook:

$types = $paragraph->field_related_content_types->getValue();
$content_types = [];

foreach ($types as $type) {
  $content_types[] = $type['target_id'];
}

$view = Views::getView('related_content');
$view->setDisplay('related_content_display');
$view->setArguments(['type' => $content_types]);
$view->setExposedInput(['type' => $content_types]);
$view->preExecute();
$view->execute();

if (count($view->result)) {
  $variables['results'] = $view->buildRenderable('related_content_display', ['type' => $content_types]);
}

'type' is the filter name in the View. If I change it to only allow one value and pass it as a string, it works. So it seems like a syntax issue, but I cannot figure it out.

The method seems to indicate that this is possible:

  /**
   * Sets the arguments for the view.
   *
   * @param array $args
   *   The arguments passed to the view.
   */
  public function setArguments(array $args) {
    // The array keys of the arguments will be incorrect if set by
    // views_embed_view() or \Drupal\views\ViewExecutable:preview().
    $this->args = array_values($args);
  }

enter image description here

  • 1
    Aren't you mixing an matching things here? How come you're doing setExposedInput and setArguments for the same thing? Are you doing an exposed filter AND contextual filter for the type? As setArguments is only applicable for contexutal links isnt it. If you've only got the exposed filter for type you should be able to remove setArguments and the 2nd argument on buildRenderable (which you dont need anyway if you're doing setArguments) I've got something very similar with an exposed type filter and just pass it an array and it works ['type' => ['news','page']] – Leigh May 24 at 13:57
  • Yeah, now it works. I also had to include the type as the key in the array, and remove the buildRenderable second argument, then it works. You can add as an answer and I will accept. – Kevin May 24 at 15:10
2

It looks like you're you doing an exposed filter and contextual filter for the type. As setArguments is for contextual filters and setExposedInput for exposed filters. If you've only got the exposed filter for type you should be able to remove setArguments and the 2nd argument on buildRenderable (which you dont need if you're doing setArguments)

I've got something very similar with an exposed type filter and just pass it an array to setExposedInput and it seems to works

$view->setExposedInput(['type' => ['news','page']]);

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.