Question

I want to union two views and pass on arguments from the first to the second view. Below is what I have so far. Not sure, if this is working. At least the bit to pass on arguments is still missing. Any ideas what I should put in the code?

use Drupal\views\Views;
use Drupal\views\ViewExecutable;

/**
 * Implements hook_views_pre_execute().
 */
function mymodule_views_pre_execute(ViewExecutable $view) {
  if ($view->id() == 'music_by_year' && $view->current_display = 'block_2') {

    $view_by_author = Views::getView('music_by_author');
    $view_by_author->setDisplay('block_1');
    $view_by_author->build();

    $query_by_author = $view_by_author->build_info['query'];

    $query_by_year = &$view->build_info['query'];
    $query_by_year->union($query_by_author);
  }
}
Was it helpful?

Solution

What you can do is to load and execute the $view_by_author view as you are doing but passing the by_year arguments/contextual as this:

$arguments = [$view_by_year->build_info['parameter_you_need']];

$view_by_author = Views::getView('music_by_author');
$view_by_author->setArguments($arguments);
$view_by_author->setDisplay('block_1');
$view_by_author->preExecute();
$view_by_author->execute(); // or build, etc

I believe something like this would work for you.

Also, there is a module called Views Field View that might do what you are trying to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top