diff --git a/config/install/system.menu.custom-add-content-page.yml b/config/install/system.menu.custom-add-content-page.yml new file mode 100644 index 0000000000000000000000000000000000000000..3e3f2f1ce83636b3c114e9a2f4914b28179a2453 --- /dev/null +++ b/config/install/system.menu.custom-add-content-page.yml @@ -0,0 +1,7 @@ +langcode: und +status: true +dependencies: { } +id: custom-add-content-page +label: 'Custom Add Content Page' +description: '' +locked: false diff --git a/custom_add_content.deploy.php b/custom_add_content.deploy.php new file mode 100644 index 0000000000000000000000000000000000000000..572f7fe0b6e030f8a6cb3136485a743b4e018184 --- /dev/null +++ b/custom_add_content.deploy.php @@ -0,0 +1,20 @@ +<?php + +/** + * @file + * Deploy hooks for custom_add_content. + */ + +/** + * Create menu items for every existing content type. + */ +function custom_add_content_deploy_populate_menu(): string { + /** @var \Drupal\custom_add_content\Services\Helper\MenuHelperInterface $helper */ + $helper = Drupal::service('custom_add_content.services.menu_helper'); + $status = $helper->createMenuItems(); + if (!$status) { + return t('The destination menu can not be found.'); + } + + return t('Created menu links for the existing content types.'); +} diff --git a/custom_add_content.install b/custom_add_content.install index c482456d68d70c34251f8fa2c413503359a87014..ef0ca9b03df4acbffc70afa44554f50698c08f4d 100644 --- a/custom_add_content.install +++ b/custom_add_content.install @@ -5,36 +5,10 @@ * 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(); - } - +function custom_add_content_install(): void { // This module must execute after core's node module and i18n module. module_set_weight('custom_add_content', 15); } @@ -43,9 +17,10 @@ function custom_add_content_install() { * Implements hook_uninstall(). */ function custom_add_content_uninstall() { - // Module's menu deletion. - Drupal::entityTypeManager() - ->getStorage('menu') - ->load('custom-add-content-page')->delete(); + $menu = Drupal::entityTypeManager()->getStorage('menu') + ->load('custom-add-content-page'); + if ($menu) { + $menu->delete(); + } } diff --git a/custom_add_content.services.yml b/custom_add_content.services.yml index 32d30e948a109116f5aa2801acd5aab22dc17978..143119ac4b7576ece58fa5b7aab882ff1cf287b5 100644 --- a/custom_add_content.services.yml +++ b/custom_add_content.services.yml @@ -3,3 +3,8 @@ services: class: Drupal\custom_add_content\Routing\CustomAddContentRouteSubscriber tags: - { name: event_subscriber } + + custom_add_content.services.menu_helper: + class: Drupal\custom_add_content\Services\Helper\MenuHelper + arguments: + - '@entity_type.manager' diff --git a/src/Drush/Commands/HelperCommands.php b/src/Drush/Commands/HelperCommands.php new file mode 100644 index 0000000000000000000000000000000000000000..07354a3f808789bb48aad49da464980ebba3d3cf --- /dev/null +++ b/src/Drush/Commands/HelperCommands.php @@ -0,0 +1,55 @@ +<?php + +namespace Drupal\custom_add_content\Drush\Commands; + +use Drupal\custom_add_content\Services\Helper\MenuHelperInterface; +use Drush\Commands\DrushCommands; +use Symfony\Component\DependencyInjection\ContainerInterface; + +/** + * Helper commands. + */ +class HelperCommands extends DrushCommands { + + /** + * Constructs a HelperCommands instance. + * + * @param \Drupal\custom_add_content\Services\Helper\MenuHelperInterface $helper + * The helper. + */ + public function __construct( + protected MenuHelperInterface $helper + ) { + parent::__construct(); + } + + /** + * Initiate an instance of the these Drush commands. + * + * @param \Symfony\Component\DependencyInjection\ContainerInterface $container + * The container. + * + * @return \Drupal\custom_add_content\Drush\Commands\HelperCommands + * Returns the instance of the Drush commands. + */ + public static function create(ContainerInterface $container): HelperCommands { + return new HelperCommands( + $container->get('custom_add_content.services.menu_helper') + ); + } + + /** + * Create menu items for all content types. + * + * @command custom_add_content:create:menu-items + */ + public function createMenuItems(): void { + $status = $this->helper->createMenuItems(); + if (!$status) { + $this->io()->error('Could not create menu items due to missing menu.'); + } + + $this->io()->success('Created menu links for the existing content types.'); + } + +} diff --git a/src/Services/Helper/MenuHelper.php b/src/Services/Helper/MenuHelper.php new file mode 100644 index 0000000000000000000000000000000000000000..09cfe3203a92195d0f903b5131f2d4acdf75a210 --- /dev/null +++ b/src/Services/Helper/MenuHelper.php @@ -0,0 +1,49 @@ +<?php + +namespace Drupal\custom_add_content\Services\Helper; + +use Drupal\Core\Entity\EntityTypeManagerInterface; + +/** + * Helper for creating menu items. + */ +class MenuHelper implements MenuHelperInterface { + + /** + * Constructs a MenuHelper instance. + * + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * The entity type manager. + */ + public function __construct( + protected EntityTypeManagerInterface $entityTypeManager + ) { + } + + /** + * {@inheritdoc} + */ + public function createMenuItems(): bool { + $menu = $this->entityTypeManager->getStorage('menu') + ->load('custom-add-content-page'); + if (!$menu) { + return FALSE; + } + + // Menu item creation for each content type. + $ct_list = $this->entityTypeManager->getStorage('node_type') + ->loadMultiple(); + foreach ($ct_list as $ct_machine_name => $obj) { + $item = $this->entityTypeManager->getStorage('menu_link_content')->create([ + 'title' => $obj->get('name'), + 'link' => ['uri' => 'internal:/node/add/' . $ct_machine_name], + 'menu_name' => 'custom-add-content-page', + 'expanded' => TRUE, + ]); + $item->save(); + } + + return TRUE; + } + +} diff --git a/src/Services/Helper/MenuHelperInterface.php b/src/Services/Helper/MenuHelperInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..922e08a1296349e9077615ecf148ea9fe007eb3c --- /dev/null +++ b/src/Services/Helper/MenuHelperInterface.php @@ -0,0 +1,18 @@ +<?php + +namespace Drupal\custom_add_content\Services\Helper; + +/** + * Interface for creating menu items. + */ +interface MenuHelperInterface { + + /** + * Create menu items for all content types. + * + * @return boolean + * Returns whether creating was successful. + */ + public function createMenuItems(): bool; + +}