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