You are here

class Countdown in Countdown 8

Provides a 'Countdown' Block.

Plugin annotation


@Block(
  id = "countdown_block",
  admin_label = @Translation("Countdown block"),
)

Hierarchy

Expanded class hierarchy of Countdown

1 string reference to 'Countdown'
countdown.info.yml in ./countdown.info.yml
countdown.info.yml

File

src/Plugin/Block/countdown.php, line 19

Namespace

Drupal\countdown\Plugin\Block
View source
class Countdown extends BlockBase {

  /**
   * Implements a block render.
   */
  public function build() {
    $config = $this
      ->getConfiguration();
    $time = time();
    $event_name = isset($config['event_name']) ? $config['event_name'] : '';
    $url = isset($config['url']) ? $config['url'] : '';
    if ($url != "") {
      if (UrlHelper::isExternal($url)) {
        $event_name = Link::fromTextAndUrl($event_name, Url::fromUri($url, []))
          ->toString();
      }
      else {
        if ($url == "<front>") {
          $url = "/";
        }
        $event_name = Link::fromTextAndUrl($event_name, Url::fromUri('internal:/' . ltrim($url, '/'), []))
          ->toString();
      }
    }
    $accuracy = isset($config['accuracy']) ? $config['accuracy'] : '';
    $countdown_timestamp = isset($config['timestamp']) ? $config['timestamp'] : $time;
    $difference = $countdown_timestamp - $time;
    if ($difference < 0) {
      $passed = 1;
      $difference = abs($difference);
    }
    else {
      $passed = 0;
    }
    if ($passed) {
      $event_name = $this
        ->t(' since @event_name.', [
        '@event_name' => $event_name,
      ]);
    }
    else {
      $event_name = $this
        ->t(' until @event_name.', [
        '@event_name' => $event_name,
      ]);
    }
    $days_left = floor($difference / 60 / 60 / 24);
    $hrs_left = floor(($difference - $days_left * 60 * 60 * 24) / 60 / 60);
    $min_left = floor(($difference - $days_left * 60 * 60 * 24 - $hrs_left * 60 * 60) / 60);
    $secs_left = floor($difference - $days_left * 60 * 60 * 24 - $hrs_left * 60 * 60 - $min_left * 60);
    $days_left = $this
      ->formatPlural($days_left, '1 day', '@count days');
    if ($accuracy == 'h' || $accuracy == 'm' || $accuracy == 's') {
      $hrs_left = $this
        ->formatPlural($hrs_left, ', 1 hour', ', @count hours');
    }
    if ($accuracy == 'm' || $accuracy == 's') {
      $min_left = $this
        ->formatPlural($min_left, ', 1 minute', ', @count minutes');
    }
    if ($accuracy == 's') {
      $secs_left = $this
        ->formatPlural($secs_left, ', 1 second', ', @count seconds');
    }
    $build = [
      '#theme' => 'countdown',
      '#cache' => [
        'max-age' => 0,
      ],
      '#accuracy' => $accuracy,
      '#countdown_url' => $url,
      '#countdown_event_name' => $event_name,
      '#days_left' => $days_left,
      '#hrs_left' => $hrs_left,
      '#min_left' => $min_left,
      '#secs_left' => $secs_left,
      '#attached' => [
        'library' => 'countdown/countdownblock',
        'drupalSettings' => [
          'countdown' => [
            'countdownblock' => [
              'accuracy' => $accuracy,
            ],
          ],
        ],
      ],
    ];
    return $build;
  }

  /**
   * Implements a block form handler.
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form = parent::blockForm($form, $form_state);
    $time = time();
    $timestamp = $time;
    $event_name = '';
    $countdown_url = '';
    $countdown_accuracy = '';

    // Retrieve existing configuration for this block.
    $config = $this
      ->getConfiguration();
    if (isset($config['event_name'])) {
      $event_name = $config['event_name'];
      $countdown_url = $config['url'];
      $countdown_accuracy = $config['accuracy'];
      $timestamp = $config['timestamp'];
    }
    $form['countdown_event_name'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Event Name'),
      '#default_value' => $event_name,
      '#size' => 30,
      '#maxlength' => 200,
      '#description' => $this
        ->t("Event name you're counting to or from."),
      '#required' => TRUE,
    ];
    $form['countdown_url'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Event URL'),
      '#default_value' => $countdown_url,
      '#size' => 30,
      '#maxlength' => 200,
      '#description' => $this
        ->t('Turn the event description into a link to more
        information about the event.
        Start typing the title of a piece of content to select it.
        You can also enter an internal path such as %add-node or an external URL
        such as %url. Enter %front to link to the front page.', [
        '%front' => '<front>',
        '%add-node' => '/node/add',
        '%url' => 'http://example.com',
      ]),
      '#required' => FALSE,
    ];
    $form['countdown_accuracy'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Accuracy'),
      '#default_value' => $countdown_accuracy,
      '#options' => [
        'd' => $this
          ->t('days'),
        'h' => $this
          ->t('hours'),
        'm' => $this
          ->t('minutes'),
        's' => $this
          ->t('seconds'),
      ],
      '#description' => $this
        ->t('Select the smallest amount of detail to display. For example, selecting "days" will display only days, selecting "hours" will display the number of days and hours.'),
    ];
    $form['target_time'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Target date/time'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#description' => $this
        ->t('Select a date relative to the server time: %s', [
        '%s' => \Drupal::service('date.formatter')
          ->format($time),
      ]),
    ];
    for ($years = [], $i = 1970; $i < 2032; $years[$i] = $i, $i++) {
    }
    $form['target_time']['year'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Year'),
      '#default_value' => (int) date('Y', $timestamp),
      '#options' => $years,
    ];
    unset($years);
    $form['target_time']['month'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Month'),
      '#default_value' => (int) date('n', $timestamp),
      '#options' => [
        1 => $this
          ->t('January'),
        2 => $this
          ->t('February'),
        3 => $this
          ->t('March'),
        4 => $this
          ->t('April'),
        5 => $this
          ->t('May'),
        6 => $this
          ->t('June'),
        7 => $this
          ->t('July'),
        8 => $this
          ->t('August'),
        9 => $this
          ->t('September'),
        10 => $this
          ->t('October'),
        11 => $this
          ->t('November'),
        12 => $this
          ->t('December'),
      ],
    ];
    for ($month_days = [], $i = 1; $i < 32; $month_days[$i] = $i, $i++) {
    }
    $form['target_time']['day'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Day'),
      '#default_value' => (int) date('j', $timestamp),
      '#options' => $month_days,
    ];
    unset($month_days);
    for ($hrs = [], $i = 0; $i < 24; $hrs[] = $i, $i++) {
    }
    $form['target_time']['hour'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Hour'),
      '#default_value' => (int) date('G', $timestamp),
      '#options' => $hrs,
    ];
    unset($hrs);
    for ($mins = [], $i = 0; $i < 60; $mins[] = $i, $i++) {
    }
    $form['target_time']['min'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Minute'),
      '#default_value' => (int) date('i', $timestamp),
      '#options' => $mins,
    ];
    $form['target_time']['sec'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Seconds'),
      '#default_value' => (int) date('s', $timestamp),
      '#options' => $mins,
    ];
    return $form;
  }

  /**
   * Implements a block submit handler.
   *
   * Save configuration into system.
   */
  public function blockSubmit($form, FormStateInterface $form_state) {

    // Save our custom settings when the form is submitted.
    $this
      ->setConfigurationValue('event_name', $form_state
      ->getValue('countdown_event_name'));
    $this
      ->setConfigurationValue('url', $form_state
      ->getValue('countdown_url'));
    $this
      ->setConfigurationValue('accuracy', $form_state
      ->getValue('countdown_accuracy'));
    $timestamp = $form_state
      ->getValue('target_time');
    $countdown_timestamp = mktime((int) $timestamp['hour'], (int) $timestamp['min'], (int) $timestamp['sec'], (int) $timestamp['month'], (int) $timestamp['day'], (int) $timestamp['year']);
    $this
      ->setConfigurationValue('timestamp', $countdown_timestamp);
  }

  /**
   * Implements form validation.
   *
   * The validateForm method is the default method called to validate input on
   * a form.
   *
   * @param array $form
   *   The render array of the currently built form.
   * @param FormStateInterface $form_state
   *   Object describing the current state of the form.
   */
  public function blockValidate($form, FormStateInterface $form_state) {
    $url = $form_state
      ->getValue('countdown_url');
    if ($url != "" || $url == "<front>") {
      if (!UrlHelper::isExternal($url) && !(strpos($url, '/') === 0)) {
        $form_state
          ->setErrorByName('countdown_url', $this
          ->t('Event URL is not vaild. Entered paths should start with /'));
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockPluginInterface::BLOCK_LABEL_VISIBLE constant Indicates the block label (title) should be displayed to end users.
BlockPluginTrait::$transliteration protected property The transliteration service.
BlockPluginTrait::access public function
BlockPluginTrait::baseConfigurationDefaults protected function Returns generic default configuration for block plugins.
BlockPluginTrait::blockAccess protected function Indicates whether the block should be shown. 16
BlockPluginTrait::buildConfigurationForm public function Creates a generic configuration form for all block types. Individual block plugins can add elements to this form by overriding BlockBase::blockForm(). Most block plugins should not override this method unless they need to alter the generic form elements. 2
BlockPluginTrait::calculateDependencies public function
BlockPluginTrait::defaultConfiguration public function 19
BlockPluginTrait::getConfiguration public function 1
BlockPluginTrait::getMachineNameSuggestion public function 1
BlockPluginTrait::getPreviewFallbackString public function 3
BlockPluginTrait::label public function
BlockPluginTrait::setConfiguration public function
BlockPluginTrait::setConfigurationValue public function
BlockPluginTrait::setTransliteration public function Sets the transliteration service.
BlockPluginTrait::submitConfigurationForm public function Most block plugins should not override this method. To add submission handling for a specific block type, override BlockBase::blockSubmit().
BlockPluginTrait::transliteration protected function Wraps the transliteration service.
BlockPluginTrait::validateConfigurationForm public function Most block plugins should not override this method. To add validation for a specific block type, override BlockBase::blockValidate(). 1
BlockPluginTrait::__construct public function 22
ContextAwarePluginAssignmentTrait::addContextAssignmentElement protected function Builds a form element for assigning a context to a given slot.
ContextAwarePluginAssignmentTrait::contextHandler protected function Wraps the context handler.
ContextAwarePluginBase::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginBase::$contexts Deprecated private property Data objects representing the contexts passed in the plugin configuration.
ContextAwarePluginBase::createContextFromConfiguration protected function Overrides ContextAwarePluginBase::createContextFromConfiguration
ContextAwarePluginBase::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts 9
ContextAwarePluginBase::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge 7
ContextAwarePluginBase::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags 4
ContextAwarePluginBase::getContext public function This code is identical to the Component in order to pick up a different Context class. Overrides ContextAwarePluginBase::getContext
ContextAwarePluginBase::getContextDefinition public function Overrides ContextAwarePluginBase::getContextDefinition
ContextAwarePluginBase::getContextDefinitions public function Overrides ContextAwarePluginBase::getContextDefinitions
ContextAwarePluginBase::getContextMapping public function Gets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::getContextMapping
ContextAwarePluginBase::getContexts public function Gets the defined contexts. Overrides ContextAwarePluginInterface::getContexts
ContextAwarePluginBase::getContextValue public function Gets the value for a defined context. Overrides ContextAwarePluginInterface::getContextValue
ContextAwarePluginBase::getContextValues public function Gets the values for all defined contexts. Overrides ContextAwarePluginInterface::getContextValues
ContextAwarePluginBase::setContext public function Set a context on this plugin. Overrides ContextAwarePluginBase::setContext
ContextAwarePluginBase::setContextMapping public function Sets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::setContextMapping
ContextAwarePluginBase::setContextValue public function Sets the value for a defined context. Overrides ContextAwarePluginBase::setContextValue
ContextAwarePluginBase::validateContexts public function Validates the set values for the defined contexts. Overrides ContextAwarePluginInterface::validateContexts
ContextAwarePluginBase::__get public function Implements magic __get() method.
Countdown::blockForm public function Implements a block form handler. Overrides BlockPluginTrait::blockForm
Countdown::blockSubmit public function Implements a block submit handler. Overrides BlockPluginTrait::blockSubmit
Countdown::blockValidate public function Implements form validation. Overrides BlockPluginTrait::blockValidate
Countdown::build public function Implements a block render. Overrides BlockPluginInterface::build
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginWithFormsTrait::getFormClass public function
PluginWithFormsTrait::hasFormClass public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2
TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2