diff --git a/src/Service/UWService.php b/src/Service/UWService.php index f2589f87350d8c9cdf641b44cecbfdc29129f93a..c3d7b33936331b86dac2a4a25608abf105eb21ac 100644 --- a/src/Service/UWService.php +++ b/src/Service/UWService.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\node\NodeInterface; use Drupal\simplify_menu\MenuItems; +use Drupal\path_alias\AliasManager; /** * Class UWService. @@ -38,6 +39,13 @@ class UWService implements UWServiceInterface { */ private $simplifyMenu; + /** + * Simplify_menu menu items. + * + * @var \Drupal\simplify_menu\MenuItems + */ + private $pathAliasManager; + /** * Default constructor. * @@ -47,11 +55,14 @@ class UWService implements UWServiceInterface { * The database entity. * @param \Drupal\simplify_menu\MenuItems $simplifyMenu * The simplify_menu menu items. + * @param \Drupal\path_alias\AliasManager $pathAliasManager + * The Drupal path alias manager. */ - public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database, MenuItems $simplifyMenu) { + public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database, MenuItems $simplifyMenu, AliasManager $pathAliasManager) { $this->entityTypeManager = $entityTypeManager; $this->database = $database; $this->simplifyMenu = $simplifyMenu; + $this->pathAliasManager = $pathAliasManager; } /** @@ -288,6 +299,9 @@ class UWService implements UWServiceInterface { // Set it to the menu_tree which is done by simplify menu. $menu = $menu['menu_tree']; + // Ensure that we only get the published menu items. + $this->uwCheckPublishedMenuItems($menu); + // If we want to have the count of menu items, then count them. if ($count_menu_items) { @@ -298,6 +312,45 @@ class UWService implements UWServiceInterface { return $menu; } + /** + * {@inheritDoc} + */ + public function uwCheckPublishedMenuItems(&$menu): void { + + // The base path, need this for removing when on subfoldered sites, + // for example d8/fdsu5/, we need to remove the fdsu5 from the path + // alias. + global $base_path; + + // Step through each menu and ensure that it is published. + foreach ($menu as $key => $m) { + + // If there is a submenu (i.e. children), process it first. + if (isset($m['submenu'])) { + $this->uwCheckPublishedMenuItems($menu[$key]['submenu']); + } + + // Remove the base path from the url so that we can get + // the actual content from the path alias. + $alias = str_replace($base_path, '', $m['url']); + + // Get the path from the URL of the menu link. + $path = $this->pathAliasManager->getPathByAlias('/' . $alias); + + // Check if it is a node path and if so check if published. + if (preg_match('/^node\/(\d+)$/', $path, $matches)) { + + // Load in the node based on the path. + $node = $this->entityTypeManager->getStorage('node')->load($matches[1]); + + // If the node is unpublished, remove it from the menus. + if ($node->status->value == 0) { + unset($menu[$key]); + } + } + } + } + /** * {@inheritDoc} */ diff --git a/src/Service/UWServiceInterface.php b/src/Service/UWServiceInterface.php index 93714b1bc5e2ff1366d6e94ac9ce4d38f3954c13..e61893790b51b22049b61ec9f92716df5bcb6e93 100644 --- a/src/Service/UWServiceInterface.php +++ b/src/Service/UWServiceInterface.php @@ -98,6 +98,14 @@ interface UWServiceInterface { */ public function uwGetMenu(string $menu_name = 'main', bool $count_menu_items = FALSE, bool $include_parent_in_count = FALSE): array; + /** + * Function to check that all menu links are published. + * + * @param array $menu + * The array of menus. + */ + public function uwCheckPublishedMenuItems(array &$menu): void; + /** * A function to setup the menu for UW display. * diff --git a/uw_cfg_common.services.yml b/uw_cfg_common.services.yml index 815b941feb978d33b58ce789aa7d2f0aca980e07..49c1f917d40f2d10083a3e0ce514a208b7882851 100644 --- a/uw_cfg_common.services.yml +++ b/uw_cfg_common.services.yml @@ -1,7 +1,7 @@ services: uw_cfg_common.uw_service: class: Drupal\uw_cfg_common\Service\UWService - arguments: ['@entity_type.manager', '@database', '@simplify_menu.menu_items'] + arguments: ['@entity_type.manager', '@database', '@simplify_menu.menu_items', '@path_alias.manager'] uw_cfg_common.route_subscriber: class: Drupal\uw_cfg_common\Routing\UwNodeAccessRouteSubscriber tags: