Problem
Assigning a default 'region'
in hook_block_info() does not enable the block when the module is enabled.
Proposed solution
When a module is enabled, automatically enable the blocks that have a non-empty region
(and set their status = 1
).
Details
- hook_block_info() allows a module to define blocks using these properties:
status
- Whether the module that defines the block is enabled - if not, then the (possibly configured) block is not exposed on the block overview page.region
- The default region to enable the block in.weight
- The default weight for the block within the region.
- Modules that want to auto-enable one of their blocks need to define both
region
andstatus
. Omitting one of both does not enable the block. - Installation profiles could additionally use a helper function to simplify the assignment of blocks to regions:
- The
powered-by
block should be enabled in the default installation profile, but disabled in the minimal installation profile. - The
search
block should be in the Dashboard region of the administration theme and also enabled in another region of the default theme. - Some blocks should only be visible in some regions of the Dashboard (in the administration theme).
However, this does not necessarily have to happen in this issue/patch already.
- The
Related issues / Dependencies
- #1373312: Assign system_main block to 'content' region and help block to 'help' region by default (followup)
- #1227966: Dynamically-defined blocks are never removed from the site, even when they disappear from hook_block_info()
Original report
hook_block_list() supports defining blocks that should be enabled by default when the module is enabled. System blocks that are enabled by default (the content block, navigation, management, etc.) do not currently use this feature, partly because it is indeed broken.
Comment | File | Size | Author |
---|---|---|---|
#43 | drupal8.block-region-status.42.patch | 1.59 KB | sun |
#41 | 512464-block-rehash-2.patch | 2.51 KB | Niklas Fiekas |
#32 | 512464-block-rehash.patch | 11.96 KB | Niklas Fiekas |
#24 | block_problem.jpg | 72.67 KB | dcor |
#20 | drupal.block-rehash.20.patch | 14.17 KB | sun |
Comments
Comment #1
Damien Tournoud CreditAttribution: Damien Tournoud commentedThis patch:
- adds a block_modules_enabled() that rehash all the enabled themes to activate the new blocks
- refactors _block_rehash() into a block_rehash_all() and a block_rehash($theme), and make it a public API function
- defines sane defaults in each module
- removes the manual activation in core default installation profiles
Comment #3
Damien Tournoud CreditAttribution: Damien Tournoud commentedInteresting, this uncovered a bug in block_initialize_theme_blocks() in which disabled blocks assigned to the empty region ('') were reassigned to the default region of the theme. This wasn't showing up before because disabled blocks were not in the {block} table inside the simpletest environment because _block_rehash() was never called.
Comment #4
moshe weitzman CreditAttribution: moshe weitzman commentedsubscribe. this has long been a dark corner of drupal. time to clean it up!. what happens after install if the install profile does not have a left/right/footer region? i'm not sure what should happen - i guess the block should appear as disabled in the blocks ui.
Comment #5
moshe weitzman CreditAttribution: moshe weitzman commentedI went through the installer and fiddled with block movement and all looks good here. The issue of blocks being placed into regions that the theme does not support is a separate issue. This issue is just bug fix.
Comment #6
Gábor HojtsyThis looks like a pretty good improvement. From the code it looks like those, who would like to have a custom set of blocks (not what modules define by default) in an install profile could just add the blocks with disabled (status = 0) rows, and block_rehash() would obey existing block data anyway.
I am also happy to see the default enabled blocks feature actually work after so long :)
Two possibly minor nitpicks:
- you use static $theme_key, while drupal_static() is all the rage for better testability (even though the function has an argument to change the static, drupal_static() provides a generic way
- not sure I'd do a separate foreach() in system_block_list() on the two menu names, but instead do that via in_array() in the previous loop
Comment #7
Damien Tournoud CreditAttribution: Damien Tournoud commentedThis is a callback function. The static is not a cache, but a way to pass information from the caller to the callback (a poorman's closure, if you may). There is zero point in using drupal_static() here. In this new patch, I even remove the other call to drupal_static() (this one had no reset, which is basically a bug).
Done.
Comment #9
Damien Tournoud CreditAttribution: Damien Tournoud commentedI failed to save a file, apparently.
Comment #10
Damien Tournoud CreditAttribution: Damien Tournoud commentedThird time a charm?
Comment #12
moshe weitzman CreditAttribution: moshe weitzman commentedA couple hunks are failing now. @Damien - could you reroll? This was RTBC before so not much left.
Comment #13
Nick_vhWe were just facing this issue and were planning to make a patch, but appearently it was already done :-)
Hope it can be commited before the codefreeze
Comment #14
sunThat return looks superfluous to me.
Why only enabled themes?
If there is a reason, we should explain in the PHPDoc description.
Since you're concatenating the placeholders here, it would be good to use @ instead of % placeholders, I think.
I really like those being removed... but I do wonder how install profiles will be able to setup blocks.
The more I think about that question (already asked moshe in IRC), the more I think the question doesn't belong into this issue... here, we're just moving the default block configuration, and apparently, both core install profiles are using the default settings exposed by core modules, so we just don't care. :)
This review is powered by Dreditor.
Comment #15
sunRe-rolled against latest HEAD with all suggestions.
Comment #17
sunThat was part of the tutorial: In above demonstration, you see how the testbot behaves when code uses a constant that is not defined. The issue that we have ~13,000 passing tests, ~5,000 failing tests, but only ~13,000 tests in total is hopefully an already known issue, but for us, it really only matters to use constants that are defined.
:-P
Comment #19
moshe weitzman CreditAttribution: moshe weitzman commentedFalures are due to region name changes. Also note that dashboard region now displays on the system blocks.
Comment #20
sunHopefully, this should pass.
Comment #22
sunThis patch would really help the UX team in cleaning up the interface... Bojhan struggled to find the place where the Search block's title is setup in #607410: Remove Search this site , and yeah, it's a total WTF.
Comment #23
sun.
Comment #24
dcor CreditAttribution: dcor commentedHi, just wondering if there's an update on this topic - I'm going through the motions of setting up a site in D7 and installing a theme (in this case Basic 7.x) and I'm getting a lovely set of error messages just from trying to add existing blocks (I haven't created any of my own) to my test site. See attached photo of the error message.
"Notice: Undefined index: region in block_admin_display_form_submit() (line 121 of /home/orchidil/public_html/scor/modules/block/block.admin.inc)."
Also, whatever changes I've made results in these error messages plus "The block settings have been updated" and yet, when I reload the page in both firefox and safari (not-logged-in)... I see absolutely no difference. What I'm looking for is for the little log-in block to vanish as an example.
Comment #25
sun.core CreditAttribution: sun.core commentedSearch block is not enabled upon installing the module. It should, or shouldn't it? This patch should fix it.
Comment #26
sun.core CreditAttribution: sun.core commentedComment #27
catchDowngrading all D8 criticals to major per http://drupal.org/node/45111
Comment #28
Damien Tournoud CreditAttribution: Damien Tournoud commentedBugs cannot target D8, as the tree is not open yet.
Comment #29
webchickComment #30
yoroy CreditAttribution: yoroy commentedTagging novice for a straight reroll of #20 using Git.
Comment #31
Niklas Fiekas CreditAttribution: Niklas Fiekas commentedJust tried it. Rerolling this isn't trivial (but maybe still novice) since lot's of code has been refactored in the meantime.
Comment #32
Niklas Fiekas CreditAttribution: Niklas Fiekas commentedAlright, let's try this one.
Comment #34
Niklas Fiekas CreditAttribution: Niklas Fiekas commentedTwo intresting failures: How can we handle admin themes and the dashboard? Is the secound assertion still valid?
Comment #35
yoroy CreditAttribution: yoroy commentedThanks for taking this on Niklas. I'll see if somebody can help this move along (I can't :-)
Comment #35.0
Niklas Fiekas CreditAttribution: Niklas Fiekas commentedSummarize issue.
Comment #36
Niklas Fiekas CreditAttribution: Niklas Fiekas commentedMaybe:
No ... better idea. Will try it out and write it down and maybe discuss in IRC shortly.
Comment #37
sunI'm no longer sure whether we actually want to do this, because it hard-codes a range of assumptions that only apply to core profiles and core themes.
Comment #38
yoroy CreditAttribution: yoroy commentedI asked sun for some clarification in IRC:
Comment #39
Niklas Fiekas CreditAttribution: Niklas Fiekas commentedThis issue is about two things:
region
inhook_block_info()
is buggy and doesn't work as expected.region
to give lot's of blocks default regions.Since 2 is questionable, should we just get 1 in? Maybe together with #1373312: Assign system_main block to 'content' region and help block to 'help' region by default (followup)? We can always do 2 later or differently.
Comment #39.0
Niklas Fiekas CreditAttribution: Niklas Fiekas commentedReword proposed solution.
Comment #39.1
sunUpdated issue summary.
Comment #39.2
sunUpdated issue summary.
Comment #39.3
sunUpdated issue summary.
Comment #39.4
sunUpdated issue summary.
Comment #40
sunAgreed. I've updated/rewritten the issue summary.
Comment #41
Niklas Fiekas CreditAttribution: Niklas Fiekas commentedReroll with just that.
Comment #43
sunmmm, not sure what remains of that except for the hook_modules_enabled() implementation.
I guess that attached patch is what we actually want to change here. As outlined in the summary, this somewhat depends on #1373312: Assign system_main block to 'content' region and help block to 'help' region by default (followup)
Comment #44
sun#43: drupal8.block-region-status.42.patch queued for re-testing.
Comment #45
Niklas Fiekas CreditAttribution: Niklas Fiekas commented#43: drupal8.block-region-status.42.patch queued for re-testing.
Comment #46
benjy CreditAttribution: benjy commentedComment #46.0
benjy CreditAttribution: benjy commentedUpdated issue summary.
Comment #47
benjy CreditAttribution: benjy commentedThis has already been fixed in HEAD.