While it would be nice to have a UI to configure what blocks can be placed in panels (see #2764897: Make available layouts and blocks configurable, a simple starting point would be to define a hook that lets modules override what blocks are available for placement in the Panels IPE. It's a quick and dirty solution, but it would make it possible to optimise the user experience. The attached patch does just that.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

floretan created an issue. See original summary.

muschpusch’s picture

Works great but after removing some items the array key order needs to get restored. e.g. array_values($data); not sure if this should be part of the patch.

samuel.mortenson’s picture

Status: Active » Needs review
samuel.mortenson’s picture

FileSize
605 bytes

Re-rolled the patch.

samuel.mortenson’s picture

Status: Needs review » Needs work

Realized this needs API documentation as well in panels_ipe.api.php

DamienMcKenna’s picture

Status: Needs work » Needs review
FileSize
2.04 KB
2.16 KB

Documentation.

DamienMcKenna’s picture

Title: Provide hook to alter available blocks » Provide hook to alter available blocks for IPE
DamienMcKenna’s picture

Renamed a variable but forgot to update all occurrences.

DamienMcKenna’s picture

japerry’s picture

Status: Needs review » Fixed

This, in theory works for me. And it didn't seem to break anything. Committed.

  • japerry committed e9f5a98 on 8.x-3.x authored by DamienMcKenna
    Issue #2767087 by DamienMcKenna, samuel.mortenson, floretan: Provide...
hideaway’s picture

Just a quick note to not forget to reassign keys in $blocks array, if you unset any block before end of the list (blocks which are not at the end of an array). Otherwise the blocks wont be rendered in IPE. I have to put the following line before end of alter hook:
$blocks = array_values($blocks);

I guess the response has some kind of problems if you unset key in indexed array and end up with something like this

[
  0 => $block1, 
  1 => $block2, 
  3 => $block4
]

In this example I unset block with index 2.

Probably the best way would be to put the fix directly after tiggering alter hooks. So we don't need to reassign keys in alter hooks. We would end up with the:

    // Trigger hook_panels_ipe_blocks_alter(). Allows other modules to change
    // the list of blocks that are visible.
    \Drupal::moduleHandler()->alter('panels_ipe_blocks', $blocks);
    // We need to reindex keys in indexed array so they are properly rendered
    $blocks = array_values($blocks);

  • samuel.mortenson committed 133f64f on 8.x-3.x
    Issue #2767087 by hideaway, samuel.mortenson: Re-index IPE blocks after...
samuel.mortenson’s picture

@hideaway Done - this was brought up in #2 but never addressed. I committed a quick follow-up based on your comment. Thanks!

  • samuel.mortenson committed 133f64f on 8.x-4.x
    Issue #2767087 by hideaway, samuel.mortenson: Re-index IPE blocks after...
  • japerry committed e9f5a98 on 8.x-4.x authored by DamienMcKenna
    Issue #2767087 by DamienMcKenna, samuel.mortenson, floretan: Provide...

Status: Fixed » Closed (fixed)

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

dsnopek’s picture

I just posed a follow-up to this issue to add some useful context to the hook:

#2886230: hook_panels_ipe_blocks_alter() should receive the PanelsDisplayVariant being editted