Repository URL to install this package:
|
Version:
2.0.2 ▾
|
<?php
namespace Drupal\custom_forms\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseDialogCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;
/**
* Class CustomFormItemFormBase
*
* Base class for the various forms used by custom form items, such as the
* settings and states forms.
*
* @package Drupal\custom_forms\Form
*/
abstract class CustomFormItemFormBase extends FormBase {
/**
* Form constructor.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The form structure.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$request = \Drupal::request();
$attributes = $request->attributes;
$customForm = $attributes->get('custom_form');
$settings = \Drupal::config('custom_forms.settings');
// Add the basic form actions.
$form['actions'] = [
'#type' => 'actions',
'#weight' => 99,
];
$form['actions']['save'] = [
'#type' => 'submit',
'#op' => 'submit',
'#value' => $this->t('Save'),
'#ajax' => [
'callback' => [$this, 'ajaxSubmitForm']
],
'#attributes' => [
'class' => [
'button',
'button-action',
'button--primary',
],
],
];
$form['actions']['cancel'] = [
'#type' => 'submit',
'#op' => 'cancel',
'#value' => $this->t('Cancel'),
'#ajax' => [
'callback' => [$this, 'ajaxCancelForm']
],
'#attributes' => [
'class' => [
'button',
'button-action',
'button--secondary',
],
],
'#limit_validation_errors' => [],
];
// If the request is not XHR, we assume it's being rendered normally and
// won't need the ajax submit and change the cancel button to a simple link.
if (!$request->isXmlHttpRequest()) {
unset($form['actions']['save']['#ajax']);
$cancel_url = Url::fromRoute('entity.custom_form.fields_form', ['custom_form' => $customForm->id()], ['absolute' => FALSE]);
$form['actions']['cancel'] = [
'#type' => 'link',
'#url' => $cancel_url,
'#title' => $this->t('Cancel'),
'#attributes' => [
'class' => [
'button',
'button-action',
'button--secondary',
],
],
'#limit_validation_errors' => [],
];
} else {
// If the form is rendered via XHR, we add a warning message in case
// the user has unsaved sorting/nesting changes to the field list.
switch ($settings->get('off_screen_field_settings')) {
case 'rebuild':
$warning_message = $this->t('If you have any unsaved changes to the field list, these changes will be lost if this form is saved. <br>Unsaved chances could be anything from the order of the fields/groups to the nesting of them.');
break;
case 'reload':
$warning_message = $this->t('When saving this form, the page itself will reload to update the changes. If the reload is interrupted the page might not work until a manual refresh is done.');
break;
}
// If there is a warning message, we display it.
if (!empty($warning_message)) {
$form['warning'] = [
'#theme' => 'status_messages',
'#weight' => 100,
'#message_list' => [
'warning' => [
$warning_message
]
],
'#status_headings' => [
'status' => $this->t('Status message'),
'error' => $this->t('Error message'),
'warning' => $this->t('Warning message')
]
];
}
}
return $form;
}
/**
* Handle submission through ajax.
*
* @param array $form
* The form render array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
* @param \Symfony\Component\HttpFoundation\Request $request
* The HTTP request.
*
* @return AjaxResponse
*/
abstract function ajaxSubmitForm(array &$form, FormStateInterface $form_state, Request $request);
/**
* Handle cancelling the ajax form.
*
* @param array $form
* The form render array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
* @param \Symfony\Component\HttpFoundation\Request $request
* The HTTP request.
*
* @return \Drupal\Core\Ajax\AjaxResponse
*/
public function ajaxCancelForm(array &$form, FormStateInterface $form_state, Request $request) {
$response = new AjaxResponse();
$response->addCommand(new CloseDialogCommand('#drupal-off-canvas'));
return $response;
}
}