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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

melissavdh’s picture

I have created a patch to do this.

StoraH’s picture

I 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.

Dave Reid’s picture

Status: Active » Needs work

Patches need to be integrated together.

pacproduct’s picture

Status: Needs work » Needs review
FileSize
4.3 KB

I 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:

Notice: Undefined index: delta in menu_block_configure_form() (line 299 of /srv/projects/brumob/project/www/sites/all/modules/contrib/menu_block/menu_block.admin.inc).

Interdiff of my change:

diff --git a/menu_block.admin.inc b/menu_block.admin.inc
index 0d42eb7..b40f12d 100644
--- a/menu_block.admin.inc
+++ b/menu_block.admin.inc
@@ -302,7 +302,7 @@ function menu_block_configure_form($form, &$form_state) {
     '#type' => 'textfield',
     '#title' => t('Block machine name (delta)'),
     '#description' => t('Enter the human-readable machine name of the block to use in contexts etc, or leave blank to use default numbering'),
-    '#default_value' => $config['delta'],
+    '#default_value' => !empty($config['delta']) ? $config['delta'] : '',
     '#disabled' => !empty($config['delta']) ? TRUE : FALSE,
     '#maxlength' => 32,
     '#element_validate' => array('menu_block_configure_form_delta_validate'),
pacproduct’s picture

Version: 7.x-2.3 » 7.x-2.x-dev

Forgot to mention that my patch is also a reroll for version 7.x-2.x

chrisgross’s picture

This 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.

chrisgross’s picture

This 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.