- πΊπΈ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' conditionBefore 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
over 1 year ago 3:25pm 17 April 2023 - Status changed to Needs review
over 1 year ago 10:19pm 8 June 2023 - πΊπΈ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