MenuTabs.php

MenuTabs

Namespace

Drupal\ooe\Menu

File

lib/Drupal/ooe/Menu/MenuTabs.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * MenuTabs
  5. */
  6. namespace Drupal\ooe\Menu;
  7. use Drupal\ooe\Module\DefaultModuleHelper;
  8. use Drupal\ooe\Page\IPageController;
  9. use Drupal\ooe\Page\IPageMenuItem;
  10. use Drupal\ooe\Factory\IFactory;
  11. /**
  12. * Implements management of a group of menu tabs.
  13. *
  14. * UML: @link http://drupal7demo.webel.com.au/node/1016 MenuTabs @endlink
  15. *
  16. * @author darrenkelly
  17. */
  18. class MenuTabs extends DefaultModuleHelper implements IMenuTabs {
  19. /**
  20. * The path portion of the URL for the menu set.
  21. *
  22. * @var string
  23. */
  24. private $path;
  25. /**
  26. * The base path page controller (will be shared with the 1st tab menu item).
  27. *
  28. * @var \Drupal\ooe\Page\IPageController
  29. */
  30. private $pageControllerBase;
  31. /**
  32. * The controlled page menu item for the base path of the tabs.
  33. * It will have the same controller as 1st (default) tab menu item.
  34. *
  35. * @var \Drupal\ooe\Page\IPageMenuItem
  36. */
  37. private $menuItemBase;
  38. /**
  39. * The controlled page menu item for the default path of the tabs (1st tab).
  40. * It will have the same controller as the base menu item.
  41. *
  42. * @var \Drupal\ooe\Page\IPageMenuItem
  43. */
  44. private $menuItemDefault;
  45. /**
  46. * Constructor.
  47. *
  48. * @param string $module
  49. * Module machine name string
  50. * @param string $path
  51. * Path to the base (primary) menu item
  52. * @param IPageController $pageControllerBase
  53. * Controller for the main page (and 1st tab).
  54. * @param string $title
  55. * Title of the main page menu (and first tab).
  56. * @param IFactory $factory
  57. * Optional factory.
  58. */
  59. public function __construct(
  60. $module, $path, IPageController $pageControllerBase, $title, IFactory $factory = NULL
  61. ) {
  62. parent::__construct($module, $factory);
  63. $this->path = $path;
  64. $this->pageControllerBase = $pageControllerBase;
  65. $this->menuItemBase = $this->factory()->newPageMenuItem(
  66. $pageControllerBase, $title, $path);
  67. $this->menuItemBase->forceAccessCallbackTRUE();
  68. // Will also be inherited into default tab item.
  69. $this->addMenuItem($this->menuItemBase);
  70. $this->menuItemDefault = $this->makeTabMenuItem(
  71. $this->pageControllerBase,
  72. NULL, // $title: Rely on default tab title assignment by item count
  73. // Coder: ERROR | Comments may not appear after statements.
  74. // Webel: https://www.drupal.org/node/2304897
  75. // 'Please relax rule for inline Comments: ..'.
  76. TRUE // $isDefault.
  77. );
  78. // The page controller is shared between the
  79. // "main/base" page and the 1st tab !
  80. $this->addMenuItem($this->menuItemDefault);
  81. // 1
  82. }
  83. /**
  84. * The menu items handled by this.
  85. *
  86. * @var \Drupal\ooe\Menu\IMenuItem[]
  87. */
  88. private $menuItems = array();
  89. /**
  90. * Adds a page menu item to the set of menu items handled by this.
  91. *
  92. * @param \Drupal\ooe\Page\IPageMenuItem $menuItem
  93. * The menu item to add to this.
  94. */
  95. protected function addMenuItem(IPageMenuItem $menuItem) {
  96. $this->menuItems[] = $menuItem;
  97. }
  98. /**
  99. * The menu items handled by this.
  100. *
  101. * @return \Drupal\ooe\Menu\IMenuItem[]
  102. * The array of menu items handled by this.
  103. */
  104. public function getMenuItems() {
  105. return $this->menuItems;
  106. }
  107. /**
  108. * Makes a new tab menuitem with the given page controller and title.
  109. *
  110. * @param \Drupal\ooe\Page\IPageController $pageController
  111. * The page controller for the tab menu item.
  112. * @param string $title
  113. * The title of the menu item
  114. * @param bool $isDefault
  115. * Whether this is a default.
  116. *
  117. * @return \Drupal\ooe\Page\IPageMenuItem
  118. * The new tab menu item.
  119. * @throws \Exception
  120. * If $isDefault is not a boolean.
  121. */
  122. protected function makeTabMenuItem(IPageController $pageController, $title, $isDefault = FALSE) {
  123. if (!is_bool($isDefault)) {
  124. throw new \Exception('$isDefault must be a boolean !');
  125. }
  126. $n = count($this->menuItems); // !
  127. // Webel: re Coder: https://www.drupal.org/node/2304897#comment-8978433
  128. $path = $this->path . "/tab" . $n;
  129. if (empty($title)) {
  130. $title = "Tab$n";
  131. }
  132. $m = $this->factory()->newPageMenuItem($pageController, $title, $path);
  133. if ($isDefault) {
  134. // $m->forceAccessCallbackTRUE(); // NO, inherited.
  135. $m->setTypeDefaultLocalTask(TRUE);
  136. }
  137. else {
  138. $m->forceAccessCallbackTRUE();
  139. $m->setTypeLocalTask(TRUE);
  140. }
  141. return $m;
  142. }
  143. /**
  144. * Creates a new tab menu item configured with a page controller.
  145. *
  146. * @param IPageController $pageController
  147. * Page controller for the page triggered by the tab.
  148. * @param string $title
  149. * Title of the tab.
  150. *
  151. * @return \Drupal\ooe\Page\IPageMenuItem
  152. * The new page menu item for a tab.
  153. */
  154. public function newTabMenuItem(IPageController $pageController, $title) {
  155. $m = $this->makeTabMenuItem($pageController, $title, FALSE);
  156. $this->addMenuItem($m);
  157. return $m;
  158. }
  159. /**
  160. * Sets the weight of the main (parent) menu item.
  161. *
  162. * @param int $weight
  163. * The weight of the main (parent) menu item of this.
  164. *
  165. * @return IMenuTabs
  166. * This.
  167. */
  168. public function setWeight($weight) {
  169. $this->menuItemBase->setWeight($weight);
  170. return $this;
  171. }
  172. }

Classes

Namesort descending Description
MenuTabs Implements management of a group of menu tabs.