You are here

DateApiToken.php in API Tokens 8.2

Same filename and directory in other branches
  1. 8 api_tokens_example/src/Plugin/ApiToken/DateApiToken.php

File

api_tokens_example/src/Plugin/ApiToken/DateApiToken.php
View source
<?php

namespace Drupal\api_tokens_example\Plugin\ApiToken;

use Drupal\api_tokens\ApiTokenBase;

/**
 * Provides a Date API token.
 *
 * Token examples:
 * - [api:date/]
 * - [api:date["D, d M y H:i:s"]/]
 *
 * @ApiToken(
 *   id = "date",
 *   label = @Translation("Date"),
 *   description = @Translation("Renders the current date.")
 * )
 */
class DateApiToken extends ApiTokenBase {

  /**
   * Build callback.
   *
   * @param string $format
   *   (optional) The date format. Defaults to "U".
   *
   * return array
   *   A renderable array.
   *
   * @see \Drupal\api_tokens\ApiTokenPluginInterface::build();
   */
  public function build($format = 'U') {
    $this
      ->mergeCacheMaxAge(0);
    $build = [
      '#type' => 'html_tag',
      '#tag' => 'span',
      '#value' => date($format),
      '#attributes' => [
        'class' => 'api-token-date',
      ],
      '#attached' => [
        'library' => [
          'api_tokens_example/date',
        ],
      ],
    ];
    return $build;
  }

}

Classes

Namesort descending Description
DateApiToken Provides a Date API token.