material_admin_support-8.x-1.x-dev/material_admin_support.module

material_admin_support.module
<?php

/**
 * @file
 * Contains material_admin_support.module.
 */

use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\views\ViewEntityInterface;

/**
 * Implements hook_help().
 */
function material_admin_support_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.material_admin_support':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Support module extends the functionality of Material_admin theme') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_theme().
 */
function material_admin_support_theme() {
  return [
    'node__content_browser' => [
      'render element' => 'content',
      'base hook' => 'node',
      'template' => 'node--content-browser',
    ],
    'media__media_browser' => [
      'render element' => 'media',
      'base hook' => 'media',
      'template' => 'media--media-browser',
    ],
    'commerce_product__browser' => [
      'base hook' => 'commerce_product',
    ],
    'material_admin_support_fab' => [
      'base hook' => 'material_admin_support_fab',
      'template' => 'material-admin-support-fab',
      'variables' => [
        'color' => 'red',
        'icon' => 'add',
        'links' => [],
      ],
    ],
  ];
}

/**
 * Implements hook_page_attachments().
 */
function material_admin_support_page_attachments(array &$page) {
  // Attach the global material admin support library to every page.
  $page['#attached']['library'][] = 'material_admin_support/global';
}

/**
 * Implements hook_preprocess_HOOK() for commerce_product__product_browser.
 */
function material_admin_support_preprocess_commerce_product__browser(&$variables) {
  // Add default styles for commerce product's browser view mode.
  // We need it for the admin and the default theme as well.
  $variables['#attached']['library'][] = 'material_admin_support/commerce-product';
}

/**
 * Implements hook_library_info_alter().
 */
function material_admin_support_library_info_alter(&$libraries, $extension) {
  $path = '/' . drupal_get_path('module', 'material_admin_support');

  $base_definition = [
    'version' => 'VERSION',
    'css' => [
      'base' => [
        $path . '/css/browsers.css' => [],
      ],
    ],
    'js' => [
      $path . '/js/browsers.js' => [],
    ],
    'dependencies' => [
      'core/jquery',
      'core/drupal',
    ],
  ];

  // Completely override other entity browser modules with our styling/logic.
  if ($extension == 'content_browser') {
    $libraries['view'] = $base_definition;
  }
  elseif ($extension == 'lightning_media') {
    $libraries['browser.styling'] = $base_definition;
  }
  elseif ($extension == 'entity_browser') {
    $libraries['entity_browser']['css']['theme'][$path . '/css/browsers.css'] = [];
  }
}

/**
 * Implements hook_preprocess_block().
 */
function material_admin_support_preprocess_block(&$variables) {
  // Alter the 'local actions' block, converting it into floating action
  // buttons (FAB).
  if ($variables['plugin_id'] == 'local_actions_block') {
    $links = [];
    $manager = \Drupal::entityTypeManager();

    // Convert the 'node add' page into FABs.
    if (\Drupal::moduleHandler()->moduleExists('node') && isset($variables['content']['node.add_page'])) {
      // Loop through all available node types, adding a button for each type of
      // node the user has access to create.
      /** @var \Drupal\node\NodeTypeInterface $type */
      foreach ($manager->getStorage('node_type')->loadMultiple() as $type) {
        // Check user access.
        $access = $manager->getAccessControlHandler('node')->createAccess($type->id(), NULL, [], TRUE);

        if ($access->isAllowed()) {
          $url = new Url('node.add', ['node_type' => $type->id()]);
          $links[] = [
            'color' => type_style_get_style($type, 'color', '#039BE5'),
            'icon' => type_style_get_style($type, 'icon', 'star'),
            'href' => $url->toString(),
            'tooltip' => $type->label(),
          ];
        }
      }
    }

    // Convert the 'media add' page into FABs.
    if (\Drupal::moduleHandler()->moduleExists('media') && isset($variables['content']['media.add'])) {
      // Assume that the user does not have the ability to add media until
      // proven otherwise.
      $any_access = FALSE;

      // Loop through all available media types, adding a button for each type
      // of media the user has access to create.
      /** @var \Drupal\media\Entity\MediaType $type */
      foreach ($manager->getStorage('media_type')->loadMultiple() as $type) {
        // Check user access.
        $access = $manager->getAccessControlHandler('media')->createAccess($type->id(), NULL, [], TRUE);

        if ($access->isAllowed()) {
          $url = new Url('entity.media.add_form', ['media_type' => $type->id()]);
          $links[] = [
            'color' => type_style_get_style($type, 'color', '#039BE5'),
            'icon' => type_style_get_style($type, 'icon', 'star'),
            'href' => $url->toString(),
            'tooltip' => $type->label(),
          ];

          // The user can add at least one type of media.
          $any_access = TRUE;
        }
      }

      // Add an additional FAB to upload media in bulk if the user can add at
      // least one type of media.
      if ($any_access && isset($variables['content']['lightning_media.bulk_upload'])) {
        $url = new Url('lightning_media.bulk_upload');
        $links[] = [
          'color' => '#ff6f00',
          'icon' => 'file_upload',
          'href' => $url->toString(),
          'tooltip' => 'Bulk upload',
        ];
      }
    }

    // Convert the links into FABs, if any are available.
    if (!empty($links)) {
      $variables['content'] = [
        '#theme' => 'material_admin_support_fab',
        '#links' => $links,
        '#cache' => [
          'contexts' => ['route', 'user.permissions'],
        ],
      ];
    }
  }
}

/**
 * Implements hook_module_implements_alter().
 */
function material_admin_support_module_implements_alter(&$implementations, $hook) {
  // Lightning media overrides the normal Entity Browser widget, but for our
  // use case and admin theme the level of details nesting looks bad.
  if (($hook === 'field_widget_entity_browser_entity_reference_form_alter' || $hook === 'field_widget_form_alter') && isset($implementations['lightning_media'])) {
    unset($implementations['lightning_media']);
  }
}

/**
 * Implements hook_type_style_form_alter().
 */
function material_admin_support_type_style_form_alter(array &$form) {
  // Add a picker to the type style form for material icons.
  _material_admin_support_attach_iconpicker($form['type_style']['icon']);
}

/**
 * A helper function to attach a material icon picker to a form element.
 *
 * @param array &$element
 *   A render array element, which should be a textfield.
 */
function _material_admin_support_attach_iconpicker(array &$element) {
  $element['#attributes']['class'][] = 'use-material-icon-picker';
  $element['#attached']['library'][] = 'material_admin_support/iconpicker';
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function material_admin_support_view_insert(ViewEntityInterface $view) {
  if (\Drupal::isConfigSyncing()) {
    return;
  }

  $module_handler = \Drupal::moduleHandler();

  // Customize the default media browser provided by the Lightning Media module.
  if ($module_handler->moduleExists('lightning_media') && $view->id() === 'media') {
    // Add a new 'media_browser' view mode to media.
    if (!EntityViewMode::load('media.media_browser')) {
      $view_mode = EntityViewMode::create([
        'id' => 'media.media_browser',
        'targetEntityType' => 'media',
        'label' => 'Media Browser',
        'dependencies' => [
          'lightning_core',
          'media',
        ],
      ]);
      $view_mode->setThirdPartySetting('lightning_core', 'description', 'A view mode for use with the Media library');
      $view_mode->save();
    }

    $field = [
      'id' => 'rendered_entity',
      'table' => 'media',
      'field' => 'rendered_entity',
      'relationship' => 'none',
      'group_type' => 'group',
      'admin_label' => '',
      'label' => '',
      'exclude' => FALSE,
      'alter' => [
        'alter_text' => FALSE,
        'text' => '',
        'make_link' => FALSE,
        'path' => '',
        'absolute' => FALSE,
        'external' => FALSE,
        'replace_spaces' => FALSE,
        'path_case' => 'none',
        'trim_whitespace' => FALSE,
        'alt' => '',
        'rel' => '',
        'link_class' => '',
        'prefix' => '',
        'suffix' => '',
        'target' => '',
        'nl2br' => FALSE,
        'max_length' => 0,
        'word_boundary' => TRUE,
        'ellipsis' => TRUE,
        'more_link' => FALSE,
        'more_link_text' => '',
        'more_link_path' => '',
        'strip_tags' => FALSE,
        'trim' => FALSE,
        'preserve_tags' => '',
        'html' => FALSE,
      ],
      'element_type' => '',
      'element_class' => '',
      'element_label_type' => '',
      'element_label_class' => '',
      'element_label_colon' => FALSE,
      'element_wrapper_type' => '',
      'element_wrapper_class' => '',
      'element_default_classes' => TRUE,
      'empty' => '',
      'hide_empty' => FALSE,
      'empty_zero' => FALSE,
      'hide_alter_empty' => TRUE,
      'view_mode' => 'media_browser',
      'entity_type' => 'media',
      'plugin_id' => 'rendered_entity',
    ];

    // The 'Browser' display.
    $display = &$view->getDisplay('entity_browser_1');

    // Render the media using the 'media_browser' view mode instead of
    // displaying a thumbnail of it.
    if (!empty($display)) {
      unset($display['display_options']['fields']['thumbnail__target_id']);
      $display['display_options']['fields']['rendered_entity'] = $field;
    }

    // The 'Image Browser' display.
    $display = &$view->getDisplay('entity_browser_2');

    // Render the media using the 'media_browser' view mode instead of
    // displaying a thumbnail of it.
    if (!empty($display)) {
      unset($display['display_options']['fields']['thumbnail__target_id']);
      $display['display_options']['fields']['rendered_entity'] = $field;
    }

    $view->save();
  }
}

/**
 * Implements hook_preprocess_HOOK() for views_view.
 */
function material_admin_support_preprocess_views_view(&$variables) {
  /** @var \Drupal\views\ViewExecutable $view */
  $view = $variables['view'];
  $style_plugin_options = $view->style_plugin->options;

  // Attach the browser library to media browser views.
  if (!empty($style_plugin_options['row_class']) && strpos($style_plugin_options['row_class'], 'browser--item') !== FALSE) {
    $variables['#attached']['library'][] = 'material_admin_support/browsers';
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc