class ThemeSwitcherRuleForm in Theme Switcher Rules 8
Form handler for the ThemeSwitcherRule add and edit forms.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\theme_switcher\Form\ThemeSwitcherRuleForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of ThemeSwitcherRuleForm
File
- src/
Form/ ThemeSwitcherRuleForm.php, line 19
Namespace
Drupal\theme_switcher\FormView source
class ThemeSwitcherRuleForm extends EntityForm {
/**
* The Messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* The logger factory.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* The theme handler.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;
/**
* The ConditionManager for building the visibility UI.
*
* @var \Drupal\Core\Executable\ExecutableManagerInterface
*/
protected $conditionPluginManager;
/**
* The context repository service.
*
* @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
*/
protected $contextRepository;
/**
* The language manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs an SwitchThemeRuleForm object.
*
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger.
* @param \Drupal\Core\Logger\LoggerChannelInterface $logger
* The logger.
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* The theme handler.
* @param \Drupal\Core\Executable\ExecutableManagerInterface $condition_plugin_manager
* The ConditionManager for building the visibility UI.
* @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository
* The lazy context repository service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(MessengerInterface $messenger, LoggerChannelInterface $logger, ThemeHandlerInterface $theme_handler, ExecutableManagerInterface $condition_plugin_manager, ContextRepositoryInterface $context_repository, LanguageManagerInterface $language_manager) {
$this->messenger = $messenger;
$this->logger = $logger;
$this->themeHandler = $theme_handler;
$this->conditionPluginManager = $condition_plugin_manager;
$this->contextRepository = $context_repository;
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('messenger'), $container
->get('logger.factory')
->get('theme_switcher'), $container
->get('theme_handler'), $container
->get('plugin.manager.condition'), $container
->get('context.repository'), $container
->get('language_manager'));
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*
* The settings conditions context mappings is now the plugin responsibility
* so we can avoid doing it here. From 8.2 the class ConditionPluginBase do
* the job on submitConfigurationForm().
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
/** @var \Drupal\theme_switcher\Entity\ThemeSwitcherRule $entity */
$entity = $this->entity;
foreach ($form_state
->getValue('visibility') as $condition_id => $values) {
// Allow the condition to submit the form.
$condition = $form_state
->get([
'conditions',
$condition_id,
]);
$subform = SubformState::createForSubform($form['visibility'][$condition_id], $form, $form_state);
$condition
->submitConfigurationForm($form['visibility'][$condition_id], $subform);
// Update the visibility conditions on the block.
$entity
->getVisibilityConditions()
->addInstanceId($condition_id, $condition
->getConfiguration());
}
// Save the settings of the plugin.
$status = $entity
->save();
$message = $this
->t("The Theme Switcher Rule '%label' has been %op.", [
'%label' => $entity
->label(),
'%op' => $status == SAVED_NEW ? 'created' : 'updated',
]);
$this->messenger
->addStatus($message);
$this->logger
->notice($message);
$form_state
->setRedirect('theme_switcher.admin');
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
// Validate the weight.
$form_state
->setValue('weight', (int) $form_state
->getValue('weight'));
// Validate visibility condition settings.
foreach ($form_state
->getValue('visibility') as $condition_id => $values) {
// All condition plugins use 'negate' as a Boolean in their schema.
// However, certain form elements may return it as 0/1. Cast here to
// ensure the data is in the expected type.
if (array_key_exists('negate', $values)) {
$form_state
->setValue([
'visibility',
$condition_id,
'negate',
], (bool) $values['negate']);
}
// Allow the condition to validate the form.
$condition = $form_state
->get([
'conditions',
$condition_id,
]);
$subform = SubformState::createForSubform($form['visibility'][$condition_id], $form, $form_state);
$condition
->validateConfigurationForm($form['visibility'][$condition_id], $subform);
}
}
/**
* Return an array with all the themes.
*
* @return array
* An array with all the themes.
*/
protected function getThemeOptions() {
$output[''] = '- None -';
foreach ($this->themeHandler
->listInfo() as $key => $value) {
$output[$key] = $value
->getName();
}
return $output;
}
/**
* Checks whether a theme_switcher_rule exists.
*
* @param string $id
* The theme_switcher_rule machine name.
*
* @return bool
* Whether the theme_switcher_rule exists.
*/
public function exist($id) {
$entity = $this->entityTypeManager
->getStorage('theme_switcher_rule')
->getQuery()
->condition('id', $id)
->execute();
return (bool) $entity;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityFormInterface:: |
41 |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
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. | |
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:: |
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. | |
ThemeSwitcherRuleForm:: |
protected | property | The ConditionManager for building the visibility UI. | |
ThemeSwitcherRuleForm:: |
protected | property | The context repository service. | |
ThemeSwitcherRuleForm:: |
protected | property | The language manager service. | |
ThemeSwitcherRuleForm:: |
protected | property | The logger factory. | |
ThemeSwitcherRuleForm:: |
protected | property |
The Messenger service. Overrides MessengerTrait:: |
|
ThemeSwitcherRuleForm:: |
protected | property | The theme handler. | |
ThemeSwitcherRuleForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ThemeSwitcherRuleForm:: |
public | function | Checks whether a theme_switcher_rule exists. | |
ThemeSwitcherRuleForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
ThemeSwitcherRuleForm:: |
protected | function | Return an array with all the themes. | |
ThemeSwitcherRuleForm:: |
public | function |
The settings conditions context mappings is now the plugin responsibility
so we can avoid doing it here. From 8.2 the class ConditionPluginBase do
the job on submitConfigurationForm(). Overrides EntityForm:: |
|
ThemeSwitcherRuleForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
ThemeSwitcherRuleForm:: |
public | function | Constructs an SwitchThemeRuleForm object. | |
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. |