public function AutosaveFormSettingsForm::buildForm in Autosave Form 8
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides ConfigFormBase::buildForm
File
- src/
Form/ AutosaveFormSettingsForm.php, line 73
Class
- AutosaveFormSettingsForm
- Configure autosave form settings for this site.
Namespace
Drupal\autosave_form\FormCode
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);
}