Skip to content
Snippets Groups Projects

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

Merged Eric Bremner requested to merge feature/ISTWCMS-4653-ebremner-unpublished-menu into 8.x-1.x
Files
2
+ 40
1
@@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Database\Connection;
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;
}
/**
@@ -282,12 +293,40 @@ class UWService implements UWServiceInterface {
*/
public function uwGetMenu(string $menu_name = 'main', bool $count_menu_items = FALSE, bool $include_parent_in_count = FALSE): array {
// 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;
// Get the main menu from the simplify menu module.
$menu = $this->simplifyMenu->getMenuTree($menu_name);
// Set it to the menu_tree which is done by simplify menu.
$menu = $menu['menu_tree'];
// Step through each menu and ensure that it is published.
foreach ($menu as $key => $m) {
// 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]);
}
}
}
// If we want to have the count of menu items, then count them.
if ($count_menu_items) {
Loading