You are here

domain_menus.install in Domain Menus for Domains 9.x

Same filename and directory in other branches
  1. 3.x domain_menus.install
  2. 9.1.x domain_menus.install

Install, update, and uninstall functions for the Domain Menus module.

File

domain_menus.install
View source
<?php

/**
 * @file
 * Install, update, and uninstall functions for the Domain Menus module.
 */
use Drupal\Component\Render\FormattableMarkup;

/**
 * Implements hook_install().
 *
 * If domain_menus module was previously enabled and uninstalled, there may
 * be domain menus that had its domain menus third party settings removed
 * as a result of module uninstallation. Add domain menus third party
 * settings back to these menus on module install so the menus are recognized
 * as domain menus.
 */
function domain_menus_install() {
  $domains = \Drupal::entityTypeManager()
    ->getStorage('domain')
    ->loadMultiple();
  $menus = \Drupal::entityTypeManager()
    ->getStorage('menu')
    ->loadMultiple();
  foreach ($menus as $menu) {
    foreach ($domains as $domain) {
      $domain_id = $domain
        ->id();
      $domain_domainid = $domain
        ->getDomainId();
      $menu_domainid_partial = trim(new FormattableMarkup(DOMAIN_MENUS_MENU_ID_PATTERN, [
        '@domain_domainid' => $domain_domainid,
        '@menu_name' => '',
      ]));
      $menu_id = $menu
        ->id();
      if (strpos($menu_id, $menu_domainid_partial) === 0) {
        $menu
          ->setThirdPartySetting('domain_menus', 'domains', array(
          $domain_id => $domain_id,
        ));
        $menu
          ->setThirdPartySetting('domain_menus', 'auto-created', 1);
        $menu
          ->save();
      }
    }
    reset($domains);
  }
}

/**
 * Implements hook_uninstall().
 */
function domain_menus_uninstall() {
}

/**
 * Apply htmlspecialchars_decode() to domain menu labels. Versions prior to
 * 8.x-1.0-alpha6 saved menu labels with special char encoding and causes
 * undesired output.
 */
function domain_menus_update_8101() {
  $domains = \Drupal::entityTypeManager()
    ->getStorage('domain')
    ->loadMultiple();
  $menus = \Drupal::entityTypeManager()
    ->getStorage('menu')
    ->loadMultiple();
  foreach ($menus as $menu) {
    $menu_domains = $menu
      ->getThirdPartySetting("domain_menus", "domains");
    if ($menu_domains !== NULL) {
      $menu
        ->set('label', htmlspecialchars_decode($menu
        ->get('label'), ENT_QUOTES));
      $menu
        ->save();
    }
  }
}

Functions

Namesort descending Description
domain_menus_install Implements hook_install().
domain_menus_uninstall Implements hook_uninstall().
domain_menus_update_8101 Apply htmlspecialchars_decode() to domain menu labels. Versions prior to 8.x-1.0-alpha6 saved menu labels with special char encoding and causes undesired output.