Updated: Comment #N

Problem/Motivation

In the 'Tools' block (which is enabled by default in the standard installation), there should be a contextual link 'Edit menu' but right now it just shows 'Configure Block'. Actually there's an 'Edit menu' link and we've tests for same also: MenuTest::testBlockContextualLinks

But somehow caching breaks stuff. See related issue: https://drupal.org/node/914382

How it appears now (no 'Edit menu'):
Contextual-links-not -visible.png

How it should appear (screenshot from 7.x):
Contextual-links-visible

Proposed resolution

Contextual links on menu blocks aren't working. The sad reason
for this is render caching (a fucking damn problem, seriously!).

      if ($plugin->isCacheable()) {
        $build[$entity_id]['#pre_render'][] = array($this, 'buildBlock');
        // ...
      }
      else {
        $build[$entity_id] = $this->buildBlock($build[$entity_id]);
      }

      $build[$key]['#block'] = $entity;

      $this->moduleHandler()->alter(array('block_view', "block_view_$base_id"), $build[$entity_id], $plugin);

If the menu block is marked as cacheable (per default caching is enabled) $build[$entity_id]['content'] is
not filled, menu_block_view_system_menu_block_alter() is called
and does not do anything.

Solution: Probably move the #contextual_link handling onto the post_render level instead of the alter stuff

Remaining tasks

Decide how to fix it.

User interface changes

None (apart from the fact that contextual links will actually be visible).

API changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cs_shadow’s picture

Issue summary: View changes
cs_shadow’s picture

Issue summary: View changes
dawehner’s picture

Priority: Normal » Critical
Issue summary: View changes

updated the issue summary with my "research"

Wim Leers’s picture

Title: Contextual Links not visible in 'Blocks' » Regression: Menu contextual links no longer visible in menu blocks
Assigned: Unassigned » Wim Leers
Status: Active » Needs review
Issue tags: +D8 cacheability, +Performance, +sprint
Related issues: +#2158003: Remove Block Cache API in favor of blocks returning #cache with cache tags
FileSize
1000 bytes

I must've introduced this regression in #2158003: Remove Block Cache API in favor of blocks returning #cache with cache tags.

I verified that is the only occurrence of this problem (by grepping for #contextual_link).

Attached is a patch that fixes the regression and simplifies the code. The checking for $build['content'] is completely unnecessary nowadays.

dawehner’s picture

Priority: Critical » Major

Let's be honest, this is major, compared to other broken stuff. Sadly we probably need a test which actually don't work

Wim Leers’s picture

#5: Agreed with priority change, and with your assessment: MenuTest::testBlockContextualLinks() does test this:

    $id = 'block:block=' . $block->id() . ':|menu:menu=tools:';
    // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:assertContextualLinkPlaceHolder()
    $this->assertRaw('<div data-contextual-id="'. $id . '"></div>', format_string('Contextual link placeholder with id @id exists.', array('@id' => $id)));

I don't understand why that didn't catch this problem.

dawehner’s picture

This one was initially discovered while working on https://drupal.org/comment/8677783#comment-8677783 , so maybe this test failure would be fixed here. Sadly I don't have my tools avaiable right now to remember why it worked before.

Wim Leers’s picture

What's still needed here?

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

Verified that the code is considerably simpler and the bug is fixed.

catch’s picture

Status: Reviewed & tested by the community » Fixed

If this is exposed by https://drupal.org/comment/8677783#comment-8677783 I'm OK with committing this without explicit test coverage - we'll get it once the other issue is committed.

Committed/pushed to 8.x, thanks!

  • Commit dda140f on 8.x by catch:
    Issue #2239833 by Wim Leers: Regression: Menu contextual links no longer...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Wim Leers’s picture

Issue tags: -sprint
olli’s picture

Status: Closed (fixed) » Needs review
FileSize
1.41 KB
2.04 KB

Here's a test.

The last submitted patch, 14: 2239833-fail.patch, failed testing.

dawehner’s picture

Good catch!

+++ b/core/modules/menu_ui/src/Tests/MenuTest.php
@@ -531,6 +532,12 @@ public function testBlockContextualLinks() {
+    $this->drupalPostForm('admin/structure/block/manage/' . $block->id(), ['settings[cache][max_age]' => Cache::PERMANENT], t('Save block'));
+    $this->drupalGet('test-page');
+    $id = 'block:block=' . $block->id() . ':|menu:menu=tools:';
+    $this->assertRaw('<div data-contextual-id="'. $id . '"></div>', format_string('Contextual link placeholder with id @id exists.', array('@id' => $id)));

I want to understand that. How does that actually work? You first add block caching, then you access the page once. Isn't block caching not returning a value yet? I know the test fails otherwise, so I try to understand why.

olli’s picture

Isn't block caching not returning a value yet?

Not sure what you mean by that but the existing test adds the block with "Maximum age" set to <no caching> and we have two code paths (see IS) so the #pre_render path is not yet covered.

Just curious, would it hurt/work to always run the #pre_render path?

olli’s picture

I think the original fix #4 was broken in 70bed33. Issue referenced in #10 went in but the Edit menu link is still missing.

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
2.05 KB

#14 only needed a straight reroll against HEAD. It still fixes the problem, and adds the necessary regression test coverage. Here's that reroll.

catch’s picture

Status: Reviewed & tested by the community » Needs work

Sorry folk for issue history please open a new issue rather than re-opening the old one (unless it's for a revert), just adding tests here would have been a normal task, rather than major bug.

Wim Leers’s picture

Status: Needs work » Closed (fixed)