shortcut.install

Same filename and directory in other branches
  1. 11.x core/modules/shortcut/shortcut.install
  2. 10 core/modules/shortcut/shortcut.install
  3. 9 core/modules/shortcut/shortcut.install
  4. 8.9.x core/modules/shortcut/shortcut.install
  5. 7.x modules/shortcut/shortcut.install

Install, update and uninstall functions for the shortcut module.

File

core/modules/shortcut/shortcut.install

View source
<?php


/**
 * @file
 * Install, update and uninstall functions for the shortcut module.
 */

use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;

/**
 * Implements hook_schema().
 */
function shortcut_schema() : array {
  $schema['shortcut_set_users'] = [
    'description' => 'Maps users to shortcut sets.',
    'fields' => [
      'uid' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {users}.uid for this set.',
      ],
      'set_name' => [
        'type' => 'varchar_ascii',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
        'description' => "The {shortcut_set}.set_name that will be displayed for this user.",
      ],
    ],
    'primary key' => [
      'uid',
    ],
    'indexes' => [
      'set_name' => [
        'set_name',
      ],
    ],
    'foreign keys' => [
      'set_user' => [
        'table' => 'users',
        'columns' => [
          'uid' => 'uid',
        ],
      ],
      'set_name' => [
        'table' => 'shortcut_set',
        'columns' => [
          'set_name' => 'set_name',
        ],
      ],
    ],
  ];
  return $schema;
}

/**
 * Implements hook_install().
 */
function shortcut_install(bool $is_syncing) : void {
  // Theme settings are not configuration entities and cannot depend on modules
  // so to set a module-specific setting, we need to set it with logic.
  if (\Drupal::service('theme_handler')->themeExists('claro')) {
    \Drupal::configFactory()->getEditable("claro.settings")
      ->set('third_party_settings.shortcut.module_link', TRUE)
      ->save();
  }
  if (\Drupal::service('theme_handler')->themeExists('olivero')) {
    \Drupal::configFactory()->getEditable("olivero.settings")
      ->set('third_party_settings.shortcut.module_link', TRUE)
      ->save();
  }
  // Add the shortcuts block to the navigation layout if Navigation is already
  // installed. When both are installed in the same request,
  // navigation_install() may have already added the block via invokeAll(),
  // so check for duplicates.
  if (!$is_syncing && \Drupal::moduleHandler()->moduleExists('navigation')) {
    $config = \Drupal::configFactory()->get('navigation.block_layout');
    if (!$config->isNew()) {
      $already_exists = FALSE;
      foreach ($config->get('sections') ?? [] as $section) {
        foreach ($section['components'] ?? [] as $component) {
          if (isset($component['configuration']['id']) && $component['configuration']['id'] === 'navigation_shortcuts') {
            $already_exists = TRUE;
            break 2;

          }
        }
      }
      if (!$already_exists) {
        \Drupal::service('plugin.manager.config_action')->applyAction('addNavigationBlock', 'navigation.block_layout', [
          'region' => 'content',
          'configuration' => [
            'id' => 'navigation_shortcuts',
            'label' => 'Shortcuts',
            'label_display' => '0',
            'provider' => 'shortcut',
          ],
          'weight' => 0,
          'additional' => [],
        ]);
      }
    }
  }
}

/**
 * Implements hook_uninstall().
 */
function shortcut_uninstall() : void {
  // Theme settings are not configuration entities and cannot depend on modules
  // so to unset a module-specific setting, we need to unset it with logic.
  if (\Drupal::service('theme_handler')->themeExists('claro')) {
    \Drupal::configFactory()->getEditable("claro.settings")
      ->clear('third_party_settings.shortcut')
      ->save();
  }
  if (\Drupal::service('theme_handler')->themeExists('olivero')) {
    \Drupal::configFactory()->getEditable("olivero.settings")
      ->clear('third_party_settings.shortcut')
      ->save();
  }
  // Remove the navigation_shortcuts block from the navigation layout.
  // @todo Fix Navigation so it does not store dependencies in config.
  //   https://www.drupal.org/project/drupal/issues/3587499
  if (\Drupal::moduleHandler()->moduleExists('navigation')) {
    /** @var \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface $section_storage_manager */
    $section_storage_manager = \Drupal::service('plugin.manager.layout_builder.section_storage');
    $navigation_storage = $section_storage_manager->load('navigation', [
      'navigation' => new Context(new ContextDefinition('string'), 'navigation'),
    ]);
    if ($navigation_storage) {
      foreach ($navigation_storage->getSections() as $section) {
        foreach ($section->getComponents() as $uuid => $component) {
          if ($component->getPluginId() === 'navigation_shortcuts') {
            $section->removeComponent($uuid);
          }
        }
        $navigation_storage->save();
      }
    }
  }
}

Functions

Title Deprecated Summary
shortcut_install Implements hook_install().
shortcut_schema Implements hook_schema().
shortcut_uninstall Implements hook_uninstall().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.