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.
Problem/Motivation
When a new module is installed, the kernel is updated to include the module. Later, the module handler invokes the hook_install()
.
In circumstances where module_set_weight()
is being called inside the hook_install()
, the kernel has already been updated, so the module's weight is not properly set in the kernel.
Proposed resolution
Alter the order in which the kernel is updated and the hook is invoked, OR add an additional kernel update after the hook has been invoked.
Remaining tasks
Fix it.
User interface changes
None.
API changes
None.
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#26 | 2679008-26.interdiff.txt | 589 bytes | webflo |
#26 | 2679008-26.patch | 4.65 KB | webflo |
#25 | 2679008-25.interdiff.txt | 670 bytes | webflo |
#25 | 2679008-25.patch | 4.97 KB | webflo |
#22 | 2679008-22-module-weight.patch | 4.98 KB | gilesmc |
Comments
Comment #2
dawehnerIMHO this is not the bug. You should be able to have a fully working system when
hook_install()
is called, which requires the kernel updating to be done.The bug is that changing the configuration, so the module weight, doesn't refire a container rebuild.
Comment #4
stBorchertThere is a very simple test for this:
* create 2 modules: module_parent and module_child
* add *.install to both modules implementing
hook_install()
(update according to the modules name):* make module_child dependent of module_parent
* install module_child
Result:
* module weight of module_parent is "0"
* module weight of module_child is "1"
For me, simply reloading the extension config did the trick (see patch).
Comment #5
dawehnerLet's adapt the title to be more sane.
Comment #6
dawehnerIt would be great to explain why it changes, so for example point to
module_set_weight()
Comment #7
stBorchertUpdated the comment and added a test for this.
Comment #10
webflo CreditAttribution: webflo at UEBERBIT GmbH commentedI took me a while to unterstand the root cause of this issue. I tried to improve the comment a bit.
Comment #11
dawehnerOut of my own interest I ran the test locally without the fix, so yeah this fix indeed fixes the problem. From my point of view the fix seems totally logical.
Comment #13
clemens.tolboomI've tested the patch manually to see it fixes Tour builder #2884881: Operations are not updated on the demo site (Cache clear vs drush cache-rebuild). And it does not fix it.
Comparing the UI Performance / Clear cache and
drush cache-rebuild
vs this patchI wonder why
- there is no UI for rebuild all?
- drush using drupal_rebuild
Is this enough comparing with drush cache-rebuild
Comment #14
clemens.tolboomMy tour_builder hook_entity_type_alter is not called after installing the module.
function tour_builder_entity_type_alter(array &$entity_types) {
overrides the TourUiListbuilder and adds Operations.Steps to reproduce
drush @drupal.d8 pm-uninstall tour_builder --yes
drush @drupal.d8 pm-enable tour_builder --yes
drush @drupal.d8 cache-rebuild
applying the patch I can confirm the module weight is adjusted using Devel PHP
$modules = \Drupal::config('core.extension')->get('module');if (isset($modules['tour_builder'])) {
dpm($modules['tour_builder'], 'weight');
}
?>
Checking Drush calling
drupal_rebuild()
and addingdrupal_flush_all_caches();
fixes my hook weight getting respected.drush @drupal.d8 pm-uninstall tour_builder --yes
drush @drupal.d8 pm-enable tour_builder --yes
Comment #16
tstoeckler@clemens.tolboom That's unfortunate that this patch does not fully solve your issue. But since this is a pretty intricate bug, it seems like we could solve the additional cache clear with dedicated tests in a follow-up? I don't think we can get away with the
drupal_flush_all_caches()
there, because that will make the Drupal installation much slower, which would be very unfortunate. So maybe there is another more fundamental cache invalidation bug that you have uncovered and the cache clear just fixes the symptom by wiping all caches. In any case, do you agree to move this to a follow-up? Would be awesome to get this nasty bug fixed...Comment #17
clemens.tolboomI've created #2886083: After installing tour_builder at least hook_entity_type_alter is not called by modules weight.
To not derail this issue any further I re-added patch #10
Comment #19
tstoecklerThanks a lot, that's nice. See you over there, then ;-)
Comment #20
clemens.tolboomIndeed RTBC
Build failure is out of disk space
Comment #21
larowlanthis is yuck, because the dependency on the config factory is hidden, but its pre-existing so untangling that is out of scope.
Twice in this method we're calling \Drupal::configFactory() - can we store a reference to that in a local variable instead to remove the duplicate call to the container? When we eventually get to make the dependency explicit, having the single variable will help with refactoring too.
does this needs to be one line?
does this has to be less than 80 chars according to our phpcs?
Comment #22
gilesmc CreditAttribution: gilesmc commentedRe #21
1. It looks to me like the second call needs to be there, so I have left this as it is
2. & 3. - I have updated the comments here.
Comment #23
dawehnerNote: storing a local variable is tricky. It might be the case that the kernel was rebuild in the meantime. This is why #2380293: Properly inject services into ModuleInstaller is not as easy as it sounds like in the first place.
Comment #25
webflo CreditAttribution: webflo at UEBERBIT GmbH commentedThe patch in #22 introduced some whitespace in module_handler_test_multiple_child.install which caused the test to fail.
Comment #26
webflo CreditAttribution: webflo at UEBERBIT GmbH commentedComment #27
tstoecklerThanks! Back to RTBC, then.
Comment #29
catchAgreed with getting this in as-is with the test coverage, the issue to do DI properly is already open, but having the system functionally correct only helps with that.
Comment #30
penyaskitoJust for the record, this broke the Lingotek module tests, which depends on content_translation, which has a
module_set_weight
inhook_install
.Not sure if this needs a change record and possibly could break other modules.
Comment #31
clemens.tolboomlingotek #2892220: Change weight of the lingotek module after content_translation changed
@penyaskito regarding a CR this was a bug fix so I guess it needs no CR.
Comment #32
penyaskitoIn theory, yes, it's a bugfix so no CR needed. In practice, modules may break or have problems. E.g., if content_translation was not enabled when lingotek is installed, content_translation weight will be 0 instead of the 10 that would be expected (see
content_translation_install()
.There are 48 implementations of hook_install in core, the ones using module_set_weight which may need a
hook_update
for updating the weight:Also very popular modules like paragraphs have a similar issue:
Also pathauto changes its weight in
hook_install
.