fields of bundle are not loaded when using Context UI

Created on 3 May 2022, about 2 years ago
Updated 24 May 2024, about 1 month ago

Problem/Motivation

hi
using this module inside together with the context module is not working correctly while in block display administration it's worked as expected

Steps to reproduce

on Context UI:
1- add a context
2- for condition choose the Node field condition
3- Select the bundle and you will see that fields are not loaded as it does at block display administration

And below is the trowed error

Notice: Trying to access array offset on value of type null in Drupal\entity_field_condition\Plugin\Condition\NodeField->fieldsCallback() (line 222 of /app/web/modules/contrib/entity_field_condition/src/Plugin/Condition/NodeField.php)

#0 /app/web/core/includes/bootstrap.inc(312): _drupal_error_handler_real(8, 'Trying to acces...', '/app/web/module...', 222)
#1 /app/web/modules/contrib/entity_field_condition/src/Plugin/Condition/NodeField.php(222): _drupal_error_handler(8, 'Trying to acces...', '/app/web/module...', 222, Array)
#2 [internal function]: Drupal\entity_field_condition\Plugin\Condition\NodeField->fieldsCallback(Array, Object(Drupal\Core\Form\FormState), Object(Symfony\Component\HttpFoundation\Request))
#3 /app/web/core/lib/Drupal/Core/Form/FormAjaxResponseBuilder.php(69): call_user_func_array(Array, Array)
#4 /app/web/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php(109): Drupal\Core\Form\FormAjaxResponseBuilder->buildResponse(Object(Symfony\Component\HttpFoundation\Request), Array, Object(Drupal\Core\Form\FormState), Array)
#5 [internal function]: Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber->onException(Object(Symfony\Component\HttpKernel\Event\ExceptionEvent), 'kernel.exceptio...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#6 /app/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(142): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\ExceptionEvent), 'kernel.exceptio...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#7 /app/vendor/symfony/http-kernel/HttpKernel.php(219): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object(Symfony\Component\HttpKernel\Event\ExceptionEvent), 'kernel.exceptio...')
#8 /app/vendor/symfony/http-kernel/HttpKernel.php(91): Symfony\Component\HttpKernel\HttpKernel->handleThrowable(Object(Drupal\Core\Form\FormAjaxException), Object(Symfony\Component\HttpFoundation\Request), 1)
#9 /app/web/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#10 /app/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#11 /app/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#12 /app/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#13 /app/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#14 /app/web/core/lib/Drupal/Core/DrupalKernel.php(717): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#15 /app/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#16 {main}

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Needs review

Version

1.0

Component

User interface

Created by

πŸ‡§πŸ‡―Benin delacosta456

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.

  • πŸ‡ΊπŸ‡ΈUnited States kevinquillen

    Here is a patch that attempts to work around the issue.

    The AJAX callback checks to see if this is a Context form or any other form (block config):

      public function fieldsCallback(array $form, FormStateInterface $form_state) {
        // Check if we are on a Context form
        if (preg_match('/context_(add|edit)_form/i', $form["#form_id"])) {
          $triggering_element = $form_state->getTriggeringElement();
          $node_type = $triggering_element['#value'];
          $parents = array_slice($triggering_element['#array_parents'], 0, -1);
          $subform = NestedArray::getValue($form, $parents);
          $subform['field']['#options'] = $this->getNodeFields($node_type);
          return $subform['field'];
        } else {
          $node_type = $form_state->getValues()['visibility']['node_field']['entity_bundle'];
          $form['visibility']['node_field']['field']['#options'] = $this->getNodeFields($node_type);
          return $form['visibility']['node_field']['field'];
        }
      }
    

    With this change, I was able to then use Context to look at Node fields.

    However, there are other issues - since this is using AJAX, when you create a context and select this as your first condition, trying to select a node type triggers an AJAX callback that makes the 'Add condition' form submit again (something with EarlyRenderingControllerWrapperSubscriber::wrapControllerExecutionInRenderContext in core).

    So this requires you to:

    - Create a context
    - Save it
    - Add 'node field' condition

    Before you can really configure it.

    My suggestion would be to remove AJAX entirely and allow the user to select from a list of fields grouped by node type. There needs to be compatibility between any form(s) that implement conditions and the AJAX process is interfering I believe this same problem is present in other issues too with layout builder or other means of forms with plugin subforms. You can't rewrite parts of a form if the parent structure is not guaranteed. This callback always assume it is a block config form.

    I can't think of any other conditions off the top of my head that use AJAX and work on block/context or other types of forms with condition support - if someone knows of one lets show it as an example.

  • Status changed to Needs work about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States kevinquillen
  • Status changed to Needs review about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States danflanagan8 St. Louis, US

    I'm using this module a lot on a project with context ui. I've been editing yml manually to add these conditions, but I thought it was time to finally dig in and try to fix this. Here's a patch that avoids hardcoding any potentially variable form array keys in the fieldsCallback method. The patch doesn't try to guess where the form is being used; instead it follows #array_parents through the form.

    From my testing this works on the Block Layout UI and the Context UI.

    However! When adding the condition through context ui I got this error:

    ...The specified condition had already been added to the context...

    I worked around this by saving the context after adding the condition (leaving the condition blank). When I came back to edit the context, everything worked fine.

    I think this must be similar or the same as what @kevinquillen describes above.

  • πŸ‡ΊπŸ‡ΈUnited States kevinquillen

    Revisiting this... Did some debugging and will post findings shortly.

  • πŸ‡ΊπŸ‡ΈUnited States kevinquillen

    @danflanagan8 please see πŸ› Support subforms that have AJAX callbacks Needs review

Production build 0.69.0 2024