summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2013-05-27 03:38:53 (GMT)
committer Alex Pott2013-05-27 03:38:53 (GMT)
commit5165b47b1011dc896fca652c132f49f6198fd174 (patch)
treeacc512d2454cfe8ba782b934715cbaf55b739f02
parent5a31f62c0ea3369d7c4e3e7e057e5f16627b9539 (diff)
Issue #1966298 by das-peter, andypost, Gábor Hojtsy, amateescu, YesCT: Introduce menu link bundles per menus.
-rw-r--r--core/modules/book/book.module23
-rw-r--r--core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php24
-rw-r--r--core/modules/menu/menu.module16
-rw-r--r--core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php3
-rw-r--r--core/modules/menu_link/lib/Drupal/menu_link/Plugin/Core/Entity/MenuLink.php20
-rw-r--r--core/modules/shortcut/shortcut.module23
6 files changed, 108 insertions, 1 deletions
diff --git a/core/modules/book/book.module b/core/modules/book/book.module
index 8679d2f..61e3701 100644
--- a/core/modules/book/book.module
+++ b/core/modules/book/book.module
@@ -40,6 +40,29 @@ function book_help($path, $arg) {
}
/**
+ * Implements hook_entity_bundle_info().
+ */
+function book_entity_bundle_info() {
+ $bundles['menu_link']['book-toc'] = array(
+ 'label' => t('Book'),
+ 'translatable' => FALSE,
+ );
+ return $bundles;
+}
+
+/**
+ * Implements hook_TYPE_load().
+ */
+function book_menu_link_load($entities) {
+ foreach ($entities as $entity) {
+ // Change the bundle of menu links related to a book.
+ if (strpos($entity->menu_name, 'book-toc-') === 0) {
+ $entity->bundle = 'book-toc';
+ }
+ }
+}
+
+/**
* Implements hook_theme().
*/
function book_theme() {
diff --git a/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php b/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php
index 6c3ed1a..106ac64 100644
--- a/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php
+++ b/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php
@@ -333,6 +333,30 @@ class MenuTest extends WebTestBase {
}
/**
+ * Tests menu link bundles.
+ */
+ public function testMenuBundles() {
+ $this->drupalLogin($this->big_user);
+ $menu = $this->addCustomMenu();
+ $bundles = entity_get_bundles('menu_link');
+ $this->assertTrue($bundles[$menu->id()]);
+ $menus = menu_list_system_menus();
+ $menus[$menu->id()] = $menu->label();
+ ksort($menus);
+ $this->assertIdentical(array_keys($bundles), array_keys($menus));
+
+ // Test if moving a menu link between menus changes the bundle.
+ $node = $this->drupalCreateNode(array('type' => 'article'));
+ $item = $this->addMenuLink(0, 'node/' . $node->nid, 'tools');
+ $this->moveMenuLink($item, 0, $menu->id());
+ $this->assertEqual($item->bundle(), 'tools', 'Menu link bundle matches the menu');
+
+ $moved_item = entity_load('menu_link', $item->id(), TRUE);
+ $this->assertNotEqual($moved_item->bundle(), $item->bundle(), 'Menu link bundle was changed');
+ $this->assertEqual($moved_item->bundle(), $menu->id(), 'Menu link bundle matches the menu');
+ }
+
+ /**
* Add a menu link using the menu module UI.
*
* @param integer $plid Parent menu link id.
diff --git a/core/modules/menu/menu.module b/core/modules/menu/menu.module
index 1b43a4e..14c41b0 100644
--- a/core/modules/menu/menu.module
+++ b/core/modules/menu/menu.module
@@ -153,6 +153,22 @@ function menu_entity_info_alter(&$entity_info) {
}
/**
+ * Implements hook_entity_bundle_info().
+ */
+function menu_entity_bundle_info() {
+ $bundles = array();
+ $config_names = config_get_storage_names_with_prefix('menu.menu.');
+ foreach ($config_names as $config_name) {
+ $config = config($config_name);
+ $bundles['menu_link'][$config->get('id')] = array(
+ 'label' => $config->get('label'),
+ );
+ }
+
+ return $bundles;
+}
+
+/**
* Entity URI callback.
*
* @param \Drupal\system\Plugin\Core\Entity\Menu $menu
diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php
index a08d02a..6a3eddb 100644
--- a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php
+++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php
@@ -104,6 +104,9 @@ class MenuLinkStorageController extends DatabaseStorageController {
// Use the weight property from the menu link.
$menu_link->router_item['weight'] = $menu_link->weight;
+ // By default use the menu_name as type.
+ $menu_link->bundle = $menu_link->menu_name;
+
// For all links that have an associated route, load the route object now
// and save it on the object. That way we avoid a select N+1 problem later.
if ($menu_link->route_name) {
diff --git a/core/modules/menu_link/lib/Drupal/menu_link/Plugin/Core/Entity/MenuLink.php b/core/modules/menu_link/lib/Drupal/menu_link/Plugin/Core/Entity/MenuLink.php
index af4c799..5af23d7 100644
--- a/core/modules/menu_link/lib/Drupal/menu_link/Plugin/Core/Entity/MenuLink.php
+++ b/core/modules/menu_link/lib/Drupal/menu_link/Plugin/Core/Entity/MenuLink.php
@@ -35,7 +35,11 @@ use Drupal\Core\Entity\Entity;
* entity_keys = {
* "id" = "mlid",
* "label" = "link_title",
- * "uuid" = "uuid"
+ * "uuid" = "uuid",
+ * "bundle" = "bundle"
+ * },
+ * bundle_keys = {
+ * "bundle" = "bundle"
* }
* )
*/
@@ -49,6 +53,13 @@ class MenuLink extends Entity implements \ArrayAccess, MenuLinkInterface {
public $menu_name = 'tools';
/**
+ * The link's bundle.
+ *
+ * @var string
+ */
+ public $bundle = 'tools';
+
+ /**
* The menu link ID.
*
* @var int
@@ -254,6 +265,13 @@ class MenuLink extends Entity implements \ArrayAccess, MenuLinkInterface {
}
/**
+ * {@inheritdoc}
+ */
+ public function bundle() {
+ return $this->bundle;
+ }
+
+ /**
* Overrides Entity::createDuplicate().
*/
public function createDuplicate() {
diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module
index 2da3d26..db284c5 100644
--- a/core/modules/shortcut/shortcut.module
+++ b/core/modules/shortcut/shortcut.module
@@ -39,6 +39,29 @@ function shortcut_help($path, $arg) {
}
/**
+ * Implements hook_entity_bundle_info().
+ */
+function shortcut_entity_bundle_info() {
+ $bundles['menu_link']['shortcut'] = array(
+ 'label' => t('Shortcut'),
+ 'translatable' => FALSE,
+ );
+ return $bundles;
+}
+
+/**
+ * Implements hook_TYPE_load().
+ */
+function shortcut_menu_link_load($entities) {
+ foreach ($entities as $entity) {
+ // Change the bundle of menu links related to a shortcut.
+ if (strpos($entity->menu_name, 'shortcut-') === 0) {
+ $entity->bundle = 'shortcut';
+ }
+ }
+}
+
+/**
* Implements hook_permission().
*/
function shortcut_permission() {