You are here

lightning_core.module in Lightning Core 8

Contains core functionality for the Lightning distribution.

File

lightning_core.module
View source
<?php

/**
 * @file
 * Contains core functionality for the Lightning distribution.
 */
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Entity\EntityDescriptionInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\lightning_core\Element as ElementHelper;
use Drupal\lightning_core\Entity\EntityFormMode;
use Drupal\lightning_core\Entity\EntityViewMode;
use Drupal\lightning_core\Entity\Role;
use Drupal\lightning_core\Form\RoleForm;
use Drupal\lightning_core\OverrideHelper as Override;
use Drupal\lightning_core\Plugin\views\filter\Bundle;
use Drupal\lightning_core\UpdateManager;

/**
 * Implements hook_local_tasks_alter().
 */
function lightning_core_local_tasks_alter(array &$local_tasks) {
  $local_tasks['entity.node.edit_form']['options']['attributes']['rel'] = 'edit-form';
}

/**
 * Implements hook_library_info_alter().
 */
function lightning_core_library_info_alter(array &$libraries, $extension) {
  if ($extension == 'seven') {
    $url = drupal_get_path('module', 'lightning_core') . '/css/admin-list.grid.css';
    $url = file_create_url($url);
    $url = file_url_transform_relative($url);
    $libraries['global-styling']['css']['component'][$url] = [];
  }
}

/**
 * Implements hook_config_schema_info_alter().
 */
function lightning_core_config_schema_info_alter(array &$definitions) {
  if (isset($definitions['views_filter'])) {
    $definitions['views_filter']['mapping']['expose']['mapping']['argument'] = [
      'type' => 'string',
      'label' => 'Yield to argument',
    ];
  }
}

/**
 * Implements hook_views_plugins_filter_alter().
 */
function lightning_core_views_plugins_filter_alter(array &$plugins) {
  Override::pluginClass($plugins['bundle'], Bundle::class);
}

/**
 * Implements hook_entity_type_alter().
 */
function lightning_core_entity_type_alter(array &$entity_types) {
  Override::entityClass($entity_types['user_role'], Role::class);
  Override::entityClass($entity_types['entity_view_mode'], EntityViewMode::class);
  Override::entityClass($entity_types['entity_form_mode'], EntityFormMode::class);
  Override::entityForm($entity_types['user_role'], RoleForm::class);
  if (\Drupal::moduleHandler()
    ->moduleExists('field_ui')) {
    Override::entityForm($entity_types['entity_view_mode'], '\\Drupal\\lightning_core\\Form\\EntityDisplayModeAddForm', 'add');
    Override::entityForm($entity_types['entity_view_mode'], '\\Drupal\\lightning_core\\Form\\EntityDisplayModeEditForm', 'edit');
    Override::entityForm($entity_types['entity_form_mode'], '\\Drupal\\lightning_core\\Form\\EntityFormModeAddForm', 'add');
    Override::entityForm($entity_types['entity_form_mode'], '\\Drupal\\lightning_core\\Form\\EntityDisplayModeEditForm', 'edit');
  }
}

/**
 * Implements hook_element_info_alter().
 */
function lightning_core_element_info_alter(array &$info) {

  // Add support for the #legend property to checkboxes and radios.
  // @see Element::processLegend()
  $info['radios']['#legend'] = $info['checkboxes']['#legend'] = [];
  $info['radios']['#process'][] = $info['checkboxes']['#process'][] = [
    ElementHelper::class,
    'processLegend',
  ];
}

/**
 * Implements hook_help().
 */
function lightning_core_help($route_name, RouteMatchInterface $route_match) {
  $matched = [];

  // Parse the route name to figure out what display mode we're looking at:
  // 0 is the entire string.
  // 1 is 'view' or 'form'.
  // 2 is the ID of the affected entity type.
  // 3 is 'view_mode' or 'form_mode'.
  // 4 is 'view' or 'form'.
  $expr = '/^entity\\.entity_(view|form)_display\\.([a-z_]+)\\.((view|form)_mode)$/';
  if (preg_match($expr, $route_name, $matched)) {
    $entity_id = sprintf('%s.%s', $route_match
      ->getParameter('entity_type_id'), $route_match
      ->getParameter($matched[3] . '_name'));
    $display_mode = \Drupal::entityTypeManager()
      ->getStorage('entity_' . $matched[3])
      ->load($entity_id);
    if ($display_mode instanceof EntityDescriptionInterface) {
      $description = $display_mode
        ->getDescription();
      if ($description) {
        return '<p>' . $description . '</p>';
      }
    }
  }
}

/**
 * Implements hook_modules_installed().
 */
function lightning_core_modules_installed(array $modules) {

  // Don't do anything during config sync.
  if (\Drupal::isConfigSyncing()) {
    return;
  }

  // Record the semantic version of every module in config.
  $versions = \Drupal::configFactory()
    ->getEditable(UpdateManager::CONFIG_NAME);

  /** @var \Drupal\lightning_core\UpdateManager $update_manager */
  $update_manager = \Drupal::service('lightning.update_manager');
  foreach ($modules as $module) {
    $versions
      ->set($module, $update_manager
      ->getVersion($module));
  }
  $versions
    ->save();
  if (in_array('token', $modules, TRUE)) {
    $view_modes = [];
    foreach (\Drupal::entityTypeManager()
      ->getDefinitions() as $entity_type) {
      $view_modes[] = $entity_type
        ->id() . '.token';
    }
    $view_modes = EntityViewMode::loadMultiple($view_modes);

    /** @var \Drupal\Core\Entity\EntityViewModeInterface $view_mode */
    foreach ($view_modes as $view_mode) {
      $view_mode
        ->setThirdPartySetting('lightning_core', 'internal', TRUE)
        ->save();
    }
  }
  if (in_array('lightning_dev', $modules, TRUE)) {
    $administrator = Role::load('administrator');
    if ($administrator == NULL) {
      $administrator = Role::create([
        'id' => 'administrator',
        'label' => 'Administrator',
      ]);
      $administrator
        ->setIsAdmin(TRUE);
      $administrator
        ->save();
    }
    \Drupal::service('theme_installer')
      ->install([
      'seven',
    ]);
    \Drupal::configFactory()
      ->getEditable('system.theme')
      ->set('admin', 'seven')
      ->set('default', 'seven')
      ->save();
  }
}

/**
 * Implements hook_block_view_alter().
 */
function lightning_core_block_view_alter(array &$build, BlockPluginInterface $block) {
  \Drupal::service('renderer')
    ->addCacheableDependency($build, $block);

  // Always add block_view:BASE_PLUGIN_ID as a cache tag.
  $build['#cache']['tags'][] = 'block_view:' . $block
    ->getBaseId();

  // If the plugin is a derivative, add block_view:FULL_PLUGIN_ID as well.
  if ($block
    ->getDerivativeId()) {
    $build['#cache']['tags'][] = 'block_view:' . $block
      ->getPluginId();
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function lightning_core_form_user_form_alter(array &$form) {
  if (isset($form['account']['roles'])) {

    /** @var \Drupal\user\RoleInterface $role */
    foreach (Role::loadMultiple() as $id => $role) {
      if ($role instanceof EntityDescriptionInterface) {
        $form['account']['roles']['#legend'][$id] = $role
          ->getDescription();
      }
    }
  }
}

/**
 * Rebuilds the service container.
 */
function lightning_core_rebuild_container() {
  require_once \Drupal::root() . '/core/includes/utility.inc';
  $class_loader = \Drupal::service('class_loader');
  $request = \Drupal::request();
  drupal_rebuild($class_loader, $request);
}

/**
 * Implements template_preprocess_block().
 */
function lightning_core_preprocess_block(array &$variables) {
  $variables['attributes']['data-block-plugin-id'] = $variables['elements']['#plugin_id'];
}