GeysirModalParagraphForm.php in Geysir 8
File
src/Form/GeysirModalParagraphForm.php
View source
<?php
namespace Drupal\geysir\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Form\FormStateInterface;
use Drupal\geysir\Ajax\GeysirCloseModalDialogCommand;
use Drupal\geysir\Ajax\GeysirReattachBehaviors;
class GeysirModalParagraphForm extends GeysirParagraphForm {
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
unset($form['#title']);
return $form;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['#prefix'] = '<div id="geysir-modal-form">';
$form['#suffix'] = '</div>';
$form['#token'] = FALSE;
$submit =& $form['actions']['submit'];
$submit['#ajax'] = [
'callback' => '::ajaxSave',
'event' => 'click',
'progress' => [
'type' => 'throbber',
'message' => NULL,
],
];
return $form;
}
public function ajaxSave(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
if ($form_state
->getErrors()) {
unset($form['#prefix']);
unset($form['#suffix']);
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
$response
->addCommand(new HtmlCommand('#geysir-modal-form', $form));
}
else {
$route_match = $this
->getRouteMatch();
$parent_entity_type = $route_match
->getParameter('parent_entity_type');
$temporary_data = $form_state
->getTemporary();
$parent_entity_revision = isset($temporary_data['parent_entity_revision']) ? $temporary_data['parent_entity_revision'] : $route_match
->getParameter('parent_entity_revision');
$field_name = $route_match
->getParameter('field');
$field_wrapper_id = $route_match
->getParameter('field_wrapper_id');
$parent_entity_revision = $this
->getParentRevisionOrParent($parent_entity_type, $parent_entity_revision);
$response
->addCommand(new ReplaceCommand('[data-geysir-field-paragraph-field-wrapper=' . $field_wrapper_id . ']', $parent_entity_revision
->get($field_name)
->view('default')));
$response
->addCommand(new InvokeCommand('[data-geysir-field-paragraph-field-wrapper=' . $field_wrapper_id . ']', 'change'));
$response
->addCommand(new GeysirCloseModalDialogCommand());
$response
->addCommand(new GeysirReattachBehaviors());
}
return $response;
}
}