Commit b3cb5c86 authored by webchick's avatar webchick

Issue #722650 by alexpott, tim.plunkett, arpeggio: Fixed Shortcut doesn't...

Issue #722650 by alexpott, tim.plunkett, arpeggio: Fixed Shortcut doesn't properly clean up or store menu links.
parent 5142b788
......@@ -54,6 +54,13 @@ class Shortcut extends ConfigEntityBase {
*/
public $label;
/**
* An array of menu links.
*
* @var array
*/
public $links = array();
/**
* Overrides \Drupal\Core\Entity\Entity::uri().
*/
......
......@@ -29,11 +29,12 @@ protected function attachLoad(&$queried_entities, $revision_id = FALSE) {
}
}
/**
* Overrides \Drupal\config\ConfigStorageController::save().
* Overrides \Drupal\config\ConfigStorageController::create().
*/
public function save(EntityInterface $entity) {
public function create(array $values) {
$entity = parent::create($values);
// Generate menu-compatible set name.
if (!$entity->getOriginalID()) {
// Save a new shortcut set with links copied from the user's default set.
......@@ -42,23 +43,30 @@ public function save(EntityInterface $entity) {
// Size of menu_name is 32 so id could be 23 = 32 - strlen('shortcut-').
$id = substr($entity->id(), 0, 23);
$entity->set('id', $id);
$entity->set('links', $default_set->links);
foreach ($entity->links as $link) {
$link = $link->createDuplicate();
$link->menu_name = $id;
unset($link->mlid);
$link->save();
if ($default_set->id() != $id) {
foreach ($default_set->links as $link) {
$link = $link->createDuplicate();
$link->enforceIsNew();
$link->menu_name = $id;
$link->save();
$entity->links[$link->uuid()] = $link;
}
}
}
return $entity;
}
/**
* Overrides \Drupal\config\ConfigStorageController::preSave().
*/
public function preSave(EntityInterface $entity) {
// Just store the UUIDs.
if (isset($entity->links)) {
foreach ($entity->links as $uuid => $link) {
$entity->links[$uuid] = $uuid;
}
foreach ($entity->links as $uuid => $link) {
$entity->links[$uuid] = $uuid;
}
return parent::save($entity);
parent::preSave($entity);
}
/**
......@@ -66,10 +74,8 @@ public function save(EntityInterface $entity) {
*/
function postSave(EntityInterface $entity, $update) {
// Process links in shortcut set.
// If links were provided for the set, save them.
if (isset($entity->links)) {
foreach ($entity->links as $uuid) {
$menu_link = entity_load_by_uuid('menu_link', $uuid);
foreach ($entity->links as $uuid) {
if ($menu_link = entity_load_by_uuid('menu_link', $uuid)) {
// Do not specifically associate these links with the shortcut module,
// since other modules may make them editable via the menu system.
// However, we do need to specify the correct menu name.
......
......@@ -118,6 +118,14 @@ function testShortcutLinkDelete() {
$saved_set = shortcut_set_load($set->id());
$mlids = $this->getShortcutInformation($saved_set, 'mlid');
$this->assertFalse(in_array($link->mlid, $mlids), 'Successfully deleted a shortcut.');
// Delete all the remaining shortcut menu links.
foreach (array_filter($mlids) as $mlid) {
menu_link_delete($mlid);
}
// Get the front page to check that no exceptions occur.
$this->drupalGet('');
}
/**
......
......@@ -270,6 +270,18 @@ function shortcut_link_access($menu_link) {
return FALSE;
}
/**
* Implements hook_menu_link_delete().
*/
function shortcut_menu_link_delete($menu_link) {
// If the deleted menu link was in a shortcut set, remove it.
if (strpos($menu_link->menu_name, 'shortcut-') === 0) {
$shortcut = entity_load('shortcut', str_replace('shortcut-', '', $menu_link->menu_name));
unset($shortcut->links[$menu_link->uuid]);
$shortcut->save();
}
}
/**
* Loads the data for a shortcut set.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment