class AutosaveFormSettingsForm in Autosave Form 8
Configure autosave form settings for this site.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\autosave_form\Form\AutosaveFormSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of AutosaveFormSettingsForm
1 string reference to 'AutosaveFormSettingsForm'
File
- src/
Form/ AutosaveFormSettingsForm.php, line 15
Namespace
Drupal\autosave_form\FormView source
class AutosaveFormSettingsForm extends ConfigFormBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity type bundle info.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* Constructs a new AutosaveFormSettingsForm.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'autosave_form_admin_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'autosave_form.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('autosave_form.settings');
$form['interval'] = [
'#type' => 'number',
'#title' => $this
->t('The interval to use for triggering autosave in milliseconds.'),
'#default_value' => $config
->get('interval'),
];
$form['only_on_form_change'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Run only on form change.') . ' ' . $this
->t('(Experimental)'),
'#description' => $this
->t('If enabled an autosave submission will only occur if the form changed since the previous autosave submission.'),
'#default_value' => $config
->get('only_on_form_change'),
];
$form['active_on'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Active on:'),
'#tree' => TRUE,
];
$form['active_on']['content_entity_forms'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Content Entity Forms'),
'#default_value' => $config
->get('active_on')['content_entity_forms'],
];
$form['active_on']['config_entity_forms'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Config Entity Forms'),
'#default_value' => $config
->get('active_on')['config_entity_forms'],
];
$form['notification'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Notification settings'),
'#description' => $this
->t('Display a simple notification every time content is saved'),
'#tree' => TRUE,
];
$form['notification']['active'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Active'),
'#default_value' => $config
->get('notification')['active'],
];
$form['notification']['message'] = [
'#type' => 'textfield',
'#title' => $this
->t('Message'),
'#default_value' => $config
->get('notification')['message'],
'#states' => [
'visible' => [
':input[name="notification[active]"]' => [
'checked' => TRUE,
],
],
],
];
$form['notification']['delay'] = [
'#type' => 'number',
'#title' => $this
->t('The duration of the notification in milliseconds.'),
'#default_value' => $config
->get('notification')['delay'],
'#states' => [
'visible' => [
':input[name="notification[active]"]' => [
'checked' => TRUE,
],
],
],
];
$allowed_content_entity_types = $config
->get('allowed_content_entity_types');
$form['allowed_content_entities'] = [
'#type' => 'details',
'#open' => !empty($allowed_content_entity_types),
'#title' => $this
->t('Allowed Content Entity Forms'),
'#description' => $this
->t('In case no entity type is selected then autosave is enabled on all entity forms, otherwise it will be enabled only on the selected ones. Selecting only the entity type will enable all corresponding bundles and selecting only a subset of the bundles will enable autosave only for those bundles and will be disabled for the others.'),
'#tree' => TRUE,
'#states' => [
'visible' => [
':input[name="active_on[content_entity_forms]"]' => [
'checked' => TRUE,
],
],
],
];
/** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
if ($entity_type instanceof ContentEntityTypeInterface) {
$entity_type_label = $entity_type
->getLabel();
$bundles_info = $this->entityTypeBundleInfo
->getBundleInfo($entity_type_id);
$allowed_bundles = !empty($allowed_content_entity_types[$entity_type_id]['bundles']) ? $allowed_content_entity_types[$entity_type_id]['bundles'] : [];
$bundles = [];
foreach ($bundles_info as $key => $bundle) {
$bundles[$key] = $bundle['label'];
}
$form['allowed_content_entities'][$entity_type_id]['active'] = [
'#type' => 'checkbox',
'#title' => $entity_type_label,
'#default_value' => isset($allowed_content_entity_types[$entity_type_id]),
];
$form['allowed_content_entities'][$entity_type_id]['bundles'] = [
'#type' => 'details',
'#open' => !empty($allowed_bundles),
'#title' => $entity_type_label . ' ' . $this
->t('bundles'),
'#states' => [
'visible' => [
':input[name="allowed_content_entities[' . $entity_type_id . '][active]"]' => [
'checked' => TRUE,
],
],
],
];
$form['allowed_content_entities'][$entity_type_id]['bundles']['selection'] = [
'#type' => 'checkboxes',
'#default_value' => $allowed_bundles,
'#options' => $bundles,
'#prefix' => '<div class="panel">',
'#suffix' => '</div>',
];
}
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Config\Config $config */
$config = $this
->config('autosave_form.settings');
$allowed_content_entity_types = [];
foreach ($form_state
->getValue('allowed_content_entities') as $entity_type_id => $data) {
if (!$data['active']) {
continue;
}
$allowed_bundles = array_filter($data['bundles']['selection']);
$allowed_content_entity_types[$entity_type_id]['bundles'] = $allowed_bundles;
}
$config
->set('interval', $form_state
->getValue('interval'))
->set('only_on_form_change', $form_state
->getValue('only_on_form_change'))
->set('active_on', $form_state
->getValue('active_on'))
->set('notification', $form_state
->getValue('notification'))
->set('allowed_content_entity_types', $allowed_content_entity_types)
->save();
parent::submitForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AutosaveFormSettingsForm:: |
protected | property | The entity type bundle info. | |
AutosaveFormSettingsForm:: |
protected | property | The entity type manager. | |
AutosaveFormSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
AutosaveFormSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
AutosaveFormSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
AutosaveFormSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
AutosaveFormSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
AutosaveFormSettingsForm:: |
public | function |
Constructs a new AutosaveFormSettingsForm. Overrides ConfigFormBase:: |
|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |