View source
<?php
namespace Drupal\lazyloader\Form;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AdminExclude extends ConfigFormBase {
protected $configuration;
protected $conditionManager;
public function __construct(ConfigFactoryInterface $config_factory, PluginManagerInterface $condition_manager) {
parent::__construct($config_factory);
$this->configuration = $this
->config('lazyloader.exclude');
$this->conditionManager = $condition_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.condition'));
}
public function getFormId() {
return 'lazyloader_admin_exclude';
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this
->config('lazyloader.exclude')
->set('filenames', $form_state
->get('filenames'));
$visibility = $this
->config('lazyloader.exclude')
->get('visibility') ?: [];
foreach ($form_state
->getValue('visibility') as $condition_id => $values) {
$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);
}
$condition_configuration = $condition
->getConfiguration();
$form_state
->setValue([
'visibility',
$condition_id,
], $condition_configuration);
$visibility[$condition_id] = $condition_configuration;
}
$this
->config('lazyloader.exclude')
->set('visibility', $visibility)
->save();
}
protected function getEditableConfigNames() {
return [
'lazyloader.exclude',
];
}
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);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$this
->validateVisibility($form, $form_state);
}
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',
],
],
];
$visibility = $this
->config('lazyloader.exclude')
->get('visibility');
foreach ($this->conditionManager
->getDefinitions() as $condition_id => $definition) {
$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;
}
protected function validateVisibility(array $form, FormStateInterface $form_state) {
foreach ($form_state
->getValue('visibility') as $condition_id => $values) {
if (array_key_exists('negate', $values)) {
$values['negate'] = (bool) $values['negate'];
}
$condition = $form_state
->get([
'conditions',
$condition_id,
]);
$condition_values = (new FormState())
->setValues($values);
$condition
->validateConfigurationForm($form, $condition_values);
$form_state
->setValue([
'visibility',
$condition_id,
], $condition_values
->getValues());
}
}
}