Skip to content
Snippets Groups Projects
Commit 0caac0e3 authored by Kevin Paxman's avatar Kevin Paxman
Browse files

Merge branch 'feature/ISTWCMS-4653-ebremner-unpublished-menu' into '8.x-1.x'

ISTWCMS-4653: checking for unpublished nodes when getting the menu

See merge request !61
parents 938dc18b bea56595
No related branches found
No related tags found
1 merge request!61ISTWCMS-4653: checking for unpublished nodes when getting the menu
...@@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityInterface; ...@@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
use Drupal\simplify_menu\MenuItems; use Drupal\simplify_menu\MenuItems;
use Drupal\path_alias\AliasManager;
/** /**
* Class UWService. * Class UWService.
...@@ -38,6 +39,13 @@ class UWService implements UWServiceInterface { ...@@ -38,6 +39,13 @@ class UWService implements UWServiceInterface {
*/ */
private $simplifyMenu; private $simplifyMenu;
/**
* Simplify_menu menu items.
*
* @var \Drupal\simplify_menu\MenuItems
*/
private $pathAliasManager;
/** /**
* Default constructor. * Default constructor.
* *
...@@ -47,11 +55,14 @@ class UWService implements UWServiceInterface { ...@@ -47,11 +55,14 @@ class UWService implements UWServiceInterface {
* The database entity. * The database entity.
* @param \Drupal\simplify_menu\MenuItems $simplifyMenu * @param \Drupal\simplify_menu\MenuItems $simplifyMenu
* The simplify_menu menu items. * 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->entityTypeManager = $entityTypeManager;
$this->database = $database; $this->database = $database;
$this->simplifyMenu = $simplifyMenu; $this->simplifyMenu = $simplifyMenu;
$this->pathAliasManager = $pathAliasManager;
} }
/** /**
...@@ -288,6 +299,9 @@ class UWService implements UWServiceInterface { ...@@ -288,6 +299,9 @@ class UWService implements UWServiceInterface {
// Set it to the menu_tree which is done by simplify menu. // Set it to the menu_tree which is done by simplify menu.
$menu = $menu['menu_tree']; $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 we want to have the count of menu items, then count them.
if ($count_menu_items) { if ($count_menu_items) {
...@@ -298,6 +312,45 @@ class UWService implements UWServiceInterface { ...@@ -298,6 +312,45 @@ class UWService implements UWServiceInterface {
return $menu; 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} * {@inheritDoc}
*/ */
......
...@@ -98,6 +98,14 @@ interface UWServiceInterface { ...@@ -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; 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. * A function to setup the menu for UW display.
* *
......
services: services:
uw_cfg_common.uw_service: uw_cfg_common.uw_service:
class: Drupal\uw_cfg_common\Service\UWService 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: uw_cfg_common.route_subscriber:
class: Drupal\uw_cfg_common\Routing\UwNodeAccessRouteSubscriber class: Drupal\uw_cfg_common\Routing\UwNodeAccessRouteSubscriber
tags: tags:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment