domain_menu_access-8.x-1.x-dev/domain_menu_access.install
domain_menu_access.install
<?php
/**
* @file
* Install, update and uninstall functions for the Domain Menu Access module.
*/
use Drupal\domain_access\DomainAccessManagerInterface;
use Drupal\user\Entity\Role;
/**
* Implements hook_install().
*/
function domain_menu_access_install() {
if (\Drupal::isConfigSyncing()) {
// Configuration is assumed to already be checked by the config importer
// validation events.
return;
}
$text = [];
$text['menu_link_content'] = [
'name' => 'menu_link_content',
'label' => 'Send for all affiliates',
'description' => 'Make this menu entry available on all domains.',
];
$entity_type = $bundle = 'menu_link_content';
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity_form_display_storage */
$entity_form_display_storage = \Drupal::entityTypeManager()->getStorage('entity_form_display');
// By default, form display for menu_content_link entity doesn't exists.
// We should create "default" form display for entity type to ensure that's
// domain access fields will be successfully added.
if (!$entity_form_display_storage->load($entity_type . '.' . $bundle . '.default')) {
$display = $entity_form_display_storage->create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => 'default',
'status' => TRUE,
]);
$display->save();
}
domain_access_confirm_fields($entity_type, $bundle, $text);
}
/**
* Implements hook_uninstall().
*
* Removes access control fields on uninstall.
*/
function domain_menu_access_uninstall() {
$entity_type = $bundle = 'menu_link_content';
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $storage */
$entity_form_display_storage = \Drupal::entityTypeManager()->getStorage('entity_form_display');
if ($display = $entity_form_display_storage->load($entity_type . '.' . $bundle . '.default')) {
$display->delete();
}
$field_names = [
$entity_type . '.' . DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD,
$entity_type . '.' . DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD,
];
$field_storage_config = \Drupal::entityTypeManager()->getStorage('field_storage_config');
$fields = $field_storage_config->loadMultiple($field_names);
if (count($fields) !== 0) {
$field_storage_config->delete($fields);
}
}
/**
* Add new permission to roles that have the 'administer domains' permission.
*/
function domain_menu_access_update_9001(&$sandbox) {
$roles = Role::loadMultiple();
foreach ($roles as $role) {
if ($role->hasPermission('administer domains')) {
$role->grantPermission('administer menu items across domains');
$role->save();
}
}
}
