From c1a3621820e31b88936a120b3ed71c038651e79c Mon Sep 17 00:00:00 2001 From: Jasper Lammens <jasper.lammens@dropsolid.com> Date: Tue, 18 Feb 2025 10:29:08 +0100 Subject: [PATCH 1/4] [#3507376] Create menu via config --- config/install/system.menu.custom-add-content-page.yml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 config/install/system.menu.custom-add-content-page.yml 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 0000000..3e3f2f1 --- /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 -- GitLab From 2a53a8d0376023d2ce6fb22ae26848033d14b909 Mon Sep 17 00:00:00 2001 From: Jasper Lammens <jasper.lammens@dropsolid.com> Date: Tue, 18 Feb 2025 10:48:12 +0100 Subject: [PATCH 2/4] [#3507376] Add helper for creating menu items --- custom_add_content.services.yml | 5 +++ src/Services/Helper/MenuHelper.php | 49 +++++++++++++++++++++ src/Services/Helper/MenuHelperInterface.php | 18 ++++++++ 3 files changed, 72 insertions(+) create mode 100644 src/Services/Helper/MenuHelper.php create mode 100644 src/Services/Helper/MenuHelperInterface.php diff --git a/custom_add_content.services.yml b/custom_add_content.services.yml index 32d30e9..143119a 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/Services/Helper/MenuHelper.php b/src/Services/Helper/MenuHelper.php new file mode 100644 index 0000000..09cfe32 --- /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 0000000..922e08a --- /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; + +} -- GitLab From e27f5cc8fa7b516cb4afcfa9547fc7da4d6b8da0 Mon Sep 17 00:00:00 2001 From: Jasper Lammens <jasper.lammens@dropsolid.com> Date: Tue, 18 Feb 2025 10:49:36 +0100 Subject: [PATCH 3/4] [#3507376] Replace install-hook with drush command and deploy hook --- custom_add_content.deploy.php | 23 +++++++++++ custom_add_content.install | 27 ------------- src/Drush/Commands/HelperCommands.php | 55 +++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 27 deletions(-) create mode 100644 custom_add_content.deploy.php create mode 100644 src/Drush/Commands/HelperCommands.php diff --git a/custom_add_content.deploy.php b/custom_add_content.deploy.php new file mode 100644 index 0000000..65948b7 --- /dev/null +++ b/custom_add_content.deploy.php @@ -0,0 +1,23 @@ +<?php + +/** + * @file + * Deploy hooks for custom_add_content. + */ + +/** + * Create menu items for every existing content type. + * + * @return \Drupal\Core\StringTranslation\TranslatableMarkup + * Returns a message regarding the status. + */ +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 c482456..10b5bd1 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(); - } - // This module must execute after core's node module and i18n module. module_set_weight('custom_add_content', 15); } @@ -43,7 +17,6 @@ function custom_add_content_install() { * Implements hook_uninstall(). */ function custom_add_content_uninstall() { - // Module's menu deletion. Drupal::entityTypeManager() ->getStorage('menu') diff --git a/src/Drush/Commands/HelperCommands.php b/src/Drush/Commands/HelperCommands.php new file mode 100644 index 0000000..07354a3 --- /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.'); + } + +} -- GitLab From 22afefff208073267d4815c1df935c98cb4b44b7 Mon Sep 17 00:00:00 2001 From: Jasper Lammens <jasper.lammens@dropsolid.com> Date: Tue, 18 Feb 2025 12:04:44 +0100 Subject: [PATCH 4/4] [#3507376] Fix uninstall hook --- custom_add_content.deploy.php | 5 +---- custom_add_content.install | 10 ++++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/custom_add_content.deploy.php b/custom_add_content.deploy.php index 65948b7..572f7fe 100644 --- a/custom_add_content.deploy.php +++ b/custom_add_content.deploy.php @@ -7,15 +7,12 @@ /** * Create menu items for every existing content type. - * - * @return \Drupal\Core\StringTranslation\TranslatableMarkup - * Returns a message regarding the status. */ 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) { + if (!$status) { return t('The destination menu can not be found.'); } diff --git a/custom_add_content.install b/custom_add_content.install index 10b5bd1..ef0ca9b 100644 --- a/custom_add_content.install +++ b/custom_add_content.install @@ -8,7 +8,7 @@ /** * Implements hook_install(). */ -function custom_add_content_install() { +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); } @@ -18,7 +18,9 @@ function custom_add_content_install() { */ 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(); + } } -- GitLab