Problem/Motivation

Blocks are normally built by the block view builder, but Page Manager's Block page display variant builds them itself. (Because the view builder is for entities, while Page Manager only cares about the plugin+configuration?) Thus we're missing out on the block_build and block_view alter hooks.

Proposed resolution

Invoke the block_build and block_view alter hooks in PageBlockDisplayVariant::buildRegions().

Remaining tasks

User interface changes

API changes

The block alter hooks also affect Page Manager blocks.

Data model changes

Comments

Arla created an issue. See original summary.

tduong’s picture

Status: Active » Needs review
StatusFileSize
new3.5 KB

Uploaded patch:

  • invoked alter hook for block_build
  • added __construct() and create() to inject dependencies (added the module handler)

Next step: invoke alter hook for block_view

Status: Needs review » Needs work

The last submitted patch, 2: page_manager-support_hooks_TYPE_alter-2617996-2.patch, failed testing.

berdir’s picture

When we were looking at this, I was kind of wondering if there shouldn't actually be more code in the base class than there is right now?

tduong’s picture

Status: Needs work » Needs review
StatusFileSize
new3.81 KB
new6.7 KB

Fixed failing tests by PHPUnit tests.

tduong’s picture

StatusFileSize
new718 bytes
new6.67 KB

After an attentive check with @Berdir, the alter hook added in the previous patch should be related to block_view instead of block_build. Fixed that.
Also we need some more discussion about what we should do with that base class.

Created followup for block_build and for the discussion about the base class.

tim.plunkett’s picture

Issue tags: +Needs tests

This needs tests. Also $moduleHandler should be $module_handler.

tduong’s picture

Replaced $moduleHandler with $module_handler and provided a test for this alter hook.

tim.plunkett’s picture

  1. +++ b/src/Plugin/DisplayVariant/PageBlockDisplayVariant.php
    @@ -26,6 +32,55 @@ use Drupal\ctools\Plugin\DisplayVariant\BlockDisplayVariant;
    +  protected $module_handler;
    

    Sorry. I only meant change the standalone variable, not the property. Should be $this->moduleHandler, and $module_handler.

  2. +++ b/src/Tests/PageManagerAdminTest.php
    @@ -265,6 +265,32 @@ class PageManagerAdminTest extends WebTestBase {
    +  protected function doTestAlterBlock() {
    

    This method won't run unless you call it from testAdmin(). Or just make it public function testAlterBlock()

tduong’s picture

  • fixed property $moduleHandler
  • fixed doTestAlterBlock(), added it in testAdmin() (consistency)
  • fixed failing test in doTestEditVariant()
berdir’s picture

  1. +++ b/src/Plugin/DisplayVariant/PageBlockDisplayVariant.php
    @@ -93,6 +148,8 @@ class PageBlockDisplayVariant extends BlockDisplayVariant {
    +        // Allow altering of cacheability metadata or setting #create_placeholder.
    +        $this->moduleHanlder->alter(['block_view', "block_view_" . $block->getBaseId()], $block_build, $block);
    

    I think the comment here is now wrong (copied from the build hook), use the comment from the view hook instead.

  2. +++ b/src/Tests/PageManagerAdminTest.php
    @@ -265,6 +266,37 @@ class PageManagerAdminTest extends WebTestBase {
    +    $edit = [
    +      'settings[label]' => 'Altered block label',
    

    Hm. Lets use something like "Label to be altered" or something like that. Altered label is not correct since it is not (yet) altered.

  3. +++ b/src/Tests/PageManagerAdminTest.php
    @@ -265,6 +266,37 @@ class PageManagerAdminTest extends WebTestBase {
    +    // Check the altered block label.
    +    $this->assertRaw($edit['settings[label]']);
    

    Because we do *not* want to see this label. You're missing the crucial part of the test, to actually implement the alter hook and switch it to something else.

    The idea is that you set the label to what I said above, and then the alter hook changes it to "Altered label"

  4. +++ b/src/Tests/PageManagerAdminTest.php
    @@ -278,8 +310,8 @@ class PageManagerAdminTest extends WebTestBase {
     
    -    $this->assertOptionSelected('edit-variant-plugin-blocks-' . $block_config['uuid'] . '-region', 'top');
    -    $this->assertOptionSelected('edit-variant-plugin-blocks-' . $block_config['uuid'] . '-weight', 0);
    +    $this->assertOptionSelected('edit-variant-plugin-blocks-' . strtolower($block_config['uuid']) . '-region', 'top');
    +    $this->assertOptionSelected('edit-variant-plugin-blocks-' . strtolower($block_config['uuid']) . '-weight', 0);
     
    

    This is a weird php/osx bug. Remove the uuid extension locally, it incorrectly generates uppercase UUID's.

berdir’s picture

Status: Needs review » Needs work
tduong’s picture

  • fixed 1-4
  • added the help module page_manager_test.module for the page_manager test
tduong’s picture

Assigned: Unassigned » tduong

berdir’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

This looks good to me. We also tested it manually and it allows to use tokens in block titles and they are then replaced by token.module.

tim.plunkett’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/src/Plugin/DisplayVariant/PageBlockDisplayVariant.php
    @@ -26,6 +32,55 @@ use Drupal\ctools\Plugin\DisplayVariant\BlockDisplayVariant;
    +  protected $moduleHanlder;
    

    Hanlder is spelled wrong

  2. +++ b/src/Plugin/DisplayVariant/PageBlockDisplayVariant.php
    @@ -26,6 +32,55 @@ use Drupal\ctools\Plugin\DisplayVariant\BlockDisplayVariant;
    +  public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextHandlerInterface $context_handler, AccountInterface $account, UuidInterface $uuid_generator, Token $token, ModuleHandlerInterface $module_handler) {
    

    The upstream signature changed, this needs a reroll.

tduong’s picture

Status: Needs work » Needs review
StatusFileSize
new6.58 KB
new10.02 KB

Rerolled and refactored the typo.

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Reroll looks good to me, typo fixed.

tim.plunkett’s picture

Status: Reviewed & tested by the community » Fixed

Committed

  • tim.plunkett committed b6b3f9f on 8.x-1.x authored by tduong
    Issue #2617996 by tduong: Support hook_block_build_alter() and...

Status: Fixed » Closed (fixed)

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