FormAlter.php in Ajax form entity 8
File
src/Form/FormAlter.php
View source
<?php
namespace Drupal\ajax_form_entity\Form;
use Drupal\Core\Ajax\AfterCommand;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\BeforeCommand;
use Drupal\Core\Ajax\RemoveCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Form\FormStateInterface;
class FormAlter {
public static function ajaxFormEntityFormSubmit(array $form, FormStateInterface $form_state) {
$build_info = $form_state
->getBuildInfo();
if (empty($build_info['callback_object'])) {
return;
}
$callback =& $build_info['callback_object'];
$entity = $callback
->getEntity();
$entity_type = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
$configurations = $form_state
->getValue('ajax_form_entity');
if ($configurations['reload'] != 'reload_entity') {
$new_entity = \Drupal::entityTypeManager()
->getStorage($entity_type)
->create([
'type' => $bundle,
]);
$callback
->setEntity($new_entity);
}
$input = $form_state
->getUserInput();
$clean_keys = $form_state
->getCleanValueKeys();
$clean_keys[] = 'ajax_page_state';
foreach ($input as $key => $item) {
if (!in_array($key, $clean_keys) && substr($key, 0, 1) !== '_') {
unset($input[$key]);
}
}
$input['entity'] = $entity;
$form_state
->setUserInput($input);
$form_state
->setRebuild();
$form_state
->setStorage([]);
}
public static function ajaxFormEntityCallback(array &$form, FormStateInterface $form_state) {
if ($form_state
->hasAnyErrors()) {
return $form;
}
else {
$user_inputs = $form_state
->getUserInput();
if (empty($user_inputs['entity'])) {
return $form;
}
$entity = $user_inputs['entity'];
$entity_type = $entity
->getEntityTypeId();
$configurations = $form_state
->getValue('ajax_form_entity');
$response = new AjaxResponse();
if ($entity_type == 'node' && $configurations['view_mode'] == 'full') {
$title = $entity
->getTitle();
$entity
->setTitle('');
$replace = '<h1>' . $title . '</h1>';
$response
->addCommand(new ReplaceCommand('h1', $replace));
}
$status_messages = [
'#type' => 'status_messages',
];
$message = [
'#markup' => \Drupal::service('renderer')
->renderRoot($status_messages),
];
$response
->addCommand(new RemoveCommand('.messages'));
if ($configurations['send_content']) {
if ($configurations['show_message']) {
$output['message'] = $message;
}
$output['entity']['#markup'] = render(\Drupal::entityTypeManager()
->getViewBuilder($entity_type)
->view($entity, $configurations['view_mode']));
if ($configurations['selector_type'] === 'prepend') {
$response
->addCommand(new BeforeCommand($configurations['content_selector'], $output));
}
elseif ($configurations['selector_type'] === 'append') {
$response
->addCommand(new AfterCommand($configurations['content_selector'], $output));
}
elseif ($configurations['content_selector']) {
$response
->addCommand(new ReplaceCommand($configurations['content_selector'], $output));
}
}
elseif ($configurations['show_message']) {
$response
->addCommand(new ReplaceCommand($configurations['content_selector'], $message));
}
if ($configurations['reload']) {
$response
->addCommand(new ReplaceCommand($configurations['form_selector'], $form));
}
else {
$response
->addCommand(new RemoveCommand($configurations['form_selector']));
}
$response
->addCommand(new RemoveCommand('.ui-dialog'));
$response
->addCommand(new RemoveCommand('.ui-widget-overlay'));
return $response;
}
}
}