[regression] Flag form element also appearing on non-edit forms

Created on 8 September 2020, about 4 years ago
Updated 30 January 2023, almost 2 years ago

In #2759671: Flag form element shows on entity delete forms the flag form element incorrectly appearing on non-edit forms was solved. However, I still see the element appearing on other forms,such as the ones provided by the Scheduled Transitions module.

It's also accompanied with the following notices/warnings:

    Notice: Undefined index: actions in flag_form_alter() (line 183 of modules/contrib/flag/flag.module).

    flag_form_alter(Array, Object, 'node_product_scheduled_transitions_entity_form_form') (Line: 539)
    Drupal\Core\Extension\ModuleHandler->alter('form', Array, Object, 'node_product_scheduled_transitions_entity_form_form') (Line: 838)
    Drupal\Core\Form\FormBuilder->prepareForm('node_product_scheduled_transitions_entity_form_form', Array, Object) (Line: 279)
    Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 91)
    Drupal\Core\Controller\FormController->getContentResult(Object, Object)
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 573)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 151)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 47)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
    Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

    Warning: array_keys() expects parameter 1 to be array, null given in flag_form_alter() (line 183 of modules/contrib/flag/flag.module).

    flag_form_alter(Array, Object, 'node_product_scheduled_transitions_entity_form_form') (Line: 539)
    Drupal\Core\Extension\ModuleHandler->alter('form', Array, Object, 'node_product_scheduled_transitions_entity_form_form') (Line: 838)
    Drupal\Core\Form\FormBuilder->prepareForm('node_product_scheduled_transitions_entity_form_form', Array, Object) (Line: 279)
    Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 91)
    Drupal\Core\Controller\FormController->getContentResult(Object, Object)
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 573)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 151)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 47)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
    Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

    Warning: Invalid argument supplied for foreach() in flag_form_alter() (line 183 of modules/contrib/flag/flag.module).

    flag_form_alter(Array, Object, 'node_product_scheduled_transitions_entity_form_form') (Line: 539)
    Drupal\Core\Extension\ModuleHandler->alter('form', Array, Object, 'node_product_scheduled_transitions_entity_form_form') (Line: 838)
    Drupal\Core\Form\FormBuilder->prepareForm('node_product_scheduled_transitions_entity_form_form', Array, Object) (Line: 279)
    Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 91)
    Drupal\Core\Controller\FormController->getContentResult(Object, Object)
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 573)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 151)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 47)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
    Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

After some digging it appears #2867967: isAddEditForm() only needs to be called once (re-)introduced this issue, along with the notices and warnings. The problem is in the following piece of code:

  $flags = $flag_service->getAllFlags($entity->getEntityTypeId(), $entity->bundle());

  // Check the first flag and return early if the form isn't considered to be
  // an edit form.
  if (!empty($flags) && isset($flags[0]) && $flags[0] instanceof FlagInterface) {
    if (!$flags[0]->getFlagTypePlugin()->isAddEditForm($object->getOperation())) {
      return;
    }
  }

$flags is an associative array, but the code is treating it as a numeric array.

🐛 Bug report
Status

Needs review

Version

4.0

Component

Flag core

Created by

🇧🇪Belgium rp7

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.

  • 🇩🇪Germany kle

    In combination with scheduled_transitions.module the problem is not solved.
    Route /node/{node}/scheduled-transitions throws err in Line 183.
    Reason:
    Line 136 if (!empty($flags) && isset($flags[0]) cannot work, because indices of $flags are keys - not numbers. So it will never return here.
    My solution seems to work:

      if (!empty($flags)) {
        $first_flag = reset($flags);
        if ($first_flag instanceof FlagInterface && !$first_flag->getFlagTypePlugin()->isAddEditForm($object->getOperation())) {
          return;
        }
      }
    
Production build 0.71.5 2024