1 {% extends "base/class.php.twig" %}
4 \Drupal\{{ module }}\Plugin\Condition\{{ class_name }}.
7 {% block namespace_class %}
8 namespace Drupal\{{ module }}\Plugin\Condition;
12 use Drupal\Core\Condition\ConditionPluginBase;
13 use Drupal\Core\Form\FormStateInterface;
14 use Drupal\Core\Plugin\Context\ContextDefinition;
17 {% block class_declaration %}
19 * Provides a '{{ label }}' condition to enable a condition based in module selected status.
22 * id = "{{ plugin_id }}",
23 * label = @Translation("{{ label }}"),
25 * "{{ context_id }}" = @ContextDefinition("{{ context_definition_id }}", required = {{ context_definition_required }} , label = @Translation("{{ context_definition_label }}"))
30 class {{ class_name }} extends ConditionPluginBase {% endblock %}
31 {% block class_methods %}
35 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition)
45 * Creates a new {{ class_name }} object.
47 * @param array $configuration
48 * The plugin configuration, i.e. an array with configuration values keyed
49 * by configuration option name. The special key 'context' may be used to
50 * initialize the defined contexts by setting it to an array of context
51 * values keyed by context names.
52 * @param string $plugin_id
53 * The plugin_id for the plugin instance.
54 * @param mixed $plugin_definition
55 * The plugin implementation definition.
57 public function __construct(array $configuration, $plugin_id, $plugin_definition) {
58 parent::__construct($configuration, $plugin_id, $plugin_definition);
64 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
65 // Sort all modules by their names.
66 $modules = system_rebuild_module_data();
67 uasort($modules, 'system_sort_modules_by_info_name');
69 $options = [NULL => t('Select a module')];
70 foreach($modules as $module_id => $module) {
71 $options[$module_id] = $module->info['name'];
76 '#title' => $this->t('Select a module to validate'),
77 '#default_value' => $this->configuration['module'],
78 '#options' => $options,
79 '#description' => $this->t('Module selected status will be use to evaluate condition.'),
82 return parent::buildConfigurationForm($form, $form_state);
88 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
89 $this->configuration['module'] = $form_state->getValue('module');
90 parent::submitConfigurationForm($form, $form_state);
96 public function defaultConfiguration() {
97 return ['module' => ''] + parent::defaultConfiguration();
101 * Evaluates the condition and returns TRUE or FALSE accordingly.
104 * TRUE if the condition has been met, FALSE otherwise.
106 public function evaluate() {
107 if (empty($this->configuration['module']) && !$this->isNegated()) {
111 $module = $this->configuration['module'];
112 $modules = system_rebuild_module_data();
114 return $modules[$module]->status;
118 * Provides a human readable summary of the condition's configuration.
120 public function summary()
122 $module = $this->getContextValue('module');
123 $modules = system_rebuild_module_data();
125 $status = ($modules[$module]->status)?t('enabled'):t('disabled');
127 return t('The module @module is @status.', ['@module' => $module, '@status' => $status]);