View source
<?php
namespace Drupal\google_tag;
use Drupal\Core\Condition\ConditionInterface;
use Drupal\Core\Executable\ExecutableManagerInterface;
use Drupal\Core\Executable\ExecutablePluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformStateInterface;
use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
abstract class ConditionBase extends ExecutablePluginBase implements ConditionInterface {
use ContextAwarePluginAssignmentTrait;
protected $executableManager;
protected $toggle;
protected $list;
protected $singular;
protected $plural;
protected $options = [];
protected $values = [];
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->setConfiguration($configuration);
}
public function getConfiguration() {
return [
'id' => $this
->getPluginId(),
] + $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = $configuration + $this
->defaultConfiguration();
return $this;
}
public function setExecutableManager(ExecutableManagerInterface $executableManager) {
$this->executableManager = $executableManager;
return $this;
}
public function defaultConfiguration() {
return [
$this->toggle => GOOGLE_TAG_EXCLUDE_LISTED,
$this->list => [],
];
}
public function isNegated() {
return $this->configuration[$this->toggle] == GOOGLE_TAG_EXCLUDE_LISTED;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
if ($form_state instanceof SubformStateInterface) {
$form_state = $form_state
->getCompleteFormState();
}
$contexts = $form_state
->getTemporaryValue('gathered_contexts') ?: [];
$form[$this->toggle] = [
'#type' => 'radios',
'#title' => $this
->specialT('Insert snippet for specific @plural'),
'#options' => [
GOOGLE_TAG_EXCLUDE_LISTED => $this
->specialT('All @plural except the selected @plural'),
GOOGLE_TAG_INCLUDE_LISTED => $this
->specialT('Only the selected @plural'),
],
'#default_value' => $this->configuration[$this->toggle],
];
$form[$this->list] = [
'#type' => 'checkboxes',
'#title' => $this
->specialT('Selected @plural'),
'#options' => $this->options,
'#default_value' => $this->configuration[$this->list],
];
$form['context_mapping'] = $this
->addContextAssignmentElement($this, $contexts);
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration[$this->toggle] = $form_state
->getValue($this->toggle);
$this->configuration[$this->list] = array_filter($form_state
->getValue($this->list));
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->hasValue('context_mapping')) {
$this
->setContextMapping($form_state
->getValue('context_mapping'));
}
}
public function execute() {
return $this->executableManager
->execute($this);
}
public function evaluate() {
$toggle = $this->configuration[$this->toggle];
$values = $this->configuration[$this->list];
if (empty($values)) {
$satisfied = $this
->isNegated();
}
else {
$satisfied = in_array($this
->contextToEvaluate(), $values);
$satisfied = $this
->isNegated() ? !$satisfied : $satisfied;
}
return $satisfied;
}
public function calculateDependencies() {
return [];
}
public function summary() {
$string = 'The @singular is @adverb@verb "@list".';
$args = [
'@singular' => $this->singular,
'@adverb' => $this
->isNegated() ? 'not ' : '',
'@verb' => count($this->values) > 1 ? 'in' : '',
];
return $this
->t(strtr($string, $args), [
'@list' => implode(', ', $this->values),
]);
}
public function specialT($string) {
return $this
->t(strtr($string, [
'@plural' => $this->plural,
]));
}
public function contextToEvaluate() {
return '';
}
}