Problem/Motivation
There is a notice displayed:
Notice: Undefined index: #attached in Drupal\content_moderation_info_block\Form\ContentModerationInfoBlockModerationForm->buildForm() (line 82 of /web/modules/contrib/content_moderation_info_block/src/Form/ContentModerationInfoBlockModerationForm.php)
This is caused by this line of code:
$key = array_search('content_moderation/content_moderation', $form['#attached']['library'], TRUE);
when the #attached
key is not available in the form array.
I have managed to track down the reason for that thing. The ContentModerationInfoBlockModerationForm
::buildForm calls the parent:buildForm which builds the EntityModerationForm
. The EntityModerationForm
checks the workflow for the entity and determines the available transitions for the entity. If it finds none it does an early exit:
EntityModerationForm line 98:
if (!count($target_states)) {
return $form;
}
and it does it BEFORE setting the #attached
libraries (it does it in line 131.
So the notice happens when there are no valid states for the current entity.
Steps to reproduce
1. Enable the module
2. Enable the block
3. Visit the entity page with enabled moderation
4. Set the moderation state to some state that does not have any allowed transitions
Proposed resolution
Either check if there is a new_state
element defined in the form (its added by the parent form after the early exit) or simply do
if (!isset($form['#attached']['library'])) {
return $form;
}
$key = array_search('content_moderation/content_moderation', $form['#attached']['library'], TRUE);
Remaining tasks
Create a patch