View source
<?php
namespace Drupal\panopoly_search\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PanopolySearchCondition extends ConditionPluginBase implements ContainerFactoryPluginInterface {
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'));
}
public function defaultConfiguration() {
return [
'module' => '',
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['module'] = [
'#type' => 'select',
'#options' => $this
->getOptions(),
'#empty_value' => '',
'#title' => $this
->t('Search backend'),
'#default_value' => $this->configuration['module'],
'#description' => $this
->t('The Panopoly Search backend that is enabled.'),
];
return parent::buildConfigurationForm($form, $form_state);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['module'] = $form_state
->getValue('module');
parent::submitConfigurationForm($form, $form_state);
}
protected function getOptions() {
return [
'none' => $this
->t('None'),
'panopoly_search_db' => $this
->t('Database'),
'panopoly_search_solr' => $this
->t('SOLR'),
];
}
public function evaluate() {
$module = $this->configuration['module'];
if (empty($module)) {
return $this
->isNegated() ? FALSE : TRUE;
}
return $this->moduleHandler
->moduleExists($module);
}
public function summary() {
$module = $this->configuration['module'];
$options = $this
->getOptions();
if (!empty($module)) {
if (!$this
->isNegated()) {
return $this
->t('The "@backend" Panopoly Search backend is enabled', [
'@backend' => $options[$module],
]);
}
else {
return $this
->t('The "@backend" Panopoly Search backend is disabled', [
'@backend' => $options[$module],
]);
}
}
else {
return $this
->t('Always evaluates to true because no backend is selected');
}
}
}