Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / plugin / condition.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }}\Plugin\Condition;
4
5 use Drupal\Component\Datetime\TimeInterface;
6 use Drupal\Core\Condition\ConditionPluginBase;
7 use Drupal\Core\Datetime\DateFormatterInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides a '{{ plugin_label }}' condition.
14  *
15  * @Condition(
16  *   id = "{{ plugin_id }}",
17  *   label = @Translation("{{ plugin_label }}"),
18  *   context = {
19  *     "node" = @ContextDefinition(
20  *       "entity:node",
21  *        label = @Translation("Node")
22  *      )
23  *   }
24  * )
25  */
26 class {{ class }} extends ConditionPluginBase implements ContainerFactoryPluginInterface {
27
28   /**
29    * The date formatter.
30    *
31    * @var \Drupal\Core\Datetime\DateFormatterInterface
32    */
33   protected $dateFormatter;
34
35   /**
36    * The time service.
37    *
38    * @var \Drupal\Component\Datetime\TimeInterface
39    */
40   protected $time;
41
42   /**
43    * Creates a new {{ class }} instance.
44    *
45    * @param array $configuration
46    *   The plugin configuration, i.e. an array with configuration values keyed
47    *   by configuration option name. The special key 'context' may be used to
48    *   initialize the defined contexts by setting it to an array of context
49    *   values keyed by context names.
50    * @param string $plugin_id
51    *   The plugin_id for the plugin instance.
52    * @param mixed $plugin_definition
53    *   The plugin implementation definition.
54    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
55    *   The date formatter.
56    * @param \Drupal\Component\Datetime\TimeInterface $time
57    *   The time service.
58    */
59   public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter, TimeInterface $time) {
60     parent::__construct($configuration, $plugin_id, $plugin_definition);
61     $this->dateFormatter = $date_formatter;
62     $this->time = $time;
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
69     return new static(
70       $configuration,
71       $plugin_id,
72       $plugin_definition,
73       $container->get('date.formatter'),
74       $container->get('datetime.time')
75     );
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function defaultConfiguration() {
82     return ['age' => NULL] + parent::defaultConfiguration();
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
89
90     $form['age'] = [
91       '#title' => $this->t('Node age, sec'),
92       '#type' => 'number',
93       '#min' => 0,
94       '#default_value' => $this->configuration['age'],
95     ];
96
97     return parent::buildConfigurationForm($form, $form_state);
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
104     $this->configuration['age'] = $form_state->getValue('age');
105     parent::submitConfigurationForm($form, $form_state);
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function summary() {
112     return $this->t(
113       'Node age: @age',
114       ['@age' => $this->dateFormatter->formatInterval($this->configuration['age'])]
115     );
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function evaluate() {
122     if (!$this->configuration['age'] && !$this->isNegated()) {
123       return TRUE;
124     }
125     $age = $this->time->getRequestTime() - $this->getContextValue('node')->getCreatedTime();
126     return $age < $this->configuration['age'];
127   }
128
129 }