tasty_backend-8.x-1.0-beta3/tasty_backend.module
tasty_backend.module
<?php
/**
* @file
* Primary module hooks for Tasty Backend module.
*/
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\tasty_backend\TastyBackendManager;
use Drupal\user\RoleInterface;
/**
* Implements hook_toolbar_alter().
*/
function tasty_backend_toolbar_alter(&$items) {
// Alter access to the default Administration menu.
$user = \Drupal::currentUser();
$items['administration']['tab']['#access'] = $user->hasPermission('access default administration menu');
// Add icons css.
$items['toolbar_menu_tb_manage']['#attached']['library'][] = 'tasty_backend/tasty_backend_icons';
}
/**
* Implements hook_menu_links_discovered_alter().
*/
function tasty_backend_menu_links_discovered_alter(&$links) {
// Add node links for each content type.
foreach (\Drupal::entityTypeManager()->getStorage('node_type')->loadMultiple() as $type) {
$links['tasty_backend.node.add.' . $type->id()] = [
'title' => $type->label(),
'route_name' => 'node.add',
'parent' => 'tasty_backend.add_content',
'menu_name' => 'tb-manage',
'route_parameters' => ['node_type' => $type->id()],
];
}
// Links for taxonomy vocabularies.
foreach (Vocabulary::loadMultiple() as $vocab) {
$links['tasty_backend.entity.taxonomy_vocabulary.overview_form.' . $vocab->id()] = [
'title' => $vocab->label(),
'route_name' => 'entity.taxonomy_vocabulary.overview_form',
'parent' => 'tasty_backend.taxonomy_vocabulary.collection',
'menu_name' => 'tb-manage',
'route_parameters' => ['taxonomy_vocabulary' => $vocab->id()],
];
$links['tasty_backend.entity.taxonomy_term.add_form.' . $vocab->id()] = [
'title' => t('Add term'),
'route_name' => 'entity.taxonomy_term.add_form',
'parent' => 'tasty_backend.entity.taxonomy_vocabulary.overview_form.' . $vocab->id(),
'menu_name' => 'tb-manage',
'route_parameters' => ['taxonomy_vocabulary' => $vocab->id()],
];
}
// Links for menus.
$menus = \Drupal::entityTypeManager()->getStorage('menu')->loadMultiple();
foreach ($menus as $menu_name => $menu) {
$links['tasty_backend.entity.menu.edit_form.' . $menu_name] = [
'title' => $menu->label(),
'route_name' => 'entity.menu.edit_form',
'menu_name' => 'tb-manage',
'parent' => 'tasty_backend.menu.collection',
'route_parameters' => ['menu' => $menu_name],
];
$links['tasty_backend.entity.menu.add_link_form.' . $menu_name] = [
'title' => t('Add link'),
'route_name' => 'entity.menu.add_link_form',
'menu_name' => 'tb-manage',
'parent' => 'tasty_backend.entity.menu.edit_form.' . $menu_name,
'route_parameters' => ['menu' => $menu_name],
];
}
}
/**
* Implements hook_entity_bundle_create().
*/
function tasty_backend_entity_bundle_create($entity_type_id, $bundle) {
if ($entity_type_id == 'node') {
$type = \Drupal::entityTypeManager()->getStorage('node_type')->load($bundle);
// Add the management view.
TastyBackendManager::addAdminView($type);
// Add permissions to the content admin role.
if (\Drupal::config('tasty_backend.settings')->get('apply_default_content_perms')) {
TastyBackendManager::addContentTypePermissions($type);
}
}
// Add default vocabulary permissions to the content admin role.
if ($entity_type_id == 'taxonomy_term' && \Drupal::config('tasty_backend.settings')->get('apply_default_vocab_perms')) {
$vocabulary = \Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary')->load($bundle);
TastyBackendManager::addVocabularyPermissions($vocabulary);
}
}
/**
* Implements hook_entity_bundle_delete().
*/
function tasty_backend_entity_bundle_delete($entity_type_id, $bundle) {
if ($entity_type_id == 'node') {
TastyBackendManager::deleteAdminView($bundle);
}
}
/**
* Implements hook_ENTITY_TYPE_insert().
*
* Temporary fix to allow the user admin role to be created initially and
* then have the relevant role_delegation permission assigned.
*
* @see https://www.drupal.org/project/role_delegation/issues/3354012
* @see https://www.drupal.org/project/drupal/issues/3368702
*/
function tasty_backend_user_role_insert(RoleInterface $role) {
if ($role->id() === 'user_admin') {
$role->grantPermission('assign user_admin role');
$role->save();
}
}
