3 namespace Drupal\{{ machine_name }}\Plugin\Condition;
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;
13 * Provides a '{{ plugin_label }}' condition.
16 * id = "{{ plugin_id }}",
17 * label = @Translation("{{ plugin_label }}"),
19 * "node" = @ContextDefinition(
21 * label = @Translation("Node")
26 class {{ class }} extends ConditionPluginBase implements ContainerFactoryPluginInterface {
31 * @var \Drupal\Core\Datetime\DateFormatterInterface
33 protected $dateFormatter;
38 * @var \Drupal\Component\Datetime\TimeInterface
43 * Creates a new {{ class }} instance.
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
56 * @param \Drupal\Component\Datetime\TimeInterface $time
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;
68 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
73 $container->get('date.formatter'),
74 $container->get('datetime.time')
81 public function defaultConfiguration() {
82 return ['age' => NULL] + parent::defaultConfiguration();
88 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
91 '#title' => $this->t('Node age, sec'),
94 '#default_value' => $this->configuration['age'],
97 return parent::buildConfigurationForm($form, $form_state);
103 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
104 $this->configuration['age'] = $form_state->getValue('age');
105 parent::submitConfigurationForm($form, $form_state);
111 public function summary() {
114 ['@age' => $this->dateFormatter->formatInterval($this->configuration['age'])]
121 public function evaluate() {
122 if (!$this->configuration['age'] && !$this->isNegated()) {
125 $age = $this->time->getRequestTime() - $this->getContextValue('node')->getCreatedTime();
126 return $age < $this->configuration['age'];