public function ThemeSwitcherRuleForm::form in Theme Switcher Rules 8
Gets the actual form array to be built.
Overrides EntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- src/
Form/ ThemeSwitcherRuleForm.php, line 105
Class
- ThemeSwitcherRuleForm
- Form handler for the ThemeSwitcherRule add and edit forms.
Namespace
Drupal\theme_switcher\FormCode
public function form(array $form, FormStateInterface $form_state) {
$available_contexts = $this->contextRepository
->getAvailableContexts();
$form_state
->setTemporaryValue('gathered_contexts', $available_contexts);
/** @var \Drupal\theme_switcher\Entity\ThemeSwitcherRule $entity */
$entity = $this->entity;
$form['#tree'] = TRUE;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Theme Switcher Rule'),
'#maxlength' => 255,
'#default_value' => $entity
->label(),
'#description' => $this
->t('The human-readable name is shown in the Theme Switcher list.'),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $entity
->id(),
'#machine_name' => [
'source' => [
'label',
],
'exists' => [
$this,
'exist',
],
],
'#disabled' => !$entity
->isNew(),
];
$form['status'] = [
'#type' => 'radios',
'#title' => $this
->t('Theme Switcher Rule status'),
'#options' => [
1 => $this
->t('Active'),
0 => $this
->t('Inactive'),
],
'#default_value' => (int) $entity
->status(),
'#description' => $this
->t('The Theme Switcher Rule will only work if the active option is set.'),
];
$form['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight'),
'#access' => FALSE,
'#default_value' => $entity
->getWeight(),
'#description' => $this
->t('The sort order for this record. Lower values display first.'),
];
$form['theme'] = [
'#type' => 'select',
'#title' => $this
->t('Theme'),
'#description' => $this
->t('The theme to apply in all pages that meet the conditions below.'),
'#options' => $this
->getThemeOptions(),
'#default_value' => $entity
->getTheme() ?? '',
'#required' => TRUE,
];
$form['admin_theme'] = [
'#type' => 'select',
'#title' => $this
->t('Admin Theme'),
'#description' => $this
->t('The theme to apply in just the admin pages that meet the conditions below.'),
'#options' => $this
->getThemeOptions(),
'#default_value' => $entity
->getAdminTheme() ?? '',
];
// Build the visibility UI form and follow this
// https://www.drupal.org/node/2284687
$form['visibility'] = [
'visibility_tabs' => [
'#type' => 'vertical_tabs',
'#title' => $this
->t('Conditions'),
'#parents' => [
'visibility_tabs',
],
],
];
$visibility = $entity
->getVisibility();
$definitions = $this->conditionPluginManager
->getFilteredDefinitions('theme_switcher_ui', $form_state
->getTemporaryValue('gathered_contexts'), [
'theme_switcher_rule' => $entity,
]);
// Allows modules to alter the number the conditions.
$this->moduleHandler
->alter('available_conditions', $definitions);
foreach ($definitions as $condition_id => $definition) {
/** @var \Drupal\Core\Condition\ConditionInterface $condition */
$condition = $this->conditionPluginManager
->createInstance($condition_id, $visibility[$condition_id] ?? []);
$form_state
->set([
'conditions',
$condition_id,
], $condition);
$condition_form = $condition
->buildConfigurationForm([], $form_state);
$form['visibility'][$condition_id] = [
'#type' => 'details',
'#title' => $condition
->getPluginDefinition()['label'],
'#group' => 'visibility_tabs',
] + $condition_form;
}
return $form;
}