Undefined index #attached when there are no valid transitions

Created on 10 November 2022, over 1 year ago
Updated 29 September 2023, 9 months ago

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

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡΅πŸ‡±Poland tom_ek

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024