You are here

viewsreference.module in Views Reference Field 8.2

Same filename and directory in other branches
  1. 8 viewsreference.module

Defines a field type to embed views in an entity reference field.

File

viewsreference.module
View source
<?php

use Drupal\Component\Utility\Xss;
use Drupal\Component\Utility\Html;

/**
 * @file
 * Defines a field type to embed views in an entity reference field.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\views\ViewExecutable;

/**
 * Implements hook_help().
 */
function viewsreference_help($route_name, RouteMatchInterface $arg) {
  if ($route_name === 'help.page.viewsreference') {
    $output = file_get_contents(drupal_get_path('module', 'viewsreference') . '/README.md');
    return \Drupal::moduleHandler()
      ->moduleExists('markdown') ? Xss::filterAdmin(\Drupal::moduleHandler()
      ->invoke('markdown', 'filter', [
      'process',
      0,
      -1,
      $output,
    ])) : '<h3>viewsreference README</h3><pre>' . Html::escape($output) . '</pre>';
  }
}

/**
 * Implements hook_theme().
 */
function viewsreference_theme($existing, $type, $theme, $path) {
  return [
    'viewsreference__view_title' => [
      'template' => 'viewsreference--view-title',
      'render element' => 'variables',
    ],
  ];
}

/**
 * Implements hook_preprocess_HOOK().
 */
function viewsreference_preprocess_viewsreference__view_title(&$variables) {
  $variables['title'] = [
    '#markup' => $variables['variables']['#title'],
  ];
}

/**
 * Implements hook_module_implements_alter().
 */
function viewsreference_module_implements_alter(&$implementations, $hook) {
  if ($hook === 'views_pre_render') {

    // Move viewsreference_views_pre_render() to the end of the list.
    $viewsreference_hook = $implementations['viewsreference'];
    unset($implementations['viewsreference']);
    $implementations['viewsreference'] = $viewsreference_hook;
  }
}

/**
 * Implements hook_views_pre_view().
 */
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);
      }
    }
  }
}

/**
 * Implements hook_views_pre_render().
 */
function viewsreference_views_pre_render($view) {

  // If using AJAX, forward the viewsreference data and settings in the request.
  if (isset($view->element['#viewsreference']) && $view
    ->ajaxEnabled() && empty($view->is_attachment) && empty($view->live_preview)) {
    $view->element['#attached']['drupalSettings']['views']['ajaxViews']['views_dom_id:' . $view->dom_id]['viewsreference'] = $view->element['#viewsreference'];
  }
  return $view;
}