custom_add_content-8.x-1.2/custom_add_content.install
custom_add_content.install
<?php
/**
* @file
* Custom add content install.
*/
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\node\Entity\NodeType;
/**
* Implements hook_install().
*/
function custom_add_content_install() {
// Module's menu creation.
Drupal::entityTypeManager()
->getStorage('menu')
->create([
'id' => 'custom-add-content-page',
'label' => 'Custom add content page',
'description' => 'Content creation links',
])
->save();
// Menu item creation for each content type.
$ct_list = NodeType::loadMultiple();
foreach ($ct_list as $ct_machine_name => $obj) {
$item = MenuLinkContent::create([
'title' => $obj->get('name'),
'link' => ['uri' => 'internal:/node/add/' . $ct_machine_name],
'menu_name' => 'custom-add-content-page',
'expanded' => TRUE,
]);
$item->save();
}
// This module must execute after core's node module and i18n module.
module_set_weight('custom_add_content', 15);
}
/**
* Implements hook_uninstall().
*/
function custom_add_content_uninstall() {
// Module's menu deletion.
Drupal::entityTypeManager()
->getStorage('menu')
->load('custom-add-content-page')->delete();
}
