You are here

class NodeApiToken in API Tokens 8

Same name and namespace in other branches
  1. 8.2 api_tokens_example/src/Plugin/ApiToken/NodeApiToken.php \Drupal\api_tokens_example\Plugin\ApiToken\NodeApiToken

Provides a Node API token.

Token examples:

  • [api:node[123]/]
  • [api:node[123, "teaser"]/]

Plugin annotation


@ApiToken(
  id = "node",
  label = @Translation("Node"),
  description = @Translation("Renders a node.")
)

Hierarchy

Expanded class hierarchy of NodeApiToken

File

api_tokens_example/src/Plugin/ApiToken/NodeApiToken.php, line 21

Namespace

Drupal\api_tokens_example\Plugin\ApiToken
View source
class NodeApiToken extends ApiTokenBase {

  /**
   * {@inheritdoc}
   */
  public function validate(array $params) {

    // For [api:node[123]/] token:

    //$params = [

    //  'id' => 123,
    //  'view_mode' => 'full',

    //];

    // For [api:node[123, "teaser"]/] token:

    //$params = [

    //  'id' => 123,
    //  'view_mode' => 'teaser',

    //];

    // Check that "nid" is a valid node ID.
    if (!preg_match('@\\d+@', $params['id'])) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * Build callback.
   *
   * @param int $id
   *   The node ID.
   * @param string $view_mode
   *   (optional) The view mode to render a node in. Defaults to "full".
   *
   * return array
   *   A renderable array.
   *
   * @see \Drupal\api_tokens\ApiTokenPluginInterface::build();
   */
  public function build($id, $view_mode = 'full') {
    $build = [];
    $node = Node::load($id);
    if ($node && $node
      ->access('view')) {
      $build = \Drupal::entityTypeManager()
        ->getViewBuilder('node')
        ->view($node, $view_mode);
    }
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ApiTokenBase::$context protected static property The API token render context.
ApiTokenBase::$hash protected property The API token parameters hash.
ApiTokenBase::$logger protected property The API tokens logger.
ApiTokenBase::$moduleHandler protected property The module handler service.
ApiTokenBase::$params protected property The API token parameters.
ApiTokenBase::$paramString protected property The API token parameters string.
ApiTokenBase::$reflector protected property The API token build method reflection object.
ApiTokenBase::$renderer protected property The renderer.
ApiTokenBase::$token protected property The API token string.
ApiTokenBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
ApiTokenBase::description public function Returns the administrative description of the API token. Overrides ApiTokenPluginInterface::description
ApiTokenBase::fallback public function Returns a build to replace the API token with in case of validation fail. Overrides ApiTokenPluginInterface::fallback
ApiTokenBase::hash public function Returns the API token parameters hash. Overrides ApiTokenPluginInterface::hash
ApiTokenBase::id public function Returns the API token ID. Overrides ApiTokenPluginInterface::id
ApiTokenBase::label public function Returns the administrative label of the API token. Overrides ApiTokenPluginInterface::label
ApiTokenBase::lazyBuilder public static function The #lazy_builder callback. Overrides ApiTokenPluginInterface::lazyBuilder
ApiTokenBase::params public function Returns the API token parameters. Overrides ApiTokenPluginInterface::params
ApiTokenBase::paramString public function Returns the API token parameter string. Overrides ApiTokenPluginInterface::paramString
ApiTokenBase::placeholder public function Returns a #lazy_builder placeholder for the API tokens filter. Overrides ApiTokenPluginInterface::placeholder
ApiTokenBase::process public function Returns processed API token build. Overrides ApiTokenPluginInterface::process
ApiTokenBase::provider public function Returns the name of the provider that owns this API token. Overrides ApiTokenPluginInterface::provider
ApiTokenBase::reflector public function Returns the API token build method reflection object. Overrides ApiTokenPluginInterface::reflector
ApiTokenBase::token public function Returns the API token string. Overrides ApiTokenPluginInterface::token
ApiTokenBase::validateToken public function Performs one-time context-independent validation of the API token. Overrides ApiTokenPluginInterface::validateToken
ApiTokenBase::__construct public function Constructs an ApiTokenBase object. Overrides PluginBase::__construct
CacheableDependencyTrait::$cacheContexts protected property Cache contexts.
CacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
CacheableDependencyTrait::$cacheTags protected property Cache tags.
CacheableDependencyTrait::getCacheContexts public function 3
CacheableDependencyTrait::getCacheMaxAge public function 3
CacheableDependencyTrait::getCacheTags public function 3
CacheableDependencyTrait::setCacheability protected function Sets cacheability; useful for value object constructors.
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.
NodeApiToken::build public function Build callback.
NodeApiToken::validate public function Validates the API token parameters. Overrides ApiTokenBase::validate
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.
RefinableCacheableDependencyTrait::addCacheableDependency public function 1
RefinableCacheableDependencyTrait::addCacheContexts public function
RefinableCacheableDependencyTrait::addCacheTags public function
RefinableCacheableDependencyTrait::mergeCacheMaxAge 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.