You are here

views_attach_library.module in Views Attach Library 2.0.x

Same filename and directory in other branches
  1. 8 views_attach_library.module
  2. 8.x views_attach_library.module

This file adds a library to views page or block.

File

views_attach_library.module
View source
<?php

/**
 * @file
 * This file adds a library to views page or block.
 */
use Drupal\views\ViewExecutable;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_views_pre_render().
 */
function views_attach_library_views_pre_render(ViewExecutable $view) {
  $current_display = $view->current_display;
  $view_config = $view->storage
    ->getDisplay($current_display);
  if (!empty($view_config['display_options']['display_extenders']['library_in_views_display_extender'])) {
    $libraries = explode(',', $view_config['display_options']['display_extenders']['library_in_views_display_extender']['attach_library']['attach_library']);
    foreach ($libraries as $library) {

      // Attach library to view.
      $view->element['#attached']['library'][] = trim($library);
    }
  }
}

/**
 * Implements hook_help().
 */
function views_attach_library_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.views_attach_library':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t("The library in views module is designed to attach JS and CSS library in views, by providing just library name.") . '</p>';
      $output .= '</dl>';
      return $output;
  }
}