Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I have needed on more than one occasion to be able to predict the delta of a menu block when it is created, or to be in control of the delta in the way that views is.
Scenario:
- A user-triggered event will create a menu block programmatically.
- The menu block needs to appear in a specific region on one specific page of a sub-theme. This could be achieved through the block settings, however the entire site layout is configured by context so it would not be a pretty solution to control the placement of one block in a different way.
- Since the block will only be created at some arbitrary time when the user triggers the event, the context must already be in place so that when the block is created it shows in the correct place immediately. The only way to achieve this is to create the context first, and since contexts control block placements using the block deltas we must know the delta of the menu block at this stage - i.e. before the block is created. Therefore the delta must be set in the context, and then programmatically applied to the menu block when it is created.
This means that I need an extra field added to the menu block admin form which will allow you to optionally set the block delta.
Comments
Comment #1
melissavdh CreditAttribution: melissavdh commentedI have created a patch to do this.
Comment #2
StoraH CreditAttribution: StoraH commentedI have the same issue but I don't use the block module. I use the ctools plugin, so here is a patch to set delta on menu_block panes.
Comment #3
Dave ReidPatches need to be integrated together.
Comment #4
pacproduct CreditAttribution: pacproduct commentedI think that would be a good addition to the module to have cleaner block deltas, particularly useful when placing blocks programmatically as it makes the code way more readable and maintainable.
Patch #1 works well for me.
Here is a new patch combining both patches #1 and #2.
I also included a minor change fixing the following notice:
Interdiff of my change:
Comment #5
pacproduct CreditAttribution: pacproduct commentedForgot to mention that my patch is also a reroll for version 7.x-2.x
Comment #6
chrisgross CreditAttribution: chrisgross commentedThis patch works, but the delta validation triggers when editing an existing menu block, and since it still thinks you are trying to create a new delta, the form fails validation.
Comment #7
chrisgross CreditAttribution: chrisgross commentedThis patch moves the validation function from the delta_value element to the add_block form. Since the field is disabled after creation, there is no need to validate it every time you submit the form.