Skip to content
Snippets Groups Projects
Commit 8f497fff authored by Cyle Carlson's avatar Cyle Carlson
Browse files

Provide new default menu link services so that 'options' can be saved and restored.

parent 1cd5fdb1
Branches 2.x
No related merge requests found
......@@ -53,6 +53,13 @@ function micon_menu_module_implements_alter(&$implementations, $hook) {
}
}
/**
* Implements hook_menu_links_discovered_alter().
*/
function micon_menu_menu_links_discovered_alter(&$links) {
$links['user.logout']['class'] = 'Drupal\micon_menu\MiconLoginLogoutMenuLink';
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
......@@ -154,6 +161,9 @@ function _micon_menu_menu_ui_node_save(NodeInterface $node, array $values) {
));
$entity->enabled->value = 1;
}
// Clean up options.
$values['options'] = array_filter($values['options']['attributes']);
$entity->title->value = trim($values['title']);
$entity->description->value = trim($values['description']);
$entity->link->options = $values['options'];
......@@ -218,8 +228,6 @@ function _micon_menu_form_menu_link_edit_alter_submit($form, FormStateInterface
if (!empty($menu_link_id)) {
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$icon = $form_state->getValue('data-icon');
$menu_link_manager->updateDefinition($menu_link_id, [
'data-icon' => $icon,
]);
$menu_link_manager->updateDefinition($menu_link_id, ['options' => ['attributes' => ['data-icon' => $icon]]]);
}
}
<?php
namespace Drupal\micon_menu;
use Drupal\user\Plugin\Menu\LoginLogoutMenuLink;
/**
* A menu link that shows "Log in" or "Log out" as appropriate.
*/
class MiconLoginLogoutMenuLink extends LoginLogoutMenuLink {
/**
* {@inheritdoc}
*/
protected $overrideAllowed = [
'menu_name' => 1,
'parent' => 1,
'weight' => 1,
'expanded' => 1,
'enabled' => 1,
// Allow override of this variable.
'options' => 1,
];
}
<?php
namespace Drupal\micon_menu;
use Drupal\Core\Menu\MenuLinkDefault;
/**
* Manages discovery, instantiation, and tree building of menu link plugins.
*
* This manager finds plugins that are rendered as menu links.
*/
class MiconMenuLinkDefault extends MenuLinkDefault {
/**
* {@inheritdoc}
*/
protected $overrideAllowed = [
'menu_name' => 1,
'parent' => 1,
'weight' => 1,
'expanded' => 1,
'enabled' => 1,
// Allow override of this variable.
'options' => 1,
];
}
......@@ -10,19 +10,21 @@ use Drupal\Core\Menu\MenuLinkManager;
class MiconMenuLinkManager extends MenuLinkManager {
/**
* {@inheritdoc}
* Performs extra processing on plugin definitions.
*
* By default we add defaults for the type to the definition. If a type has
* additional processing logic, the logic can be added by replacing or
* extending this method.
*
* @param array $definition
* The definition to be processed and modified by reference.
* @param $plugin_id
* The ID of the plugin this definition is being used for.
*/
public function updateDefinition($id, array $new_definition_values, $persist = TRUE) {
$instance = $this->createInstance($id);
if ($instance) {
$new_definition_values['id'] = $id;
$changed_definition = $instance->updateLink($new_definition_values, $persist);
if (isset($new_definition_values['data-icon'])) {
$changed_definition['options']['attributes']['data-icon'] = $new_definition_values['data-icon'];
}
$this->treeStorage->save($changed_definition);
}
return $instance;
protected function processDefinition(array &$definition, $plugin_id) {
// Use the micon link class override.
$this->default['class'] = 'Drupal\micon_menu\MiconMenuLinkDefault';
parent::processDefinition($definition, $plugin_id);
}
}
......@@ -4,6 +4,7 @@ namespace Drupal\micon_menu;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Symfony\Component\DependencyInjection\Reference;
/**
* Modifies the language manager service.
......@@ -18,5 +19,10 @@ class MiconMenuServiceProvider extends ServiceProviderBase {
// to the menu item options.
$definition = $container->getDefinition('plugin.manager.menu.link');
$definition->setClass('Drupal\micon_menu\MiconMenuLinkManager');
// Overrides menu_link.static.overrides service so that we can save
// options statically.
$definition = $container->getDefinition('menu_link.static.overrides');
$definition->setClass('Drupal\micon_menu\MiconStaticMenuLinkOverrides');
}
}
<?php
namespace Drupal\micon_menu;
use Drupal\Core\Menu\StaticMenuLinkOverrides;
/**
* Defines an implementation of the menu link override using a config file.
*/
class MiconStaticMenuLinkOverrides extends StaticMenuLinkOverrides {
/**
* {@inheritdoc}
*/
public function saveOverride($id, array $definition) {
// Only allow to override a specific subset of the keys.
$expected = [
'menu_name' => '',
'parent' => '',
'weight' => 0,
'expanded' => FALSE,
'enabled' => FALSE,
// Micon: options are allowed.
'options' => [],
];
// Filter the overrides to only those that are expected.
$definition = array_intersect_key($definition, $expected);
// Ensure all values are set.
$definition = $definition + $expected;
if ($definition) {
// Cast keys to avoid config schema during save.
$definition['menu_name'] = (string) $definition['menu_name'];
$definition['parent'] = (string) $definition['parent'];
$definition['weight'] = (int) $definition['weight'];
$definition['expanded'] = (bool) $definition['expanded'];
$definition['enabled'] = (bool) $definition['enabled'];
// Micon: options are allowed.
$definition['options'] = $definition['options'];
$id = static::encodeId($id);
$all_overrides = $this->getConfig()->get('definitions');
// Combine with any existing data.
$all_overrides[$id] = $definition + $this->loadOverride($id);
$this->getConfig()->set('definitions', $all_overrides)->save(TRUE);
}
return array_keys($definition);
}
}
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