core/modules/contextual/contextual.js | 65 +++++++-- core/modules/contextual/contextual.module | 109 +++++++++++++-- core/modules/contextual/contextual.routing.yml | 6 + core/modules/contextual/contextual.toolbar.js | 55 +++++--- .../lib/Drupal/contextual/ContextualController.php | 50 +++++++ .../Plugin/views/field/ContextualLinks.php | 27 ++-- .../Tests/ContextualDynamicContextTest.php | 139 ++++++++++++++++++-- .../Drupal/contextual/Tests/ContextualUnitTest.php | 122 +++++++++++++++++ core/modules/edit/edit.module | 18 +-- core/modules/edit/js/edit.js | 10 +- core/modules/views/js/views-contextual.js | 9 +- .../lib/Drupal/views/Tests/UI/DisplayTest.php | 21 ++- core/modules/views/views.module | 34 +++-- core/modules/views/views_ui/views_ui.module | 4 +- 14 files changed, 566 insertions(+), 103 deletions(-) diff --git a/core/modules/contextual/contextual.js b/core/modules/contextual/contextual.js index aa80b31..c06128c 100644 --- a/core/modules/contextual/contextual.js +++ b/core/modules/contextual/contextual.js @@ -3,7 +3,7 @@ * Attaches behaviors for the Contextual module. */ -(function ($, Drupal) { +(function ($, Drupal, drupalSettings) { "use strict"; @@ -11,19 +11,64 @@ var contextuals = []; /** * Attaches outline behavior for regions associated with contextual links. + * + * Events + * Contextual triggers an event that can be used by other scripts. + * - drupalContextualLinkAdded: Triggered when a contextual link is added. */ Drupal.behaviors.contextual = { attach: function (context) { - $('ul.contextual-links', context).once('contextual', function () { - var $this = $(this); - var contextual = new Drupal.contextual($this, $this.closest('.contextual-region')); - contextuals.push(contextual); - $this.data('drupal-contextual', contextual); + $('body').once('contextual', function () { + $(document) + // Bind to edit mode changes. + .on('drupalEditModeChanged.contextual', toggleEditMode) + // Track all contextual links on the page. + .on('drupalContextualLinkAdded.contextual', function(event, data) { + contextuals.push(data.contextual); + }); }); - // Bind to edit mode changes. - $('body').once('contextual', function () { - $(document).on('drupalEditModeChanged.contextual', toggleEditMode); + this._render(context); + }, + + /** + * Find contextual links, render them server-side and move them in the DOM. + */ + _render: function (context) { + var $context = $(context); + + var $contextualLinks = $context + .find('[data-contextual-id]') + .once('contextual-render'); + if ($contextualLinks.length === 0) { + return; + } + + var ids = []; + $contextualLinks.each(function () { + ids.push($(this).attr('data-contextual-id')); + }); + $.ajax({ + url: Drupal.url('contextual/render') + '?destination=' + Drupal.encodePath(drupalSettings.currentPath), + type: 'POST', + data: { 'ids[]' : ids }, + dataType: 'json', + success: function(results) { + for (var id in results) { + if (results.hasOwnProperty(id)) { + var $contextual = $context + // Find the location for the current rendered contextual link. + .find('[data-contextual-id="' + id + '"]') + // Move it into the DOM. + .html(results[id]); + // Create a Drupal.contextual object and notify listeners of a new + // contextual link. + $(document).trigger('drupalContextualLinkAdded', { + contextual: new Drupal.contextual($contextual, $contextual.closest('.contextual-region')) + }); + } + } + } }); } }; @@ -198,4 +243,4 @@ Drupal.theme.contextualTrigger = function () { return ''; }; -})(jQuery, Drupal); +})(jQuery, Drupal, drupalSettings); diff --git a/core/modules/contextual/contextual.module b/core/modules/contextual/contextual.module index 76e39ba..24c18db 100644 --- a/core/modules/contextual/contextual.module +++ b/core/modules/contextual/contextual.module @@ -6,6 +6,18 @@ */ /** + * Implements hook_custom_theme(). + * + * @todo Add an event subscriber to the Ajax system to automatically set the + * base page theme for all Ajax requests, and then remove this one off. + */ +function contextual_custom_theme() { + if (substr(current_path(), 0, 11) === 'contextual/') { + return ajax_base_page_theme(); + } +} + +/** * Implements hook_toolbar(). */ function contextual_toolbar() { @@ -24,14 +36,13 @@ function contextual_toolbar() { 'role' => 'button', 'aria-pressed' => 'false', ), - // @todo remove this once http://drupal.org/node/1908906 lands. - '#options' => array('attributes' => array()), ), '#wrapper_attributes' => array( 'class' => array('element-hidden', 'contextual-toolbar-tab'), ), '#attached' => array( 'library' => array( + array('contextual', 'drupal.contextual-links'), array('contextual', 'drupal.contextual-toolbar'), ), ), @@ -91,6 +102,7 @@ function contextual_library_info() { 'dependencies' => array( array('system', 'jquery'), array('system', 'drupal'), + array('system', 'drupalSettings'), array('system', 'jquery.once'), ), ); @@ -139,11 +151,6 @@ function contextual_element_info() { * @see contextual_pre_render_links() */ function contextual_preprocess(&$variables, $hook) { - // Nothing to do here if the user is not permitted to access contextual links. - if (!user_access('access contextual links')) { - return; - } - $hooks = theme_get_registry(FALSE); // Determine the primary theme function argument. @@ -159,14 +166,18 @@ function contextual_preprocess(&$variables, $hook) { } if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) { - // Initialize the template variable as a renderable array. - $variables['title_suffix']['contextual_links'] = array( - '#type' => 'contextual_links', - '#contextual_links' => $element['#contextual_links'], - '#element' => $element, - ); // Mark this element as potentially having contextual links attached to it. $variables['attributes']['class'][] = 'contextual-region'; + + // Render an empty (and thus invisible) div as a title suffix, with a data- + // attribute that contains an identifier, which allows contextual.module's + // JavaScript to determine which contextual links should be rendered. + // This div with data- attribute is added unconditionally, and thus does not + // break the render cache. + // @see _contextual_links_to_id() + $id = _contextual_links_to_id($element['#contextual_links']); + $variables['title_suffix']['contextual_links']['#id'] = $id; + $variables['title_suffix']['contextual_links']['#markup'] = '
'; } } @@ -224,3 +235,75 @@ function contextual_pre_render_links($element) { return $element; } +/** + * Implements hook_contextual_links_view_alter(). + * + * @see \Drupal\contextual\Plugin\views\field\ContextualLinks::render() + */ +function contextual_contextual_links_view_alter(&$element, $items) { + if (isset($element['#contextual_links']['contextual'])) { + $encoded_links = $element['#contextual_links']['contextual'][2]['contextual-views-field-links']; + $element['#links'] = drupal_json_decode(rawurldecode($encoded_links)); + } +} + +/** + * Serializes #contextual_links property metadata to a "contextual id". + * + * 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: + * + * So, expressed in a pattern: + * ::: + * + * The (dynamic) path args are joined with slashes. The metadata is encoded as a + * query string + * + * @param array $contextual_links + * The $element['#contextual_links'] value for some render element. + * + * @return string + * A contextual id. + */ +function _contextual_links_to_id($contextual_links) { + $id = ''; + foreach ($contextual_links as $module => $args) { + $parent_path = $args[0]; + $path_args = implode('/', $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; + } + return $id; +} + +/** + * Serializes a contextual id back to #contextual_links property metadata. + * + * The inverse operation of _contextual_links_to_id(). + * + * @see _contextual_links_to_id + * + * @param string $id + * A contextual id. + * + * @return array + * The value for a #contextual_links property. + */ +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); + $metadata = drupal_get_query_array($metadata_raw); + $contextual_links[$module] = array($parent_path, $path_args, $metadata); + } + return $contextual_links; +} + diff --git a/core/modules/contextual/contextual.routing.yml b/core/modules/contextual/contextual.routing.yml new file mode 100644 index 0000000..5dd4457 --- /dev/null +++ b/core/modules/contextual/contextual.routing.yml @@ -0,0 +1,6 @@ +contextual_render: + pattern: '/contextual/render' + defaults: + _controller: '\Drupal\contextual\ContextualController::render' + requirements: + _permission: 'access contextual links' diff --git a/core/modules/contextual/contextual.toolbar.js b/core/modules/contextual/contextual.toolbar.js index 45f9757..1ce6a46 100644 --- a/core/modules/contextual/contextual.toolbar.js +++ b/core/modules/contextual/contextual.toolbar.js @@ -17,34 +17,45 @@ Drupal.behaviors.contextualToolbar = { attach: function (context) { $('body').once('contextualToolbar-init', function () { - var $contextuals = $(context).find('.contextual-links'); - var $tab = $('.js .toolbar .bar .contextual-toolbar-tab'); var model = new Drupal.contextualToolbar.models.EditToggleModel({ isViewing: true }); var view = new Drupal.contextualToolbar.views.EditToggleView({ - el: $tab, + el: $('.js .toolbar .bar .contextual-toolbar-tab'), model: model }); - // Update the model based on overlay events. - $(document) - .on('drupalOverlayOpen.contextualToolbar', function () { - model.set('isVisible', false); - }) - .on('drupalOverlayClose.contextualToolbar', function () { - model.set('isVisible', true); - }); + // Update the model based on events to the page. + $(document).on({ + 'drupalOverlayOpen.contextualToolbar': function () { + model.set('overlayIsOpen', true); + }, + 'drupalOverlayClose.contextualToolbar': function () { + model.set('overlayIsOpen', false); + }, + 'drupalContextualLinkAdded.contextualToolbar': function() { + model.set('contextualCount', model.get('contextualCount') + 1); + } + }); - // Update the model to show the edit tab if there's >=1 contextual link. - if ($contextuals.length > 0) { - model.set('isVisible', true); - } + // React when the model is changed. + model + .on('change:overlayIsOpen', function(model, value) { + model.set('isVisible', !value); + }) + .on('change:contextualCount', function(model, value) { + model.set('isVisible', (value > 0 && model.get('overlayIsOpen') === false)); - // Allow other scripts to respond to edit mode changes. - model.on('change:isViewing', function (model, value) { - $(document).trigger('drupalEditModeChanged', { status: !value }); - }); + // If Edit mode is enabled, also apply it to this new contextual link. + if (!model.get('isViewing')) { + $(document).trigger('drupalEditModeChanged', { status: true }); + } + }) + // Allow other scripts to respond to edit mode changes. + .on('change:isViewing', function (model, value) { + $(document).trigger('drupalEditModeChanged', { status: !value }); + }) +; // Checks whether localStorage indicates we should start in edit mode // rather than view mode. @@ -66,7 +77,11 @@ Drupal.contextualToolbar.models.EditToggleModel = Backbone.Model.extend({ // Indicates whether the toggle is currently in "view" or "edit" mode. isViewing: true, // Indicates whether the toggle should be visible or hidden. - isVisible: false + isVisible: false, + // Indicates whether the overlay is open or not. + overlayIsOpen: false, + // Indicates how many contextual links exist on the page. + contextualCount: 0 } }); diff --git a/core/modules/contextual/lib/Drupal/contextual/ContextualController.php b/core/modules/contextual/lib/Drupal/contextual/ContextualController.php new file mode 100644 index 0000000..5c986b6 --- /dev/null +++ b/core/modules/contextual/lib/Drupal/contextual/ContextualController.php @@ -0,0 +1,50 @@ +request->get('ids'); + if (!isset($ids)) { + throw new BadRequestHttpException(t('No contextual ids specified.')); + } + + $rendered = array(); + foreach ($ids as $id) { + $element = array( + '#type' => 'contextual_links', + '#contextual_links' => _contextual_id_to_links($id), + ); + $rendered[$id] = drupal_render($element); + } + + return new JsonResponse($rendered); + } + +} diff --git a/core/modules/contextual/lib/Drupal/contextual/Plugin/views/field/ContextualLinks.php b/core/modules/contextual/lib/Drupal/contextual/Plugin/views/field/ContextualLinks.php index 5ef7df0..ea3f6cd 100644 --- a/core/modules/contextual/lib/Drupal/contextual/Plugin/views/field/ContextualLinks.php +++ b/core/modules/contextual/lib/Drupal/contextual/Plugin/views/field/ContextualLinks.php @@ -67,6 +67,9 @@ function pre_render(&$values) { /** * Render the contextual fields. + * + * @see contextual_preprocess() + * @see contextual_contextual_links_view_alter() */ function render($values) { $links = array(); @@ -95,19 +98,21 @@ function render($values) { } } + // The code belows duplicates part of contextual_preprocess(). This is + // essential because if we use drupal_render() here, then hook_process() is + // not invoked, and consequently contextual_preprocess() isn't either. if (!empty($links)) { - $build = array( - '#prefix' => '
', - '#suffix' => '
', - '#theme' => 'links__contextual', - '#links' => $links, - '#attributes' => array('class' => array('contextual-links')), - '#attached' => array( - 'library' => array(array('contextual', 'contextual-links')), - ), - '#access' => user_access('access contextual links'), + $contextual_links = array( + 'contextual' => array( + '', + array(), + array( + 'contextual-views-field-links' => drupal_encode_path(drupal_json_encode($links)), + ) + ) ); - return drupal_render($build); + $id = _contextual_links_to_id($contextual_links); + return '
'; } else { return ''; diff --git a/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualDynamicContextTest.php b/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualDynamicContextTest.php index f84e9f4..207dfef 100644 --- a/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualDynamicContextTest.php +++ b/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualDynamicContextTest.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\contextual\Tests\ContextualDynamicContextTest. + * Contains \Drupal\contextual\Tests\ContextualDynamicContextTest. */ namespace Drupal\contextual\Tests; @@ -19,7 +19,7 @@ class ContextualDynamicContextTest extends WebTestBase { * * @var array */ - public static $modules = array('contextual', 'node', 'views'); + public static $modules = array('contextual', 'node', 'views', 'views_ui'); public static function getInfo() { return array( @@ -31,16 +31,24 @@ public static function getInfo() { function setUp() { parent::setUp(); + $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page')); $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); - $web_user = $this->drupalCreateUser(array('access content', 'access contextual links', 'edit any article content')); - $this->drupalLogin($web_user); + + $this->editor_user = $this->drupalCreateUser(array('access content', 'access contextual links', 'edit any article content')); + $this->authenticated_user = $this->drupalCreateUser(array('access content', 'access contextual links')); + $this->anonymous_user = $this->drupalCreateUser(array('access content')); } /** - * Tests contextual links on node lists with different permissions. + * Tests contextual links with different permissions. + * + * Ensures that contextual link placeholders always exist, even if the user is + * not allowed to use contextual links. */ - function testNodeLinks() { + function testDifferentPermissions() { + $this->drupalLogin($this->editor_user); + // Create three nodes in the following order: // - An article, which should be user-editable. // - A page, which should not be user-editable. @@ -49,11 +57,120 @@ function testNodeLinks() { $node2 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 1)); $node3 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1)); - // Now, on the front page, all article nodes should have contextual edit - // links. The page node in between should not. + // 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->nid . ':', + 'node:node:' . $node2->nid . ':', + 'node:node:' . $node3->nid . ':', + 'views_ui:admin/structure/views/view:frontpage:location=page&name=frontpage&display_id=page_1', + ); + + // Editor user: can access contextual links and can edit articles. + $this->drupalGet('node'); + for ($i = 0; $i < count($ids); $i++) { + $this->assertContextualLinkPlaceHolder($ids[$i]); + } + $this->renderContextualLinks(array(), 'node'); + $this->assertResponse(400); + $this->assertRaw('No contextual ids specified.'); + $response = $this->renderContextualLinks($ids, 'node'); + $this->assertResponse(200); + $json = drupal_json_decode($response); + $this->assertIdentical($json[$ids[0]], ''); + $this->assertIdentical($json[$ids[1]], NULL); + $this->assertIdentical($json[$ids[2]], ''); + $this->assertIdentical($json[$ids[3]], NULL); + + // Authenticated user: can access contextual links, cannot edit articles. + $this->drupalLogin($this->authenticated_user); $this->drupalGet('node'); - $this->assertRaw('node/' . $node1->nid . '/edit', 'Edit link for oldest article node showing.'); - $this->assertNoRaw('node/' . $node2->nid . '/edit', 'No edit link for page nodes.'); - $this->assertRaw('node/' . $node3->nid . '/edit', 'Edit link for most recent article node showing.'); + for ($i = 0; $i < count($ids); $i++) { + $this->assertContextualLinkPlaceHolder($ids[$i]); + } + $this->renderContextualLinks(array(), 'node'); + $this->assertResponse(400); + $this->assertRaw('No contextual ids specified.'); + $response = $this->renderContextualLinks($ids, 'node'); + $this->assertResponse(200); + $json = drupal_json_decode($response); + $this->assertIdentical($json[$ids[0]], NULL); + $this->assertIdentical($json[$ids[1]], NULL); + $this->assertIdentical($json[$ids[2]], NULL); + $this->assertIdentical($json[$ids[3]], NULL); + + // Anonymous user: cannot access contextual links. + $this->drupalLogin($this->anonymous_user); + $this->drupalGet('node'); + for ($i = 0; $i < count($ids); $i++) { + $this->assertContextualLinkPlaceHolder($ids[$i]); + } + $this->renderContextualLinks(array(), 'node'); + $this->assertResponse(403); + $this->renderContextualLinks($ids, 'node'); + $this->assertResponse(403); + } + + /** + * Asserts that a contextual link placeholder with the given id exists. + * + * @param string $id + * A contextual link id. + * + * @return bool + */ + protected function assertContextualLinkPlaceHolder($id) { + $this->assertRaw('
', format_string('Contextual link placeholder with id @id exists.', array('@id' => $id))); + } + + /** + * Asserts that a contextual link placeholder with the given id does not exist. + * + * @param string $id + * A contextual link id. + * + * @return bool + */ + protected function assertNoContextualLinkPlaceHolder($id) { + $this->assertNoRaw('
', format_string('Contextual link placeholder with id @id does not exist.', array('@id' => $id))); + } + + /** + * Get server-rendered contextual links for the given contextual link ids. + * + * @param array $ids + * An array of contextual link ids. + * @param string $current_path + * The Drupal path for the page for which the contextual links are rendered. + * + * @return string + * The response body. + */ + protected function renderContextualLinks($ids, $current_path) { + // Build POST values. + $post = array(); + for ($i = 0; $i < count($ids); $i++) { + $post['ids[' . $i . ']'] = $ids[$i]; + } + + // Serialize POST values. + foreach ($post as $key => $value) { + // Encode according to application/x-www-form-urlencoded + // Both names and values needs to be urlencoded, according to + // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1 + $post[$key] = urlencode($key) . '=' . urlencode($value); + } + $post = implode('&', $post); + + // Perform HTTP request. + return $this->curlExec(array( + CURLOPT_URL => url('contextual/render', array('absolute' => TRUE, 'query' => array('destination' => $current_path))), + CURLOPT_POST => TRUE, + CURLOPT_POSTFIELDS => $post, + CURLOPT_HTTPHEADER => array( + 'Accept: application/json', + 'Content-Type: application/x-www-form-urlencoded', + ), + )); } } diff --git a/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualUnitTest.php b/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualUnitTest.php new file mode 100644 index 0000000..51e9a51 --- /dev/null +++ b/core/modules/contextual/lib/Drupal/contextual/Tests/ContextualUnitTest.php @@ -0,0 +1,122 @@ + 'Conversion to and from "contextual id"s (for placeholders)', + 'description' => 'Tests all edge cases of converting from #contextual_links to ids and vice versa.', + 'group' => 'Contextual', + ); + } + + /** + * Provides testcases for testContextualLinksToId() and + */ + function _contextual_links_id_testcases() { + // Test branch conditions: + // - one module. + // - one dynamic path argument. + // - no metadata. + $tests[] = array( + 'links' => array( + 'node' => array( + 'node', + array('14031991'), + array() + ), + ), + 'id' => 'node:node:14031991:', + ); + + // Test branch conditions: + // - one module. + // - multiple dynamic path arguments. + // - no metadata. + $tests[] = array( + 'links' => array( + 'foo' => array( + 'baz/in/ga', + array('bar', 'baz', 'qux'), + array() + ), + ), + 'id' => 'foo:baz/in/ga:bar/baz/qux:', + ); + + // Test branch conditions: + // - one module. + // - one dynamic path argument. + // - metadata. + $tests[] = array( + 'links' => array( + 'views_ui' => array( + 'admin/structure/views/view', + array('frontpage'), + array( + 'location' => 'page', + 'display' => 'page_1', + ) + ), + ), + 'id' => 'views_ui:admin/structure/views/view:frontpage:location=page&display=page_1', + ); + + // Test branch conditions: + // - multiple modules. + // - multiple dynamic path arguments. + $tests[] = array( + 'links' => array( + 'node' => array( + 'node', + array('14031991'), + array() + ), + 'foo' => array( + 'baz/in/ga', + array('bar', 'baz', 'qux'), + array() + ), + 'edge' => array( + 'edge', + array('20011988'), + array() + ), + ), + 'id' => 'node:node:14031991:|foo:baz/in/ga:bar/baz/qux:|edge:edge:20011988:', + ); + + return $tests; + } + + /** + * Tests _contextual_links_to_id(). + */ + function testContextualLinksToId() { + $tests = $this->_contextual_links_id_testcases(); + foreach ($tests as $test) { + $this->assertIdentical(_contextual_links_to_id($test['links']), $test['id']); + } + } + + /** + * Tests _contextual_id_to_links(). + */ + function testContextualIdToLinks() { + $tests = $this->_contextual_links_id_testcases(); + foreach ($tests as $test) { + $this->assertIdentical(_contextual_id_to_links($test['id']), $test['links']); + } + } +} diff --git a/core/modules/edit/edit.module b/core/modules/edit/edit.module index a6ee046..f1e9227 100644 --- a/core/modules/edit/edit.module +++ b/core/modules/edit/edit.module @@ -39,7 +39,7 @@ function edit_permission() { } /** - * Implements hook_contextual_links_view_alter(). + * Implements hook_toolbar_alter(). * * In-place editing builds upon contextual.module, but doesn't actually add its * "Quick edit" contextual link in PHP (i.e. here) because: @@ -48,14 +48,16 @@ function edit_permission() { * - it should only work when JavaScript is enabled, because only then in-place * editing is possible. */ -function edit_contextual_links_view_alter(&$element, $items) { - if (!user_access('access in-place editing')) { - return; - } +function edit_toolbar_alter(&$items) { + if (isset($items['contextual'])) { + if (!user_access('access in-place editing')) { + return; + } - // Include the attachments and settings for all available editors. - $attachments = drupal_container()->get('edit.editor.selector')->getAllEditorAttachments(); - $element['#attached'] = NestedArray::mergeDeep($element['#attached'], $attachments); + // Include the attachments and settings for all available editors. + $attachments = drupal_container()->get('edit.editor.selector')->getAllEditorAttachments(); + $items['contextual']['#attached'] = NestedArray::mergeDeep($items['contextual']['#attached'], $attachments); + } } /** diff --git a/core/modules/edit/js/edit.js b/core/modules/edit/js/edit.js index f924e7b..183e70b 100644 --- a/core/modules/edit/js/edit.js +++ b/core/modules/edit/js/edit.js @@ -92,11 +92,13 @@ Drupal.edit.init = function() { // Add "Quick edit" links to all contextual menus where editing the full // node is possible. // @todo Generalize this to work for all entities. - $('ul.contextual-links li.node-edit') - .before('
  • ') - .each(function() { + $(document).on('drupalContextualLinkAdded.edit', function(event, data) { + var $editContextualLink = data.contextual.$links + .find('li.node-edit') + .before('
  • ') + .prev(); + // Instantiate ContextualLinkView. - var $editContextualLink = $(this).prev(); var editContextualLinkView = new Drupal.edit.views.ContextualLinkView({ el: $editContextualLink.get(0), model: appModel, diff --git a/core/modules/views/js/views-contextual.js b/core/modules/views/js/views-contextual.js index 3c3ae96..d10768e 100644 --- a/core/modules/views/js/views-contextual.js +++ b/core/modules/views/js/views-contextual.js @@ -8,10 +8,11 @@ Drupal.behaviors.viewsContextualLinks = { attach: function (context) { - // If there are views-related contextual links attached to the main page - // content, find the smallest region that encloses both the links and the - // view, and display it as a contextual links region. - $('.views-contextual-links-page', context).closest(':has(.view)').addClass('contextual-region'); + var id = $('body[data-views-page-contextual-id]') + .attr('data-views-page-contextual-id'); + $('[data-contextual-id="' + id + '"]') + .closest(':has(.view)') + .addClass('contextual-region'); } }; diff --git a/core/modules/views/lib/Drupal/views/Tests/UI/DisplayTest.php b/core/modules/views/lib/Drupal/views/Tests/UI/DisplayTest.php index 15cb97a..7bfb2b1 100644 --- a/core/modules/views/lib/Drupal/views/Tests/UI/DisplayTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/UI/DisplayTest.php @@ -284,8 +284,27 @@ public function testPageContextualLinks() { $this->drupalLogin($this->drupalCreateUser(array('administer views', 'access contextual links'))); $view = entity_load('view', 'test_display'); $view->enable()->save(); + $this->drupalGet('test-display'); - $this->assertLinkByHref("admin/structure/views/view/{$view->id()}/edit/page_1"); + $id = 'views_ui:admin/structure/views/view: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))); + + // Get server-rendered contextual links. + // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:renderContextualLinks() + $post = urlencode('ids[0]') . '=' . urlencode($id); + $response = $this->curlExec(array( + CURLOPT_URL => url('contextual/render', array('absolute' => TRUE, 'query' => array('destination' => 'test-display'))), + CURLOPT_POST => TRUE, + CURLOPT_POSTFIELDS => $post, + CURLOPT_HTTPHEADER => array( + 'Accept: application/json', + 'Content-Type: application/x-www-form-urlencoded', + ), + )); + $this->assertResponse(200); + $json = drupal_json_decode($response); + $this->assertIdentical($json[$id], ''); } } diff --git a/core/modules/views/views.module b/core/modules/views/views.module index f1cb911..5c86dd6 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -467,6 +467,11 @@ function views_page_alter(&$page) { * Implements MODULE_preprocess_HOOK(). */ function views_preprocess_html(&$variables) { + // Early-return to prevent adding unnecessary JavaScript. + if (!user_access('access contextual links')) { + return; + } + // If the page contains a view as its main content, contextual links may have // been attached to the page as a whole; for example, by views_page_alter(). // This allows them to be associated with the page and rendered by default @@ -479,10 +484,11 @@ function views_preprocess_html(&$variables) { // page.tpl.php, so we can only find it using JavaScript. We therefore remove // the "contextual-region" class from the tag here and add // JavaScript that will insert it back in the correct place. - if (!empty($variables['page']['#views_contextual_links_info'])) { + if (!empty($variables['page']['#contextual_links']['views_ui'])) { $key = array_search('contextual-region', $variables['attributes']['class']->value()); if ($key !== FALSE) { unset($variables['attributes']['class'][$key]); + $variables['attributes']['data-views-page-contextual-id'] = $variables['title_suffix']['contextual_links']['#id']; // Add the JavaScript, with a group and weight such that it will run // before modules/contextual/contextual.js. drupal_add_library('views', 'views.contextual-links'); @@ -491,18 +497,6 @@ function views_preprocess_html(&$variables) { } /** - * Implements hook_contextual_links_view_alter(). - */ -function views_contextual_links_view_alter(&$element, $items) { - // If we are rendering views-related contextual links attached to the overall - // page array, add a class to the list of contextual links. This will be used - // by the JavaScript added in views_preprocess_html(). - if (!empty($element['#element']['#views_contextual_links_info']) && !empty($element['#element']['#type']) && $element['#element']['#type'] == 'page') { - $element['#attributes']['class'][] = 'views-contextual-links-page'; - } -} - -/** * Adds contextual links associated with a view display to a renderable array. * * This function should be called when a view is being rendered in a particular @@ -611,12 +605,14 @@ function views_add_contextual_links(&$render_element, $location, ViewExecutable // If the link was valid, attach information about it to the renderable // array. if ($valid) { - $render_element['#contextual_links'][$module] = array($link['parent path'], $args); - $render_element['#views_contextual_links_info'][$module] = array( - 'location' => $location, - 'view' => $view, - 'view_name' => $view->storage->id(), - 'view_display_id' => $display_id, + $render_element['#contextual_links'][$module] = array( + $link['parent path'], + $args, + array( + 'location' => $location, + 'name' => $view->storage->id(), + 'display_id' => $display_id, + ) ); } } diff --git a/core/modules/views/views_ui/views_ui.module b/core/modules/views/views_ui/views_ui.module index 4c3ae95..d62eaa5 100644 --- a/core/modules/views/views_ui/views_ui.module +++ b/core/modules/views/views_ui/views_ui.module @@ -376,8 +376,8 @@ function views_ui_contextual_links_view_alter(&$element, $items) { // Append the display ID to the Views UI edit links, so that clicking on the // contextual link takes you directly to the correct display tab on the edit // screen. - elseif (!empty($element['#links']['views-ui-edit']) && !empty($element['#element']['#views_contextual_links_info']['views_ui']['view_display_id'])) { - $display_id = $element['#element']['#views_contextual_links_info']['views_ui']['view_display_id']; + elseif (!empty($element['#links']['views-ui-edit'])) { + $display_id = $element['#contextual_links']['views_ui'][2]['display_id']; $element['#links']['views-ui-edit']['href'] .= '/' . $display_id; } }