You are here

function viewsreference_views_pre_view in Views Reference Field 8.2

Implements hook_views_pre_view().

File

./viewsreference.module, line 57
Defines a field type to embed views in an entity reference field.

Code

function viewsreference_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  if (!isset($view->element['#viewsreference']) && empty($view
    ->getRequest()->request
    ->get('viewsreference'))) {
    return;
  }
  if (!empty($view
    ->getRequest()->request
    ->get('viewsreference'))) {
    $view->element['#viewsreference'] = $view
      ->getRequest()->request
      ->get('viewsreference');

    // For ajax views we reset all handlers and make the view initialize again
    // to allow changes from the settings plugins.
    $view->display_handler->handlers = [];
    $view->inited = FALSE;
  }

  // Let all settings plugins alter the view.
  $viewsreference_plugin_manager = \Drupal::service('plugin.manager.viewsreference.setting');
  $plugin_definitions = $viewsreference_plugin_manager
    ->getDefinitions();
  if (isset($view->element['#viewsreference']['enabled_settings'])) {
    foreach ($view->element['#viewsreference']['enabled_settings'] as $enabled_setting) {
      if (!empty($plugin_definitions[$enabled_setting])) {
        $plugin_definition = $plugin_definitions[$enabled_setting];

        /** @var \Drupal\viewsreference\Plugin\ViewsReferenceSettingInterface $plugin_instance */
        $plugin_instance = $viewsreference_plugin_manager
          ->createInstance($plugin_definition['id']);
        $value = isset($view->element['#viewsreference']['data'][$plugin_definition['id']]) ? $view->element['#viewsreference']['data'][$plugin_definition['id']] : $plugin_definition['default_value'];
        $plugin_instance
          ->alterView($view, $value);
      }
    }
  }
}