Skip to content
Snippets Groups Projects
Commit 2dccebc1 authored by Igor Biki's avatar Igor Biki
Browse files

Merge branch 'feature/ISTWCMS-4199-ebremner-menu-items-count' into '8.x-1.x'

Feature/istwcms 4199 ebremner menu items count

See merge request !17
parents fd010828 fcd2f303
No related branches found
No related tags found
1 merge request!17Feature/istwcms 4199 ebremner menu items count
...@@ -6,6 +6,7 @@ use Drupal\Core\Entity\EntityInterface; ...@@ -6,6 +6,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\Core\Database\Connection; use Drupal\Core\Database\Connection;
use Drupal\simplify_menu\MenuItems;
/** /**
* Class UWService. * Class UWService.
...@@ -30,6 +31,13 @@ class UWService implements UWServiceInterface { ...@@ -30,6 +31,13 @@ class UWService implements UWServiceInterface {
*/ */
private $database; private $database;
/**
* Simplify_menu menu items.
*
* @var \Drupal\simplify_menu\MenuItems
*/
private $simplifyMenu;
/** /**
* Default constructor. * Default constructor.
* *
...@@ -37,10 +45,13 @@ class UWService implements UWServiceInterface { ...@@ -37,10 +45,13 @@ class UWService implements UWServiceInterface {
* Entity Type Manager from core. * Entity Type Manager from core.
* @param \Drupal\Core\Database\Connection $database * @param \Drupal\Core\Database\Connection $database
* The database entity. * The database entity.
* @param \Drupal\simplify_menu\MenuItems $simplifyMenu
* The simplify_menu menu items.
*/ */
public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database) { public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database, MenuItems $simplifyMenu) {
$this->entityTypeManager = $entityTypeManager; $this->entityTypeManager = $entityTypeManager;
$this->database = $database; $this->database = $database;
$this->simplifyMenu = $simplifyMenu;
} }
/** /**
...@@ -259,4 +270,109 @@ class UWService implements UWServiceInterface { ...@@ -259,4 +270,109 @@ class UWService implements UWServiceInterface {
return $return_content_types; return $return_content_types;
} }
/**
* {@inheritDoc}
*/
public function uwGetMenu(string $menu_name = 'main', bool $count_menu_items = FALSE, bool $include_parent_in_count = FALSE): array {
// 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'];
// If we want to have the count of menu items, then count them.
if ($count_menu_items) {
// Add the count of the menu items.
$menu = $this->uwSetMenuItems($menu, $include_parent_in_count);
}
return $menu;
}
/**
* {@inheritDoc}
*/
public function uwSetMenuItems(array $menus, bool $include_parent_in_count = FALSE): array {
// Step through the menu and do a few things:
// (a) Remove the Home link (this will be a house icon)
// (b) Add the number of menu items per menu link.
foreach ($menus as $index => $menu) {
// If this is the Home link, then remove it.
if ($menu['text'] == "Home") {
// Remove the home link.
unset($menus[$index]);
}
// If we are not on the home link, then add the number
// of menu items in this menu link.
else {
// Set the menu_item_count, initially to 0.
$menu_items_count = 0;
// If there is a submenu, recursivley call the count function.
if (isset($menu['submenu']) && count($menu['submenu']) > 0) {
// Call the count function recursively till we have the number of
// menu items.
$this->uwCountMenuItems($menu['submenu'], $menu_items_count);
// Set the menu items count.
// If we need to include the parent in the count, for example the
// main menu will print the parent inside the tray, then we increment
// the menu items count by 1.
if ($include_parent_in_count) {
$menu_items_count++;
}
// Set the menu items count in the menu.
$menus[$index]['menu_items_count'] = $menu_items_count;
}
// If there are no submenus, the count is 0, there will be no tray.
else {
// Set the number of items count, which is 0.
$menus[$index]['menu_items_count'] = $menu_items_count;
}
}
}
return $menus;
}
/**
* {@inheritDoc}
*/
public function uwCountMenuItems($menus, &$menu_items_count): void {
// Step through the menus and check for submenus and count.
foreach ($menus as $menu) {
// If there is a submenu, recursivley call the count function.
if (isset($menu['submenu']) && count($menu['submenu']) > 0) {
// Increment the menu items counter.
$menu_items_count++;
// Recursively check the submenu.
$this->uwCountMenuItems($menu['submenu'], $menu_items_count);
}
// If there are no submenus, simple increment the menu items counter.
else {
// Increment the menu items counter.
$menu_items_count++;
}
}
}
} }
...@@ -83,4 +83,68 @@ interface UWServiceInterface { ...@@ -83,4 +83,68 @@ interface UWServiceInterface {
*/ */
public function getUwContentTypes(bool $with_sidebar = FALSE): array; public function getUwContentTypes(bool $with_sidebar = FALSE): array;
/**
* A function to get an array of menu.
*
* @param string $menu_name
* A string to the machine name of the menu to get.
* @param bool $count_menu_items
* A boolean on whether to count the number of menu items.
* @param bool $include_parent_in_count
* A boolean on whether to include the parent in the menu items count.
*
* @return array
* An array of the menu.
*/
public function uwGetMenu(string $menu_name = 'main', bool $count_menu_items = FALSE, bool $include_parent_in_count = FALSE): array;
/**
* A function to setup the menu for UW display.
*
* This function will count of the number of menu items,
* and use a recursive function (uwCountMenuItems) to count
* the total number of menu items.
*
* The variable include_parent_in_count tells us if we need
* to add one to the total count of menu items. This is required
* for displaying some menus as the parent is also included
* in the displaying of the menu (i.e. the tray of the main and
* information for menus).
*
* For example, the menu structure will look like:
* Parent
* Child #1
* Grandchild #1-1
* Grandchild #1-2
* Child #2
* Child #3
* Grandchild #3-1
* Great grandchild #3-1-1
* Great grandchild #3-1-2
* Grandchild #3-2
* Grandchild #3-3
* Great grandchild #3-3-1
* So it should with the recursive function it will return 11.
* If we include the parent the count will be 12.
*
* @param array $menus
* A reference to the array list of menu items.
* @param bool $include_parent_in_count
* A boolean on whether to include the parent in the menu items count.
*
* @return array
* An array of the updated menu items.
*/
public function uwSetMenuItems(array $menus, bool $include_parent_in_count = FALSE): array;
/**
* A function to recursively count the number of menu items in the submenu.
*
* @param array $menu
* An array that contains the submenu.
* @param int &$menu_items_count
* A reference to the integer that is storing the number of menu items.
*/
public function uwCountMenuItems(array $menu, int &$menu_items_count): void;
} }
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'] arguments: ['@entity_type.manager', '@database', '@simplify_menu.menu_items']
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