class AdminExclude in Image Lazyloader 8
Class AdminExclude.
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\lazyloader\Form\AdminExclude
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of AdminExclude
1 string reference to 'AdminExclude'
File
- src/
Form/ AdminExclude.php, line 16
Namespace
Drupal\lazyloader\FormView source
class AdminExclude extends ConfigFormBase {
/**
* The Lazyloader exclude configuration entity.
*
* @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig
*/
protected $configuration;
/**
* The Plugin Manager.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $conditionManager;
/**
* Constructs a \Drupal\lazyloader\Form\LazyLoaderAdminConfigure object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Component\Plugin\PluginManagerInterface $condition_manager
* The condition manager.
*/
public function __construct(ConfigFactoryInterface $config_factory, PluginManagerInterface $condition_manager) {
parent::__construct($config_factory);
$this->configuration = $this
->config('lazyloader.exclude');
$this->conditionManager = $condition_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.condition'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'lazyloader_admin_exclude';
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this
->config('lazyloader.exclude')
->set('filenames', $form_state
->get('filenames'));
// Submit visibility condition settings.
$visibility = $this
->config('lazyloader.exclude')
->get('visibility') ?: [];
foreach ($form_state
->getValue('visibility') as $condition_id => $values) {
// Allow the condition to submit the form.
$condition = $form_state
->get([
'conditions',
$condition_id,
]);
$condition_values = (new FormState())
->setValues($values);
$condition
->submitConfigurationForm($form, $condition_values);
if ($condition instanceof ContextAwarePluginInterface) {
$context_mapping = isset($values['context_mapping']) ? $values['context_mapping'] : [];
$condition
->setContextMapping($context_mapping);
}
// Update the original form values.
$condition_configuration = $condition
->getConfiguration();
$form_state
->setValue([
'visibility',
$condition_id,
], $condition_configuration);
// Update the visibility conditions on the block.
$visibility[$condition_id] = $condition_configuration;
}
$this
->config('lazyloader.exclude')
->set('visibility', $visibility)
->save();
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'lazyloader.exclude',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = [];
$form['visibility'] = [
'#tree' => TRUE,
];
$form['visibility'] += $this
->buildVisibilityInterface([], $form_state);
$form['filenames'] = [
'#type' => 'textarea',
'#title' => $this
->t('Exclude images by filename'),
'#default_value' => $this->configuration
->get('filenames'),
'#description' => $this
->t('Any filenames entered in this field will be excluded from lazyloading. Enter one filename per line.'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$this
->validateVisibility($form, $form_state);
}
/**
* Helper function for building the visibility UI form.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The form array with the visibility UI added in.
*/
protected function buildVisibilityInterface(array $form, FormStateInterface $form_state) {
$form['visibility_tabs'] = [
'#type' => 'vertical_tabs',
'#title' => $this
->t('Visibility'),
'#parents' => [
'visibility_tabs',
],
'#attached' => [
'library' => [
'block/drupal.block',
],
],
];
// @todo Allow list of conditions to be configured in
// https://www.drupal.org/node/2284687.
$visibility = $this
->config('lazyloader.exclude')
->get('visibility');
foreach ($this->conditionManager
->getDefinitions() as $condition_id => $definition) {
/** @var \Drupal\Core\Condition\ConditionInterface $condition */
$condition = $this->conditionManager
->createInstance($condition_id, isset($visibility[$condition_id]) ? $visibility[$condition_id] : []);
$form_state
->set([
'conditions',
$condition_id,
], $condition);
$condition_form = $condition
->buildConfigurationForm([], $form_state);
$condition_form['#type'] = 'details';
$condition_form['#title'] = $condition
->getPluginDefinition()['label'];
$condition_form['#group'] = 'visibility_tabs';
$form[$condition_id] = $condition_form;
}
if (isset($form['node_type'])) {
$form['node_type']['#title'] = $this
->t('Content types');
$form['node_type']['bundles']['#title'] = $this
->t('Content types');
$form['node_type']['negate']['#type'] = 'value';
$form['node_type']['negate']['#title_display'] = 'invisible';
$form['node_type']['negate']['#value'] = $form['node_type']['negate']['#default_value'];
}
if (isset($form['user_role'])) {
$form['user_role']['#title'] = $this
->t('Roles');
unset($form['user_role']['roles']['#description']);
$form['user_role']['negate']['#type'] = 'value';
$form['user_role']['negate']['#value'] = $form['user_role']['negate']['#default_value'];
}
if (isset($form['request_path'])) {
$form['request_path']['#title'] = $this
->t('Pages');
$form['request_path']['negate']['#type'] = 'radios';
$form['request_path']['negate']['#default_value'] = (int) $form['request_path']['negate']['#default_value'];
$form['request_path']['negate']['#title_display'] = 'invisible';
$form['request_path']['negate']['#options'] = [
$this
->t('Show for the listed pages'),
$this
->t('Hide for the listed pages'),
];
}
if (isset($form['language'])) {
$form['language']['negate']['#type'] = 'value';
$form['language']['negate']['#value'] = $form['language']['negate']['#default_value'];
}
return $form;
}
/**
* Helper function to independently validate the visibility UI.
*
* @param array $form
* A nested array form elements comprising the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function validateVisibility(array $form, FormStateInterface $form_state) {
// 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)) {
$values['negate'] = (bool) $values['negate'];
}
// Allow the condition to validate the form.
$condition = $form_state
->get([
'conditions',
$condition_id,
]);
$condition_values = (new FormState())
->setValues($values);
$condition
->validateConfigurationForm($form, $condition_values);
// Update the original form values.
$form_state
->setValue([
'visibility',
$condition_id,
], $condition_values
->getValues());
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AdminExclude:: |
protected | property | The Plugin Manager. | |
AdminExclude:: |
protected | property | The Lazyloader exclude configuration entity. | |
AdminExclude:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
AdminExclude:: |
protected | function | Helper function for building the visibility UI form. | |
AdminExclude:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
AdminExclude:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
AdminExclude:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
AdminExclude:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
AdminExclude:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
AdminExclude:: |
protected | function | Helper function to independently validate the visibility UI. | |
AdminExclude:: |
public | function |
Constructs a \Drupal\lazyloader\Form\LazyLoaderAdminConfigure 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. | |
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. |