You are here

AutoBlockScheduler.php in Auto Block Scheduler 8

Same filename and directory in other branches
  1. 8.2 src/Plugin/Condition/AutoBlockScheduler.php

Contains \Drupal\block_scheduler\Plugin\Condition\Expiry.

File

src/Plugin/Condition/AutoBlockScheduler.php
View source
<?php

/**
 * @file
 * Contains \Drupal\block_scheduler\Plugin\Condition\Expiry.
 */
namespace Drupal\auto_block_scheduler\Plugin\Condition;

use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Datetime\DrupalDateTime;

/**
 * Provides a 'AutoBlockScheduler ' condition.
 *
 * @Condition(
 *   id = "auto_block_scheduler",
 *   label = @Translation("Auto Block Scheduler")
 * )
 */
class AutoBlockScheduler extends ConditionPluginBase {

  /**
   * {@inheritdoc}
   */
  public function summary() {
    return t('Auto Block Scheduler');
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $default_start = !empty($this->configuration['published_on']) ? DrupalDateTime::createFromTimestamp($this->configuration['published_on']) : '';
    $default_end = !empty($this->configuration['unpublished_on']) ? DrupalDateTime::createFromTimestamp($this->configuration['unpublished_on']) : '';
    $form['published_on'] = [
      '#type' => 'datetime',
      '#title' => t('Published Date'),
      '#default_value' => $default_start,
      '#description' => $this
        ->t('If you select no Published Date, the condition will evaluate to TRUE for all requests.'),
      '#attached' => [
        'library' => [
          'auto_block_scheduler/drupal.auto_block_scheduler',
        ],
      ],
    ];
    $form['unpublished_on'] = [
      '#type' => 'datetime',
      '#title' => t('Unpublished Date'),
      '#default_value' => $default_end,
      '#description' => $this
        ->t('If you select no Unpublished Date, the condition will evaluate to TRUE for all requests.'),
    ];
    $form['#attached']['library'][] = 'auto_block_scheduler/drupal.auto_block_scheduler';
    return parent::buildConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    if (is_object($form_state
      ->getValue('published_on'))) {
      $this->configuration['published_on'] = $form_state
        ->getValue('published_on')
        ->getTimestamp();
    }
    else {
      $this->configuration['published_on'] = '';
    }
    if (is_object($form_state
      ->getValue('unpublished_on'))) {
      $this->configuration['unpublished_on'] = $form_state
        ->getValue('unpublished_on')
        ->getTimestamp();
    }
    else {
      $this->configuration['unpublished_on'] = '';
    }
    parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function evaluate() {
    $status = TRUE;
    if (empty($this->configuration['published_on']) && empty($this->configuration['unpublished_on']) && !$this
      ->isNegated()) {
      return TRUE;
    }
    if (!empty($this->configuration['published_on'])) {
      $status = $status && time() >= $this->configuration['published_on'];
    }
    if (!empty($this->configuration['unpublished_on'])) {
      $status = $status && time() <= $this->configuration['unpublished_on'];
    }
    return $status;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'published_on' => '',
      'unpublished_on' => '',
    ] + parent::defaultConfiguration();
  }

}

Classes

Namesort descending Description
AutoBlockScheduler Provides a 'AutoBlockScheduler ' condition.