class SettingsForm in Image Effects 8
Same name and namespace in other branches
- 8.3 src/Form/SettingsForm.php \Drupal\image_effects\Form\SettingsForm
- 8.2 src/Form/SettingsForm.php \Drupal\image_effects\Form\SettingsForm
Main image_effects settings admin form.
@todo the array syntax for $form_state->getValue([...]) fails code style checking, but this is quite inconvenient. See if sniff gets adjusted or a different way to access nested keys will be available.
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\image_effects\Form\SettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SettingsForm
1 string reference to 'SettingsForm'
File
- src/
Form/ SettingsForm.php, line 21
Namespace
Drupal\image_effects\FormView source
class SettingsForm extends ConfigFormBase {
/**
* The color selector plugin manager.
*
* @var \Drupal\image_effects\Plugin\ImageEffectsPluginManager
*/
protected $colorManager;
/**
* The image selector plugin manager.
*
* @var \Drupal\image_effects\Plugin\ImageEffectsPluginManager
*/
protected $imageManager;
/**
* The font selector plugin manager.
*
* @var \Drupal\image_effects\Plugin\ImageEffectsPluginManager
*/
protected $fontManager;
/**
* Constructs the class for image_effects settings form.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\image_effects\Plugin\ImageEffectsPluginManager $color_plugin_manager
* The color selector plugin manager.
* @param \Drupal\image_effects\Plugin\ImageEffectsPluginManager $image_plugin_manager
* The image selector plugin manager.
* @param \Drupal\image_effects\Plugin\ImageEffectsPluginManager $font_plugin_manager
* The font selector plugin manager.
*/
public function __construct(ConfigFactoryInterface $config_factory, ImageEffectsPluginManager $color_plugin_manager, ImageEffectsPluginManager $image_plugin_manager, ImageEffectsPluginManager $font_plugin_manager) {
parent::__construct($config_factory);
$this->colorManager = $color_plugin_manager;
$this->imageManager = $image_plugin_manager;
$this->fontManager = $font_plugin_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.image_effects.color_selector'), $container
->get('plugin.manager.image_effects.image_selector'), $container
->get('plugin.manager.image_effects.font_selector'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'image_effects_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'image_effects.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('image_effects.settings');
$ajaxing = (bool) $form_state
->getValues();
// Color selector plugin.
$color_plugin_id = $ajaxing ? $form_state
->getValue([
'settings',
'color_selector',
'plugin_id',
]) : $config
->get('color_selector.plugin_id');
$color_plugin = $this->colorManager
->getPlugin($color_plugin_id);
if ($ajaxing && $form_state
->hasValue([
'settings',
'color_selector',
'plugin_settings',
])) {
$color_plugin
->setConfiguration($form_state
->getValue([
'settings',
'color_selector',
'plugin_settings',
]));
}
// Image selector plugin.
$image_plugin_id = $ajaxing ? $form_state
->getValue([
'settings',
'image_selector',
'plugin_id',
]) : $config
->get('image_selector.plugin_id');
$image_plugin = $this->imageManager
->getPlugin($image_plugin_id);
if ($ajaxing && $form_state
->hasValue([
'settings',
'image_selector',
'plugin_settings',
])) {
$image_plugin
->setConfiguration($form_state
->getValue([
'settings',
'image_selector',
'plugin_settings',
]));
}
// Font selector plugin.
$font_plugin_id = $ajaxing ? $form_state
->getValue([
'settings',
'font_selector',
'plugin_id',
]) : $config
->get('font_selector.plugin_id');
$font_plugin = $this->fontManager
->getPlugin($font_plugin_id);
if ($ajaxing && $form_state
->hasValue([
'settings',
'font_selector',
'plugin_settings',
])) {
$font_plugin
->setConfiguration($form_state
->getValue([
'settings',
'font_selector',
'plugin_settings',
]));
}
// AJAX messages.
$form['ajax_messages'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'image-effects-ajax-messages',
],
];
// AJAX settings.
$ajax_settings = [
'callback' => [
$this,
'processAjax',
],
];
// Main part of settings form.
$form['settings'] = [
'#type' => 'container',
'#tree' => TRUE,
'#attributes' => [
'id' => 'image-effects-settings-main',
],
];
// Color selector.
$form['settings']['color_selector'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Color selector'),
'#tree' => TRUE,
];
$form['settings']['color_selector']['plugin_id'] = [
'#type' => 'radios',
'#options' => $this->colorManager
->getPluginOptions(),
'#default_value' => $color_plugin
->getPluginId(),
'#required' => TRUE,
'#ajax' => $ajax_settings,
];
$form['settings']['color_selector']['plugin_settings'] = $color_plugin
->buildConfigurationForm([], $form_state, $ajax_settings);
// Image selector.
$form['settings']['image_selector'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Image selector'),
'#tree' => TRUE,
];
$form['settings']['image_selector']['plugin_id'] = [
'#type' => 'radios',
'#options' => $this->imageManager
->getPluginOptions(),
'#default_value' => $image_plugin
->getPluginId(),
'#required' => TRUE,
'#ajax' => $ajax_settings,
];
$form['settings']['image_selector']['plugin_settings'] = $image_plugin
->buildConfigurationForm([], $form_state, $ajax_settings);
// Font selector.
$form['settings']['font_selector'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Font selector'),
'#tree' => TRUE,
];
$form['settings']['font_selector']['plugin_id'] = [
'#type' => 'radios',
'#options' => $this->fontManager
->getPluginOptions(),
'#default_value' => $font_plugin
->getPluginId(),
'#required' => TRUE,
'#ajax' => $ajax_settings,
];
$form['settings']['font_selector']['plugin_settings'] = $font_plugin
->buildConfigurationForm([], $form_state, $ajax_settings);
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('image_effects.settings');
// Color plugin.
$color_plugin = $this->colorManager
->getPlugin($form_state
->getValue([
'settings',
'color_selector',
'plugin_id',
]));
if ($form_state
->hasValue([
'settings',
'color_selector',
'plugin_settings',
])) {
$color_plugin
->setConfiguration($form_state
->getValue([
'settings',
'color_selector',
'plugin_settings',
]));
}
$config
->set('color_selector.plugin_id', $color_plugin
->getPluginId())
->set('color_selector.plugin_settings.' . $color_plugin
->getPluginId(), $color_plugin
->getConfiguration());
// Image plugin.
$image_plugin = $this->imageManager
->getPlugin($form_state
->getValue([
'settings',
'image_selector',
'plugin_id',
]));
if ($form_state
->hasValue([
'settings',
'image_selector',
'plugin_settings',
])) {
$image_plugin
->setConfiguration($form_state
->getValue([
'settings',
'image_selector',
'plugin_settings',
]));
}
$config
->set('image_selector.plugin_id', $image_plugin
->getPluginId())
->set('image_selector.plugin_settings.' . $image_plugin
->getPluginId(), $image_plugin
->getConfiguration());
// Font plugin.
$font_plugin = $this->fontManager
->getPlugin($form_state
->getValue([
'settings',
'font_selector',
'plugin_id',
]));
if ($form_state
->hasValue([
'settings',
'font_selector',
'plugin_settings',
])) {
$font_plugin
->setConfiguration($form_state
->getValue([
'settings',
'font_selector',
'plugin_settings',
]));
}
$config
->set('font_selector.plugin_id', $font_plugin
->getPluginId())
->set('font_selector.plugin_settings.' . $font_plugin
->getPluginId(), $font_plugin
->getConfiguration());
$config
->save();
parent::submitForm($form, $form_state);
}
/**
* AJAX callback.
*/
public function processAjax($form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$status_messages = [
'#type' => 'status_messages',
];
$response
->addCommand(new HtmlCommand('#image-effects-ajax-messages', $status_messages));
$response
->addCommand(new HtmlCommand('#image-effects-settings-main', $form['settings']));
return $response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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. | |
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. | |
SettingsForm:: |
protected | property | The color selector plugin manager. | |
SettingsForm:: |
protected | property | The font selector plugin manager. | |
SettingsForm:: |
protected | property | The image selector plugin manager. | |
SettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
SettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SettingsForm:: |
public | function | AJAX callback. | |
SettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
SettingsForm:: |
public | function |
Constructs the class for image_effects settings form. Overrides ConfigFormBase:: |
|
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. |