class ColorsSettingsForm in Colors 8
Configure color settings.
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\colors\Form\ColorsSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of ColorsSettingsForm
1 string reference to 'ColorsSettingsForm'
File
- src/
Form/ ColorsSettingsForm.php, line 15
Namespace
Drupal\colors\FormView source
class ColorsSettingsForm extends ConfigFormBase {
/**
* An array of configuration names that should be editable.
*
* @var array
*/
protected $editableConfig = [];
/**
* Stores the Colors Scheme plugins.
*
* @var \Drupal\colors\Plugin\ColorsSchemePluginCollection
*/
protected $pluginCollection;
/**
* Constructs a ColorsSettingsForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* @param \Drupal\Component\Plugin\PluginManagerInterface $manager
*/
public function __construct(ConfigFactoryInterface $config_factory, PluginManagerInterface $manager) {
parent::__construct($config_factory);
$this->pluginCollection = new ColorsSchemePluginCollection($manager, $this);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.colors'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'colors_ui_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return $this->editableConfig;
}
/**
*
* @return \Drupal\colors\Plugin\ColorsSchemePluginCollection;|\Drupal\colors\Plugin\ColorsSchemeinterface[]
*/
public function getPlugins() {
return $this->pluginCollection;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Color picker.
$form = colors_load_colorpicker();
$form['#attached'] = array(
'library' => array(
'colors/colors',
),
);
$entity = $this
->getEntityFromRoute(\Drupal::routeMatch());
$plugins = \Drupal::service('plugin.manager.colors')
->getDefinitions();
$plugin = !empty($entity) ? $plugins[$entity] : NULL;
// Global settings tab.
if (!$plugin) {
$this
->getDefaultFormElements($form);
}
else {
$this
->getFormElements($form, $entity);
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$plugin = $form_state
->getValue('plugin');
if ($plugin) {
foreach ($form_state
->getValue('palette') as $id => $palette) {
\Drupal::configFactory()
->getEditable($id)
->set('type', $plugin['id'])
->set('label', $plugin['title'])
->set('enabled', $form_state
->getValue($form_state
->getValue('id')))
->set('palette', $palette)
->set('weight', $plugin['weight'])
->save();
}
// Update settings.
$config = \Drupal::configFactory()
->getEditable('colors.settings');
$order = $config
->get('order');
if ($form_state
->getValue($form_state
->getValue('id'))) {
// Enabled.
$order[$plugin['id']] = 0;
}
else {
// Disabled.
unset($order[$plugin['id']]);
}
$config
->set('order', $order)
->save();
}
else {
$process = $form_state
->getValue('process_order');
\Drupal::configFactory()
->getEditable('colors.settings')
->set('override', $process['enabled'])
->set('order', $process['enabled'] ? colors_get_weights($form_state) : [])
->set('palette', $form_state
->getValue('palette'))
->save();
}
}
/**
* Get form element for default settings.
*
* @param array $form
*/
protected function getDefaultFormElements(&$form) {
$config = colors_get_info();
$palette = colors_get_palette($config);
$names = $config
->get('fields');
$form['palette']['#tree'] = TRUE;
foreach ($palette as $name => $value) {
if (isset($names[$name])) {
$form['palette'][$name] = array(
'#type' => 'textfield',
'#title' => $names[$name],
'#value_callback' => 'color_palette_color_value',
'#default_value' => $value,
'#size' => 8,
'#attributes' => array(
'class' => array(
'colorpicker-input',
),
),
);
}
}
$form['process_order'] = [
'#tree' => TRUE,
'info' => [
'#type' => 'item',
'#title' => t('Process order'),
],
'enabled' => [
'#type' => 'checkbox',
'#title' => t('Change the CSS processing order.'),
'#default_value' => \Drupal::configFactory()
->getEditable('colors.settings')
->get('override'),
'#description' => t('Color order is cascading, CSS from modules at the bottom will override the top.'),
],
];
$form['order_settings'] = [
'#type' => 'container',
'#states' => [
'visible' => [
':input[name="process_order[enabled]"]' => [
'checked' => TRUE,
],
],
],
];
$form['order_settings']['entities'] = [
'#type' => 'table',
'#header' => [
$this
->t('Name'),
$this
->t('Weight'),
],
'#empty' => $this
->t('No entities available.'),
'#attributes' => [
'id' => 'colors-entities',
],
];
$configs = \Drupal::configFactory()
->getEditable('colors.settings')
->get('order');
$configs = is_array($configs) ? $configs : [];
foreach ($configs as $config_id => $weight) {
$plugin = \Drupal::service('plugin.manager.colors')
->getDefinition($config_id);
$form['order_settings']['entities'][$config_id]['entity'] = [
'#plain_text' => $plugin['title'],
];
$form['order_settings']['entities'][$config_id]['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight for added entity'),
'#title_display' => 'invisible',
'#default_value' => $weight,
'#attributes' => [
'class' => [
'color-weight',
],
],
];
$form['order_settings']['entities'][$config_id]['#attributes']['class'][] = 'draggable';
$form['order_settings']['entities']['#tabledrag'][] = [
'action' => 'order',
'relationship' => 'sibling',
'group' => 'color-weight',
];
}
}
/**
* Get settings form elements.
*
* @param array $form
* @param string $entity
*/
protected function getFormElements(&$form, $entity) {
$plugin = \Drupal::service('plugin.manager.colors')
->getDefinition($entity);
if (!$plugin['id']) {
return;
}
$form[$plugin['id']] = [
'#type' => 'item',
'#title' => t('@title colors', [
'@title' => $plugin['title'],
]),
'#description' => $plugin['description'],
];
$multiple = !empty($plugin['multiple']);
$repeat = !empty($multiple) ? $plugin['multiple']() : [
NULL => NULL,
];
$enabled = colors_get_enabled($entity) ? TRUE : FALSE;
foreach ($repeat as $id => $repeat_value) {
$config_name = "colors-{$entity}";
$config_name .= !empty($multiple) ? "-{$id}" : '';
$element = [
'#type' => 'details',
'#title' => !empty($multiple) ? $repeat_value
->label() : t('@title colors', array(
'@title' => $plugin['title'],
)),
'#collapsible' => TRUE,
'#open' => $enabled,
'#attributes' => [
'class' => [
'overflow-hidden',
],
],
'#prefix' => '<div class="clearfix">',
'#suffix' => '</div>',
];
$element[$config_name] = [
'#type' => 'checkbox',
'#title' => $plugin['label'],
'#default_value' => $enabled,
];
$element['plugin'] = [
'#type' => 'value',
'#value' => $plugin,
];
$element['id'] = [
'#type' => 'value',
'#value' => $config_name,
];
// @todo: don't display vocab fieldset if there's no terms yet.
foreach ($plugin['callback']($id) as $key => $label) {
//$config_id = "colors.$entity.$key";
$config_id = str_replace('-', '.', $config_name) . ".{$key}";
$element['palette'][$config_id] = [
'#type' => 'details',
'#title' => t($label),
'#collapsible' => TRUE,
'#open' => TRUE,
'#attributes' => [
'class' => [
'views-left-25',
],
],
'#states' => [
'visible' => [
':input[name="' . $config_name . '"]' => [
'checked' => TRUE,
],
],
],
];
// Get configs.
$default_config = colors_get_info();
$config = colors_get_info($config_id);
$config = $config
->isNew() ? $default_config : $config;
// Get palette.
$palette = colors_get_palette($config);
$names = $default_config
->get('fields');
$element['palette']['#tree'] = TRUE;
foreach ($palette as $name => $value) {
if (isset($names[$name])) {
$element['palette'][$config_id][$name] = [
'#type' => 'textfield',
'#title' => $names[$name],
'#value_callback' => 'color_palette_color_value',
'#default_value' => $value,
'#maxlength' => 7,
'#size' => 7,
'#attributes' => [
'class' => [
'colorpicker-input',
],
],
'#states' => [
'visible' => [
':input[name="' . $config_name . '"]' => [
'checked' => TRUE,
],
],
],
];
}
}
}
$element_key = !empty($multiple) ? $id : 'details';
$form[$element_key] = $element;
}
}
// @todo: service
public function getEntityFromRoute($route) {
$part = '';
$route_name = $route
->getRouteName();
if ($route_name === 'colors.ui_settings_entity.users') {
$part .= 'user_current';
}
elseif ($route_name !== 'colors.ui_settings') {
$parts = explode('/', $route
->getRouteObject()
->getPath());
$part .= end($parts);
}
return $part;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ColorsSettingsForm:: |
protected | property | An array of configuration names that should be editable. | |
ColorsSettingsForm:: |
protected | property | Stores the Colors Scheme plugins. | |
ColorsSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
ColorsSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
ColorsSettingsForm:: |
protected | function | Get form element for default settings. | |
ColorsSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
ColorsSettingsForm:: |
public | function | ||
ColorsSettingsForm:: |
protected | function | Get settings form elements. | |
ColorsSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ColorsSettingsForm:: |
public | function | ||
ColorsSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
ColorsSettingsForm:: |
public | function |
Constructs a ColorsSettingsForm object. 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. |