Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / templates / module / src / Plugin / Condition / condition.php.twig
1 {% extends "base/class.php.twig" %}
2
3 {% block file_path %}
4 \Drupal\{{ module }}\Plugin\Condition\{{ class_name }}.
5 {% endblock %}
6
7 {% block namespace_class %}
8 namespace Drupal\{{ module }}\Plugin\Condition;
9 {% endblock %}
10
11 {% block use_class %}
12 use Drupal\Core\Condition\ConditionPluginBase;
13 use Drupal\Core\Form\FormStateInterface;
14 use Drupal\Core\Plugin\Context\ContextDefinition;
15 {% endblock %}
16
17 {% block class_declaration %}
18 /**
19 * Provides a '{{ label }}' condition to enable a condition based in module selected status.
20 *
21 * @Condition(
22 *   id = "{{ plugin_id }}",
23 *   label = @Translation("{{ label }}"),
24 *   context = {
25 *     "{{ context_id }}" = @ContextDefinition("{{ context_definition_id }}", required = {{ context_definition_required }} , label = @Translation("{{ context_definition_label }}"))
26 *   }
27 * )
28 *
29 */
30 class {{ class_name }} extends ConditionPluginBase {% endblock %}
31 {% block class_methods %}
32 /**
33 * {@inheritdoc}
34 */
35 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition)
36 {
37     return new static(
38     $configuration,
39     $plugin_id,
40     $plugin_definition
41     );
42 }
43
44 /**
45  * Creates a new {{ class_name }} object.
46  *
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.
56  */
57  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
58     parent::__construct($configuration, $plugin_id, $plugin_definition);
59  }
60
61  /**
62    * {@inheritdoc}
63    */
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');
68
69      $options = [NULL => t('Select a module')];
70      foreach($modules as $module_id => $module) {
71          $options[$module_id] = $module->info['name'];
72      }
73
74      $form['module'] = [
75          '#type' => 'select',
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.'),
80      ];
81
82      return parent::buildConfigurationForm($form, $form_state);
83  }
84
85 /**
86  * {@inheritdoc}
87  */
88  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
89      $this->configuration['module'] = $form_state->getValue('module');
90      parent::submitConfigurationForm($form, $form_state);
91  }
92
93 /**
94  * {@inheritdoc}
95  */
96  public function defaultConfiguration() {
97     return ['module' => ''] + parent::defaultConfiguration();
98  }
99
100 /**
101   * Evaluates the condition and returns TRUE or FALSE accordingly.
102   *
103   * @return bool
104   *   TRUE if the condition has been met, FALSE otherwise.
105   */
106   public function evaluate() {
107       if (empty($this->configuration['module']) && !$this->isNegated()) {
108           return TRUE;
109       }
110
111       $module = $this->configuration['module'];
112       $modules = system_rebuild_module_data();
113
114       return $modules[$module]->status;
115   }
116
117 /**
118  * Provides a human readable summary of the condition's configuration.
119  */
120  public function summary()
121  {
122      $module = $this->getContextValue('module');
123      $modules = system_rebuild_module_data();
124
125      $status = ($modules[$module]->status)?t('enabled'):t('disabled');
126
127      return t('The module @module is @status.', ['@module' => $module, '@status' => $status]);
128  }
129 {% endblock %}