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