Problem/Motivation

In #2543340: Convert BlockViewBuilder to use #lazy_builder (but don't yet let context-aware blocks be placeholdered), it was discovered that these blocks carry around state, for no good reason. It used to be necessary due to limitations in the block rendering pipeline, but those limitations have since been fixed.

Rectifying this also makes the blocks significantly simpler.

And, in fact, due to this particular behavior, the Help block sometimes fail to bubble the url cache context (because access is denied based on the URL, yet the access result does not reflect this!).

Proposed resolution

Fix them.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Task because 1) sets the right example for contrib developers, 2) helps improve cacheability, 3) even fixes a cacheability bug
Issue priority Normal because no big impact.
Prioritized changes The main goal of this issue is DX + cacheability.
Disruption Zero disruption.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Wim Leers’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
5.42 KB
Wim Leers’s picture

dawehner’s picture

Wait, don't we still have the problem of empty blocks not showing to be empty?

Wim Leers’s picture

Wait, don't we still have the problem of empty blocks not showing to be empty?

No, that was fixed. See if ($content !== NULL && !Element::isEmpty($content)) {. Fixed in #1805054: Cache localized, access filtered, URL resolved, and rendered menu trees.

Crell’s picture

  1. +++ b/core/modules/help/src/Plugin/Block/HelpBlock.php
    @@ -93,19 +86,6 @@ public static function create(ContainerInterface $container, array $configuratio
    -  protected function blockAccess(AccountInterface $account) {
    -    $this->help = $this->getActiveHelp($this->request);
    -    if ($this->help) {
    -      return AccessResult::allowed();
    -    }
    -    else {
    -      return AccessResult::forbidden();
    -    }
    -  }
    

    Do we perhaps need a comment on the method in the interface that this method should NOT be used to vary visibility based on stateful information, eg, data that would be shared with build()? Seems like a wise thing to do, since we want to discourage module authors from doing... what we're making this block not do anymore. :-)

  2. +++ b/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php
    @@ -123,18 +86,25 @@ public function blockSubmit($form, FormStateInterface $form_state) {
    -    if ($this->day_list) {
    -      $content['top_day'] = $this->day_list;
    -      $content['top_day']['#suffix'] = '<br />';
    +    if ($this->configuration['top_day_num'] > 0) {
    +      $result = statistics_title_list('daycount', $this->configuration['top_day_num']);
    +      if ($result) {
    +        $content['top_day'] = node_title_list($result, $this->t("Today's:"));
    +        $content['top_day']['#suffix'] = '<br />';
    +      }
         }
    

    These changes are going to conflict with #1446932: Improve statistics performance by adding a swappable backend, which is largely rewriting this method.

Wim Leers’s picture

#5:

  1. I think that actually belongs on the interface docs in general, and not on this specific method. i.e. where it currently says:
     * @todo Add detailed documentation here explaining the block system's
     *   architecture and the relationships between the various objects, including
     *   brief references to the important components that are not coupled to the
     *   interface.
    

    i.e. blocks should not do stateful things in general. They should just… calculate things. But IMO that's out of scope for this issue. Created an issue for that: #2543984: Write BlockPluginInterface's docblock.

  2. Should be an easy-to-resolve conflict. But thanks for the warning.
timmillwood’s picture

As much as I hate conflicts I agree the statistics block should be an easy-to-resolve conflict, and as much as I would love #1446932: Improve statistics performance by adding a swappable backend into 8.0.x I'm not sure I can argue the case, so guess I need to accept conflicts.

Crell’s picture

Sorry Wim, but I disagree. Whether or not we punt on documenting something seems to vary widely, depending primarily on whether or not jhodgdon is in the issue thread. :-) I'm going to channel her in this case and say that if this issue is fixing "these blocks are doing something unnecessarily stupid", then it should also be responsible for the documentation to ensure no one else does anything unnecessarily stupid.

Wim Leers’s picture

#7: I'd be happy to reroll it for you even :) I'm probably more used to resolving conflicts, so happy to do it if that helps!

#8: Please, no. After >2 years, block plugins still have not been documented. Let's not put the responsibility to do that on an otherwise simple clean-up issue. These blocks are simply unnecessarily complex in their implementation. We should resolve that regardless of #2543340: Convert BlockViewBuilder to use #lazy_builder (but don't yet let context-aware blocks be placeholdered)'s needs, which just happened to resurface this.

timmillwood’s picture

@wim - My patch pretty much just rewrites statistics_title_list function to work with the service also added in the patch, so should be pretty straightforward to merge.

If this was just the statistics patch I would RTBC, but I guess the block docs needs a fight.

Crell’s picture

Status: Needs review » Reviewed & tested by the community

I talked with Wim in IRC earlier today about this issue, and offered to just write the requisite docs myself. However, after staring at it for a while I realized I didn't know how to do it justice without referencing StatefulBlockPluginInterface as the correct way to handle edge cases... which of course doesn't exist yet and won't until #2543340: Convert BlockViewBuilder to use #lazy_builder (but don't yet let context-aware blocks be placeholdered). Sooo... I'm going to RTBC this and instead be annoying about documentation in that issue instead, as then we can do it properly (and it's partially done already by StatefulBlockPluginInterface itself).

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 1: 2543554-1.patch, failed testing.

Status: Needs work » Needs review

timmillwood queued 1: 2543554-1.patch for re-testing.

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community

Green again.

tim.plunkett’s picture

+1 as subsystem maintainer.

webchick’s picture

Issue tags: -Quickfix

I cannot possibly fathom how this is a quick fix. :D

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Needs a reroll.

tim.plunkett’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs reroll
FileSize
5.41 KB

Simple reroll.
Conflicted with the cache context change of #2543332: Auto-placeholdering for #lazy_builder without bubbling

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committing this as it improves cacheability. Committed d904faa and pushed to 8.0.x. Thanks!

Thanks you for adding the beta evaluation to the issue summary.

  • alexpott committed d904faa on 8.0.x
    Issue #2543554 by Wim Leers, tim.plunkett: Clean up Help...

Status: Fixed » Closed (fixed)

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