diff --git a/core/core.services.yml b/core/core.services.yml index 4fab422..40ad444 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -175,6 +175,9 @@ services: plugin.manager.menu.local_action: class: Drupal\Core\Menu\LocalActionManager arguments: ['@container.namespaces', '@controller_resolver', '@request', '@module_handler', '@cache.cache', '@language_manager'] + plugin.manager.menu.contextual_link: + class: Drupal\Core\Menu\ContextualLinkManager + arguments: ['@controller_resolver', '@module_handler', '@cache.cache', '@language_manager'] plugin.manager.menu.local_task: class: Drupal\Core\Menu\LocalTaskManager arguments: ['@controller_resolver', '@request', '@router.route_provider', '@module_handler', '@cache.cache', '@language_manager'] diff --git a/core/includes/theme.inc b/core/includes/theme.inc index d583eb7..c208e4f 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1676,7 +1676,7 @@ function theme_links($variables) { } // Handle links. - if (isset($link['href'])) { + if (isset($link['href']) || isset($link['route_name'])) { $is_current_path = ($link['href'] == current_path() || ($link['href'] == '' && drupal_is_front_page())); $is_current_language = (empty($link['language']) || $link['language']->id == $language_url->id); if ($is_current_path && $is_current_language) { @@ -1697,8 +1697,18 @@ function theme_links($variables) { $item = drupal_render($link_element); } else { - // Pass in $link as $options, they share the same keys. - $item = l($link['title'], $link['href'], $link); + // @todo theme_links() should *really* use the same parameters as l(), + // and just take an array of '#type' => 'link' elements. + // Pass in $link as $options, as they share the same keys. + if (isset($link['href'])) { + $item = l($link['title'], $link['href'], $link); + } + else { + if (empty($link['route_parameters'])) { + $link['route_parameters'] = array(); + } + $item = \Drupal::l($link['title'], $link['route_name'], $link['route_parameters'], $link); + } } } // Handle title-only text items. diff --git a/core/lib/Drupal/Core/Menu/ContextualLinkDefault.php b/core/lib/Drupal/Core/Menu/ContextualLinkDefault.php new file mode 100644 index 0000000..94c46b6 --- /dev/null +++ b/core/lib/Drupal/Core/Menu/ContextualLinkDefault.php @@ -0,0 +1,53 @@ +t() when https://drupal.org/node/2087231 lands. + return t($this->pluginDefinition['title']); + } + + /** + * {@inheritdoc} + */ + public function getRouteName() { + return $this->pluginDefinition['route_name']; + } + + /** + * {@inheritdoc} + */ + public function getGroup() { + return $this->pluginDefinition['group']; + } + + /** + * {@inheritdoc} + */ + public function getOptions() { + return $this->pluginDefinition['options']; + } + + /** + * {@inheritdoc} + */ + public function getWeight() { + return $this->pluginDefinition['weight']; + } + +} diff --git a/core/lib/Drupal/Core/Menu/ContextualLinkInterface.php b/core/lib/Drupal/Core/Menu/ContextualLinkInterface.php new file mode 100644 index 0000000..0943cda --- /dev/null +++ b/core/lib/Drupal/Core/Menu/ContextualLinkInterface.php @@ -0,0 +1,57 @@ + '', + // (required) The contextual links group. + 'group' => '', + // The static title text for the link. + 'title' => '', + // The default link options. + 'options' => array(), + // The weight of the link. + 'weight' => NULL, + // Default class for contextual link implementations. + 'class' => '\Drupal\Core\Menu\ContextualLinkDefault', + // The plugin id. Set by the plugin system based on the top-level YAML key. + 'id' => '', + ); + + /** + * A controller resolver object. + * + * @var \Symfony\Component\HttpKernel\Controller\ControllerResolverInterface + */ + protected $controllerResolver; + + /** + * Constructs a new ContextualLinksManager instance. + * + * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver + * The controller resolver. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler. + * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend + * The cache backend. + * @param \Drupal\Core\Language\LanguageManager $language_manager + * The language manager. + */ + public function __construct(ControllerResolverInterface $controller_resolver, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManager $language_manager) { + $this->discovery = new YamlDiscovery('contextual_links', $module_handler->getModuleDirectories()); + $this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery); + $this->factory = new ContainerFactory($this); + + $this->controllerResolver = $controller_resolver; + $this->alterInfo($module_handler, 'contextual_links'); + $this->setCacheBackend($cache_backend, $language_manager, 'contextual_links_plugins'); + } + + /** + * {@inheritdoc} + */ + public function processDefinition(&$definition, $plugin_id) { + parent::processDefinition($definition, $plugin_id); + // If there is no route name, this is a broken definition. + if (empty($definition['route_name'])) { + throw new PluginException(sprintf('Plugin (%s) definition must include "route_name"', $plugin_id)); + } + // If there is no group name, this is a broken definition. + if (empty($definition['group'])) { + throw new PluginException(sprintf('Plugin (%s) definition must include "group"', $plugin_id)); + } + } + + /** + * Gets the contextual link plugins by contextual link group. + * + * @param string $group_name + * The group name. + * + * @return array + * A list of contextual links plugin definitions, which should be shown. + */ + public function getContextualLinkPluginsByGroup($group_name) { + if ($cache = $this->cacheBackend->get($this->cacheKey . ':' . $group_name)) { + $contextual_links = $cache->data; + } + else { + $contextual_links = array(); + foreach ($this->getDefinitions() as $plugin_id => $plugin_definition) { + if ($plugin_definition['group'] == $group_name) { + $contextual_links[$plugin_id] = $plugin_definition; + } + } + $this->cacheBackend->set($this->cacheKey . ':' . $group_name, $contextual_links); + } + return $contextual_links; + } + + /** + * Gets the contextual links prepared as expected by theme_links. + * + * @param string $group_name + * The group name. + * @param array $route_parameters + * The incoming route parameters. + * + * @return array + * A list of links as array, keyed by the plugin ID. Each entry is an + * associative array with the following keys: + * - route_name: The route name to link to. + * - route_parameters: The route parameters for the contextual link. + * - title: The title of the contextual link. + * - weight: The weight of the contextual link. + * - localized_options: The options of the link, which will be passed + * to the link generator. + * + */ + public function getContextualLinksArrayByGroup($group_name, array $route_parameters) { + $links = array(); + foreach ($this->getContextualLinkPluginsByGroup($group_name) as $plugin_id => $plugin_definition) { + /** @var $plugin \Drupal\Core\Menu\ContextualLinkInterface */ + $plugin = $this->createInstance($plugin_id); + $links[$plugin_id] = array( + 'route_name' => $plugin->getRouteName(), + 'route_parameters' => $route_parameters, + 'title' => $plugin->getTitle(), + 'weight' => $plugin->getWeight(), + 'localized_options' => $plugin->getOptions(), + ); + } + return $links; + } + +} diff --git a/core/modules/block/block.contextual_links.yml b/core/modules/block/block.contextual_links.yml new file mode 100644 index 0000000..7a4173a --- /dev/null +++ b/core/modules/block/block.contextual_links.yml @@ -0,0 +1,9 @@ +block.block_configure: + title: 'Configure block' + route_name: 'block.admin_edit' + group: 'block' + +block.block_delete: + title: 'Delete block' + route_name: 'block.admin_block_delete' + group: 'block' diff --git a/core/modules/block/block.module b/core/modules/block/block.module index 5fd66cb..839187f 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -115,7 +115,7 @@ function block_menu() { $items['admin/structure/block/manage/%block/configure'] = array( 'title' => 'Configure block', 'type' => MENU_DEFAULT_LOCAL_TASK, - 'context' => MENU_CONTEXT_INLINE, + 'context' => MENU_CONTEXT_NONE, ); $items['admin/structure/block/manage/%block/delete'] = array( 'title' => 'Delete block', @@ -313,7 +313,7 @@ function _block_get_renderable_region($list = array()) { // to perform contextual actions on the help block, and the links needlessly // draw attention on it. if (isset($build[$key]) && !in_array($block->get('plugin'), array('system_help_block', 'system_main_block'))) { - $build[$key]['#contextual_links']['block'] = array('admin/structure/block/manage', array($key)); + $build[$key]['#contextual_links']['block'] = array('block', array('block' => $key)); // If there are any nested contextual links, move them to the top level. if (isset($build[$key]['content']['#contextual_links'])) { diff --git a/core/modules/block/custom_block/custom_block.contextual_links.yml b/core/modules/block/custom_block/custom_block.contextual_links.yml new file mode 100644 index 0000000..acd117f --- /dev/null +++ b/core/modules/block/custom_block/custom_block.contextual_links.yml @@ -0,0 +1,10 @@ +custom_block.block_edit: + title: 'Edit' + group: custom_block + route_name: 'custom_block.edit' + +custom_block.block_delete: + title: 'Delete' + group: custom_block + route_name: 'custom_block.delete' + weight: 1 diff --git a/core/modules/block/custom_block/custom_block.module b/core/modules/block/custom_block/custom_block.module index 20460ce..130ecec 100644 --- a/core/modules/block/custom_block/custom_block.module +++ b/core/modules/block/custom_block/custom_block.module @@ -122,14 +122,11 @@ function custom_block_menu() { 'title' => 'Edit', 'weight' => 0, 'type' => MENU_DEFAULT_LOCAL_TASK, - 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE, ); $items['block/%custom_block/delete'] = array( 'title' => 'Delete', 'weight' => 1, 'type' => MENU_LOCAL_TASK, - 'context' => MENU_CONTEXT_INLINE, - 'route_name' => 'custom_block.delete', ); return $items; } diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockRenderController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockRenderController.php index 3457115..c162a64 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockRenderController.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockRenderController.php @@ -23,7 +23,7 @@ protected function alterBuild(array &$build, EntityInterface $entity, EntityDisp parent::alterBuild($build, $entity, $display, $view_mode, $langcode); // Add contextual links for this custom block. if (!empty($entity->id->value) && $view_mode == 'full') { - $build['#contextual_links']['custom_block'] = array('block', array($entity->id())); + $build['#contextual_links']['custom_block'] = array('custom_block', array('custom_block' => $entity->id())); } } diff --git a/core/modules/block/lib/Drupal/block/Tests/Views/DisplayBlockTest.php b/core/modules/block/lib/Drupal/block/Tests/Views/DisplayBlockTest.php index 1e07586..ad8889e 100644 --- a/core/modules/block/lib/Drupal/block/Tests/Views/DisplayBlockTest.php +++ b/core/modules/block/lib/Drupal/block/Tests/Views/DisplayBlockTest.php @@ -230,7 +230,7 @@ public function testBlockContextualLinks() { $block = $this->drupalPlaceBlock('views_block:test_view_block-block_1'); $this->drupalGet('test-page'); - $id = 'block:admin/structure/block/manage:' . $block->id() . ':|views_ui:admin/structure/views/view:test_view_block:location=block&name=test_view_block&display_id=block_1'; + $id = 'block:admin/structure/block/manage:block=' . $block->id() . ':|views_ui:admin/structure/views/view:0=test_view_block:location=block&name=test_view_block&display_id=block_1'; // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:assertContextualLinkPlaceHolder() $this->assertRaw('
', format_string('Contextual link placeholder with id @id exists.', array('@id' => $id))); diff --git a/core/modules/contextual/contextual.module b/core/modules/contextual/contextual.module index 529fa5c..62a33de 100644 --- a/core/modules/contextual/contextual.module +++ b/core/modules/contextual/contextual.module @@ -256,6 +256,17 @@ function contextual_pre_render_placeholder($element) { function contextual_pre_render_links($element) { // Retrieve contextual menu links. $items = array(); + + $contextual_links_manager = \Drupal::service('plugin.manager.menu.contextual_link'); + foreach ($element['#contextual_links'] as $module => $args) { + $res = $contextual_links_manager->getContextualLinksArrayByGroup($args[0], $args[1]); + if (!empty($res)) { + unset($element['#contextual_links'][$module]); + } + $items += $res; + } + + // @todo Remove once all contextual links are converted. foreach ($element['#contextual_links'] as $module => $args) { $items += menu_contextual_links($module, $args[0], $args[1]); } @@ -266,9 +277,10 @@ function contextual_pre_render_links($element) { $class = drupal_html_class($class); $links[$class] = array( 'title' => $item['title'], - 'href' => $item['href'], + 'href' => isset($item['href']) ? $item['href'] : NULL, + 'route_name' => isset($item['route_name']) ? $item['route_name'] : '', + 'route_parameters' => isset($item['route_parameters']) ? $item['route_parameters'] : array(), ); - // @todo theme_links() should *really* use the same parameters as l(). $item['localized_options'] += array('query' => array()); $item['localized_options']['query'] += drupal_get_destination(); $links[$class] += $item['localized_options']; @@ -302,15 +314,14 @@ function contextual_contextual_links_view_alter(&$element, $items) { * Serializes #contextual_links property value array to a string. * * Examples: - * - node:node:1: - * - views_ui:admin/structure/views/view:frontpage:location=page&view_name=frontpage&view_display_id=page_1 - * - menu:admin/structure/menu/manage:tools:|block:admin/structure/block/manage:bartik.tools: + * - node:node:node=1: + * - views_ui:admin/structure/views/view:view=frontpage:location=page&view_name=frontpage&view_display_id=page_1 + * - menu:admin/structure/menu/manage:menu=tools:|block:admin/structure/block/manage:block=bartik.tools: * * So, expressed in a pattern: * ::: * - * The (dynamic) path args are joined with slashes. The options are encoded as a - * query string. + * The path args and options are encoded as query strings. * * @param array $contextual_links * The $element['#contextual_links'] value for some render element. @@ -320,18 +331,14 @@ function contextual_contextual_links_view_alter(&$element, $items) { * use in a data- attribute. */ function _contextual_links_to_id($contextual_links) { - $id = ''; + $ids = array(); foreach ($contextual_links as $module => $args) { $parent_path = $args[0]; - $path_args = implode('/', $args[1]); + $path_args = drupal_http_build_query($args[1]); $metadata = drupal_http_build_query((isset($args[2])) ? $args[2] : array()); - - if (drupal_strlen($id) > 0) { - $id .= '|'; - } - $id .= $module . ':' . $parent_path . ':' . $path_args . ':' . $metadata; + $ids[] = "{$module}:{$parent_path}:{$path_args}:{$metadata}"; } - return $id; + return implode('|', $ids); } /** @@ -349,8 +356,9 @@ function _contextual_id_to_links($id) { $contextual_links = array(); $contexts = explode('|', $id); foreach ($contexts as $context) { - list($module, $parent_path, $path_args, $metadata_raw) = explode(':', $context); - $path_args = explode('/', $path_args); + list($module, $parent_path, $args_raw, $metadata_raw) = explode(':', $context); + $path_args = array(); + parse_str($args_raw, $path_args); $metadata = array(); parse_str($metadata_raw, $metadata); $contextual_links[$module] = array($parent_path, $path_args, $metadata); diff --git a/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualDynamicContextTest.php b/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualDynamicContextTest.php index 638ce15..955c0e1 100644 --- a/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualDynamicContextTest.php +++ b/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualDynamicContextTest.php @@ -60,10 +60,10 @@ function testDifferentPermissions() { // Now, on the front page, all article nodes should have contextual links // placeholders, as should the view that contains them. $ids = array( - 'node:node:' . $node1->id() . ':', - 'node:node:' . $node2->id() . ':', - 'node:node:' . $node3->id() . ':', - 'views_ui:admin/structure/views/view:frontpage:location=page&name=frontpage&display_id=page_1', + 'node:node:node=' . $node1->id() . ':', + 'node:node:node=' . $node2->id() . ':', + 'node:node:node=' . $node3->id() . ':', + 'views_ui:admin/structure/views/view:0=frontpage:location=page&name=frontpage&display_id=page_1', ); // Editor user: can access contextual links and can edit articles. diff --git a/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualUnitTest.php b/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualUnitTest.php index d2018ac..2c93f4c 100644 --- a/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualUnitTest.php +++ b/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualUnitTest.php @@ -33,11 +33,13 @@ function _contextual_links_id_testcases() { 'links' => array( 'node' => array( 'node', - array('14031991'), + array( + 'node' => '14031991', + ), array() ), ), - 'id' => 'node:node:14031991:', + 'id' => 'node:node:node=14031991:', ); // Test branch conditions: @@ -48,11 +50,15 @@ function _contextual_links_id_testcases() { 'links' => array( 'foo' => array( 'baz/in/ga', - array('bar', 'baz', 'qux'), + array( + 'bar', + 'key' => 'baz', + 'qux', + ), array() ), ), - 'id' => 'foo:baz/in/ga:bar/baz/qux:', + 'id' => 'foo:baz/in/ga:0=bar&key=baz&1=qux:', ); // Test branch conditions: @@ -70,7 +76,7 @@ function _contextual_links_id_testcases() { ) ), ), - 'id' => 'views_ui:admin/structure/views/view:frontpage:location=page&display=page_1', + 'id' => 'views_ui:admin/structure/views/view:0=frontpage:location=page&display=page_1', ); // Test branch conditions: @@ -80,12 +86,18 @@ function _contextual_links_id_testcases() { 'links' => array( 'node' => array( 'node', - array('14031991'), + array( + 'node' => '14031991', + ), array() ), 'foo' => array( 'baz/in/ga', - array('bar', 'baz', 'qux'), + array( + 'bar', + 'key' => 'baz', + 'qux', + ), array() ), 'edge' => array( @@ -94,7 +106,7 @@ function _contextual_links_id_testcases() { array() ), ), - 'id' => 'node:node:14031991:|foo:baz/in/ga:bar/baz/qux:|edge:edge:20011988:', + 'id' => 'node:node:node=14031991:|foo:baz/in/ga:0=bar&key=baz&1=qux:|edge:edge:0=20011988:', ); return $tests; diff --git a/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php b/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php index f7e0690..a45458e 100644 --- a/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php +++ b/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php @@ -408,7 +408,7 @@ public function testBlockContextualLinks() { $block = $this->drupalPlaceBlock('system_menu_block:tools', array('label' => 'Tools', 'module' => 'system')); $this->drupalGet('test-page'); - $id = 'block:admin/structure/block/manage:' . $block->id() . ':|menu:admin/structure/menu/manage:tools:'; + $id = 'block:admin/structure/block/manage:block=' . $block->id() . ':|menu:admin/structure/menu/manage:menu=tools:'; // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:assertContextualLinkPlaceHolder() $this->assertRaw('
', format_string('Contextual link placeholder with id @id exists.', array('@id' => $id))); diff --git a/core/modules/menu/menu.contextual_links.yml b/core/modules/menu/menu.contextual_links.yml new file mode 100644 index 0000000..2aa49f3 --- /dev/null +++ b/core/modules/menu/menu.contextual_links.yml @@ -0,0 +1,4 @@ +menu.menu_edit: + title: 'Edit menu' + route_name: 'menu.menu_edit' + group: menu diff --git a/core/modules/menu/menu.module b/core/modules/menu/menu.module index 08355a1..fcb5499 100644 --- a/core/modules/menu/menu.module +++ b/core/modules/menu/menu.module @@ -99,7 +99,6 @@ function menu_menu() { $items['admin/structure/menu/manage/%menu/edit'] = array( 'title' => 'Edit menu', 'type' => MENU_DEFAULT_LOCAL_TASK, - 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE, ); $items['admin/structure/menu/manage/%menu/delete'] = array( 'title' => 'Delete menu', @@ -391,7 +390,9 @@ function menu_block_view_system_menu_block_alter(array &$build, BlockPluginInter list(, $menu_name) = explode(':', $block->getPluginId()); if (isset($menus[$menu_name]) && isset($build['content'])) { foreach (element_children($build['content']) as $key) { - $build['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($build['content'][$key]['#original_link']['menu_name'])); + $build['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array( + 'menu' => $build['content'][$key]['#original_link']['menu_name'], + )); } } } diff --git a/core/modules/node/lib/Drupal/node/NodeRenderController.php b/core/modules/node/lib/Drupal/node/NodeRenderController.php index 6da0b29..75f85a8 100644 --- a/core/modules/node/lib/Drupal/node/NodeRenderController.php +++ b/core/modules/node/lib/Drupal/node/NodeRenderController.php @@ -83,7 +83,7 @@ public function buildContent(array $entities, array $displays, $view_mode, $lang protected function alterBuild(array &$build, EntityInterface $entity, EntityDisplay $display, $view_mode, $langcode = NULL) { parent::alterBuild($build, $entity, $display, $view_mode, $langcode); if ($entity->id()) { - $build['#contextual_links']['node'] = array('node', array($entity->id())); + $build['#contextual_links']['node'] = array('node', array('node' => $entity->id())); } } diff --git a/core/modules/node/lib/Drupal/node/Tests/Views/NodeContextualLinksTest.php b/core/modules/node/lib/Drupal/node/Tests/Views/NodeContextualLinksTest.php index cf24cba..f2f77e9 100644 --- a/core/modules/node/lib/Drupal/node/Tests/Views/NodeContextualLinksTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/Views/NodeContextualLinksTest.php @@ -47,10 +47,10 @@ public function testNodeContextualLinks() { $user = $this->drupalCreateUser(array('administer nodes', 'access contextual links')); $this->drupalLogin($user); - $response = $this->renderContextualLinks(array('node:node:1:'), 'node'); + $response = $this->renderContextualLinks(array('node:node:node=1:'), 'node'); $this->assertResponse(200); $json = Json::decode($response); - $this->drupalSetContent($json['node:node:1:']); + $this->drupalSetContent($json['node:node:node=1:']); $this->assertLinkByHref('node/1/contextual-links', 0, 'The contextual link to the view was found.'); $this->assertLink('Test contextual link', 0, 'The contextual link to the view was found.'); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/TermRenderController.php b/core/modules/taxonomy/lib/Drupal/taxonomy/TermRenderController.php index cf319d0..6b0fd2f 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/TermRenderController.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/TermRenderController.php @@ -54,7 +54,9 @@ protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langco protected function alterBuild(array &$build, EntityInterface $entity, EntityDisplay $display, $view_mode, $langcode = NULL) { parent::alterBuild($build, $entity, $display, $view_mode, $langcode); $build['#attached']['css'][] = drupal_get_path('module', 'taxonomy') . '/css/taxonomy.module.css'; - $build['#contextual_links']['taxonomy'] = array('taxonomy/term', array($entity->id())); + $build['#contextual_links']['taxonomy'] = array('taxonomy/term', array( + 'taxonomy_term' => $entity->id(), + )); } } diff --git a/core/modules/views_ui/lib/Drupal/views_ui/Tests/DisplayTest.php b/core/modules/views_ui/lib/Drupal/views_ui/Tests/DisplayTest.php index 4f700cf..58d2676 100644 --- a/core/modules/views_ui/lib/Drupal/views_ui/Tests/DisplayTest.php +++ b/core/modules/views_ui/lib/Drupal/views_ui/Tests/DisplayTest.php @@ -288,7 +288,7 @@ public function testPageContextualLinks() { $view->enable()->save(); $this->drupalGet('test-display'); - $id = 'views_ui:admin/structure/views/view:test_display:location=page&name=test_display&display_id=page_1'; + $id = 'views_ui:admin/structure/views/view:0=test_display:location=page&name=test_display&display_id=page_1'; // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:assertContextualLinkPlaceHolder() $this->assertRaw('
', format_string('Contextual link placeholder with id @id exists.', array('@id' => $id))); diff --git a/core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php b/core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php new file mode 100644 index 0000000..70cd370 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php @@ -0,0 +1,269 @@ + 'Contextual links manager.', + 'description' => 'Tests the contextual links manager.', + 'group' => 'Menu', + ); + } + + protected function setUp() { + $this->contextualLinkManager = $this + ->getMockBuilder('Drupal\Core\Menu\ContextualLinkManager') + ->disableOriginalConstructor() + ->setMethods(NULL) + ->getMock(); + + $this->controllerResolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface'); + $this->pluginDiscovery = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface'); + $this->factory = $this->getMock('Drupal\Component\Plugin\Factory\FactoryInterface'); + $this->cacheBackend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); + + $property = new \ReflectionProperty('Drupal\Core\Menu\ContextualLinkManager', 'controllerResolver'); + $property->setAccessible(TRUE); + $property->setValue($this->contextualLinkManager, $this->controllerResolver); + + $property = new \ReflectionProperty('Drupal\Core\Menu\ContextualLinkManager', 'discovery'); + $property->setAccessible(TRUE); + $property->setValue($this->contextualLinkManager, $this->pluginDiscovery); + + $property = new \ReflectionProperty('Drupal\Core\Menu\ContextualLinkManager', 'factory'); + $property->setAccessible(TRUE); + $property->setValue($this->contextualLinkManager, $this->factory); + + $language_manager = $this->getMockBuilder('Drupal\Core\Language\LanguageManager') + ->disableOriginalConstructor() + ->getMock(); + $language_manager->expects($this->any()) + ->method('getLanguage') + ->will($this->returnValue(new Language(array('id' => 'en')))); + + $this->contextualLinkManager->setCacheBackend($this->cacheBackend, $language_manager, 'contextual_links_plugins'); + } + + /** + * Tests the getContextualLinkPluginsByGroup method. + * + * @see \Drupal\Core\Menu\ContextualLinkManager::getContextualLinkPluginsByGroup() + */ + public function testGetContextualLinkPluginsByGroup() { + $definitions = array( + 'test_plugin1' => array( + 'id' => 'test_plugin1', + 'class' => '\Drupal\Core\Menu\ContextualLinkDefault', + 'group' => 'group1', + 'route_name' => 'test_route', + ), + 'test_plugin2' => array( + 'id' => 'test_plugin2', + 'class' => '\Drupal\Core\Menu\ContextualLinkDefault', + 'group' => 'group1', + 'route_name' => 'test_route2', + ), + 'test_plugin3' => array( + 'id' => 'test_plugin3', + 'class' => '\Drupal\Core\Menu\ContextualLinkDefault', + 'group' => 'group2', + 'route_name' => 'test_router3', + ), + ); + $this->pluginDiscovery->expects($this->once()) + ->method('getDefinitions') + ->will($this->returnValue($definitions)); + + // Test with a non existing group. + $result = $this->contextualLinkManager->getContextualLinkPluginsByGroup('group_non_existing'); + $this->assertEmpty($result); + + $result = $this->contextualLinkManager->getContextualLinkPluginsByGroup('group1'); + $this->assertEquals(array('test_plugin1', 'test_plugin2'), array_keys($result)); + + $result = $this->contextualLinkManager->getContextualLinkPluginsByGroup('group2'); + $this->assertEquals(array('test_plugin3'), array_keys($result)); + } + + /** + * Tests the getContextualLinkPluginsByGroup with a prefilled cache. + */ + public function testGetContextualLinkPluginsByGroupWithCache() { + $definitions = array( + 'test_plugin1' => array( + 'id' => 'test_plugin1', + 'class' => '\Drupal\Core\Menu\ContextualLinkDefault', + 'group' => 'group1', + 'route_name' => 'test_route', + ), + 'test_plugin2' => array( + 'id' => 'test_plugin2', + 'class' => '\Drupal\Core\Menu\ContextualLinkDefault', + 'group' => 'group1', + 'route_name' => 'test_route2', + ), + ); + + $this->cacheBackend->expects($this->once()) + ->method('get') + ->with('contextual_links_plugins:en:group1') + ->will($this->returnValue((object) array('data' => $definitions))); + + $result = $this->contextualLinkManager->getContextualLinkPluginsByGroup('group1'); + $this->assertEquals($definitions, $result); + } + + /** + * Tests processDefinition() by passing a plugin definition without a route. + * + * @see \Drupal\Core\Menu\ContextualLinkManager::processDefinition() + * + * @expectedException \Drupal\Component\Plugin\Exception\PluginException + */ + public function testProcessDefinitionWithoutRoute() { + $definition = array( + 'class' => '\Drupal\Core\Menu\ContextualLinkDefault', + 'group' => 'example', + 'id' => 'test_plugin', + ); + $this->contextualLinkManager->processDefinition($definition, 'test_plugin'); + } + + /** + * Tests processDefinition() by passing a plugin definition without a group. + * + * @see \Drupal\Core\Menu\ContextualLinkManager::processDefinition() + * + * @expectedException \Drupal\Component\Plugin\Exception\PluginException + */ + public function testProcessDefinitionWithoutGroup() { + $definition = array( + 'class' => '\Drupal\Core\Menu\ContextualLinkDefault', + 'route_name' => 'example', + 'id' => 'test_plugin', + ); + $this->contextualLinkManager->processDefinition($definition, 'test_plugin'); + } + + + /** + * Tests the getContextualLinksArrayByGroup method. + * + * @see \Drupal\Core\Menu\ContextualLinkManager::getContextualLinksArrayByGroup() + */ + public function testGetContextualLinksArrayByGroup() { + $definitions = array( + 'test_plugin1' => array( + 'id' => 'test_plugin1', + 'class' => '\Drupal\Core\Menu\ContextualLinkDefault', + 'title' => 'Plugin 1', + 'weight' => 0, + 'group' => 'group1', + 'route_name' => 'test_route', + 'options' => array(), + ), + 'test_plugin2' => array( + 'id' => 'test_plugin2', + 'class' => '\Drupal\Core\Menu\ContextualLinkDefault', + 'title' => 'Plugin 2', + 'weight' => 2, + 'group' => 'group1', + 'route_name' => 'test_route2', + 'options' => array('key' => 'value'), + ), + 'test_plugin3' => array( + 'id' => 'test_plugin3', + 'class' => '\Drupal\Core\Menu\ContextualLinkDefault', + 'title' => 'Plugin 3', + 'weight' => 5, + 'group' => 'group2', + 'route_name' => 'test_router3', + 'options' => array(), + ), + ); + + $this->pluginDiscovery->expects($this->once()) + ->method('getDefinitions') + ->will($this->returnValue($definitions)); + + $map = array(); + foreach ($definitions as $plugin_id => $definition) { + $plugin = $this->getMock('Drupal\Core\Menu\ContextualLinkInterface'); + $plugin->expects($this->any()) + ->method('getRouteName') + ->will($this->returnValue($definition['route_name'])); + $plugin->expects($this->any()) + ->method('getTitle') + ->will($this->returnValue($definition['title'])); + $plugin->expects($this->any()) + ->method('getWeight') + ->will($this->returnValue($definition['weight'])); + $plugin->expects($this->any()) + ->method('getOptions') + ->will($this->returnValue($definition['options'])); + $map[] = array($plugin_id, array(), $plugin); + } + $this->factory->expects($this->any()) + ->method('createInstance') + ->will($this->returnValueMap($map)); + + $result = $this->contextualLinkManager->getContextualLinksArrayByGroup('group1', array('key' => 'value')); + foreach (array('test_plugin1', 'test_plugin2') as $plugin_id) { + $definition = $definitions[$plugin_id]; + $this->assertEquals($definition['weight'], $result[$plugin_id]['weight']); + $this->assertEquals($definition['title'], $result[$plugin_id]['title']); + $this->assertEquals($definition['route_name'], $result[$plugin_id]['route_name']); + } + } + +}