form #states do not work when form is built from entity.form_builder

Created on 26 February 2018, over 6 years ago
Updated 24 May 2024, 4 months ago

Problem/Motivation

I have a custom module which opens an entity add form in a modal in 2 different ways:

1. simply creating a link and adding the modal attrs to the link.
- this method works perfectly except (afaik) there are no js hooks provided by this (there really should be!!) which would allow me to pass client side info to the modal. As a result i also use 2nd approach.

2. I have an ajax call triggered on a mouseup event. In js, code is:

     var callback = 'reserve/ajax/reservation_add';
            $.ajax({
              async: false,
              url: Drupal.url(callback),
              data: {
                count: count,
                path: href
              },
              //dataType: 'json',
              success: function success(data) {
                if (data) {
                  var $myDialog = $('<div>' + data + '</div>').appendTo('body');
                  var options = {
                    title: 'Add Reservation',
                    width: 700,
                  };
                  Drupal.dialog($myDialog, options).showModal();
                }
              }
            });

and on server, the main code (not including routes, etc) is:

public function reservationAddModalCallback() {
    $response = new jsonResponse();

    $entity = ReserveReservation::create();
    $editForm = \Drupal::service('entity.form_builder')->getForm($entity, 'default');
    $form = \Drupal::service('renderer')->renderPlain($editForm);

    $response->setData($form);

    return $response;
  }

Method #2 does work and gives me the ability to pass info to/from the js code. But, the modal does not work the same as in method #1.

Even though i have a form_alter for this entity form which does this:

$form['reservation_repeat_until']['#states'] = array(
    'visible' => array(
      ':input[name="reservation_repeat_type"]' => array('!value' => '0'),
    ),
  );

and i have verified that this code is hit in both case 1 and 2, in case 2 the state properties are not adhered to (i.e. the dependent field is always visible).

There is also another issue with case 2 that the footer of the modal is not styled correctly.

My guess would be there are modal or state js and/or css files which do not get attached when the form is called using the entity.form_builder service.

Steps to reproduce

Proposed resolution

Remaining tasks

Test to determine if this problem still exists.

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Closed: cannot reproduce

Version

11.0 πŸ”₯

Component
FormΒ  β†’

Last updated less than a minute ago

Created by

πŸ‡¨πŸ‡¦Canada liquidcms

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.

  • πŸ‡³πŸ‡ΏNew Zealand quietone New Zealand

    @liquidcms Thank you for reporting this problem. We rely on issue reports like this one to resolve bugs and improve Drupal core.

    There has been no discuss about this since this issue was opened 5 years ago. Has it been fixed in the mean time.

    Thanks!

  • Status changed to Closed: cannot reproduce 4 months ago
  • πŸ‡¦πŸ‡ΊAustralia pameeela

    Closing this in light of no further updates.

Production build 0.71.5 2024