class NodeApiToken in API Tokens 8
Same name and namespace in other branches
- 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\api_tokens\ApiTokenBase implements ApiTokenPluginInterface uses RefinableCacheableDependencyTrait
- class \Drupal\api_tokens_example\Plugin\ApiToken\NodeApiToken
 
 
 - class \Drupal\api_tokens\ApiTokenBase implements ApiTokenPluginInterface uses RefinableCacheableDependencyTrait
 
 - class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
 
Expanded class hierarchy of NodeApiToken
File
- api_tokens_example/
src/ Plugin/ ApiToken/ NodeApiToken.php, line 21  
Namespace
Drupal\api_tokens_example\Plugin\ApiTokenView 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
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            ApiTokenBase:: | 
                  protected static | property | The API token render context. | |
| 
            ApiTokenBase:: | 
                  protected | property | The API token parameters hash. | |
| 
            ApiTokenBase:: | 
                  protected | property | The API tokens logger. | |
| 
            ApiTokenBase:: | 
                  protected | property | The module handler service. | |
| 
            ApiTokenBase:: | 
                  protected | property | The API token parameters. | |
| 
            ApiTokenBase:: | 
                  protected | property | The API token parameters string. | |
| 
            ApiTokenBase:: | 
                  protected | property | The API token build method reflection object. | |
| 
            ApiTokenBase:: | 
                  protected | property | The renderer. | |
| 
            ApiTokenBase:: | 
                  protected | property | The API token string. | |
| 
            ApiTokenBase:: | 
                  public static | function | 
            Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public | function | 
            Returns the administrative description of the API token. Overrides ApiTokenPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public | function | 
            Returns a build to replace the API token with in case of validation fail. Overrides ApiTokenPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public | function | 
            Returns the API token parameters hash. Overrides ApiTokenPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public | function | 
            Returns the API token ID. Overrides ApiTokenPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public | function | 
            Returns the administrative label of the API token. Overrides ApiTokenPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public static | function | 
            The #lazy_builder callback. Overrides ApiTokenPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public | function | 
            Returns the API token parameters. Overrides ApiTokenPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public | function | 
            Returns the API token parameter string. Overrides ApiTokenPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public | function | 
            Returns a #lazy_builder placeholder for the API tokens filter. Overrides ApiTokenPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public | function | 
            Returns processed API token build. Overrides ApiTokenPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public | function | 
            Returns the name of the provider that owns this API token. Overrides ApiTokenPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public | function | 
            Returns the API token build method reflection object. Overrides ApiTokenPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public | function | 
            Returns the API token string. Overrides ApiTokenPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public | function | 
            Performs one-time context-independent validation of the API token. Overrides ApiTokenPluginInterface:: | 
                  |
| 
            ApiTokenBase:: | 
                  public | function | 
            Constructs an ApiTokenBase object. Overrides PluginBase:: | 
                  |
| 
            CacheableDependencyTrait:: | 
                  protected | property | Cache contexts. | |
| 
            CacheableDependencyTrait:: | 
                  protected | property | Cache max-age. | |
| 
            CacheableDependencyTrait:: | 
                  protected | property | Cache tags. | |
| 
            CacheableDependencyTrait:: | 
                  public | function | 3 | |
| 
            CacheableDependencyTrait:: | 
                  public | function | 3 | |
| 
            CacheableDependencyTrait:: | 
                  public | function | 3 | |
| 
            CacheableDependencyTrait:: | 
                  protected | function | Sets cacheability; useful for value object constructors. | |
| 
            DependencySerializationTrait:: | 
                  protected | property | An array of entity type IDs keyed by the property name of their storages. | |
| 
            DependencySerializationTrait:: | 
                  protected | property | An array of service IDs keyed by property name used for serialization. | |
| 
            DependencySerializationTrait:: | 
                  public | function | 1 | |
| 
            DependencySerializationTrait:: | 
                  public | function | 2 | |
| 
            MessengerTrait:: | 
                  protected | property | The messenger. | 29 | 
| 
            MessengerTrait:: | 
                  public | function | Gets the messenger. | 29 | 
| 
            MessengerTrait:: | 
                  public | function | Sets the messenger. | |
| 
            NodeApiToken:: | 
                  public | function | Build callback. | |
| 
            NodeApiToken:: | 
                  public | function | 
            Validates the API token parameters. Overrides ApiTokenBase:: | 
                  |
| 
            PluginBase:: | 
                  protected | property | Configuration information passed into the plugin. | 1 | 
| 
            PluginBase:: | 
                  protected | property | The plugin implementation definition. | 1 | 
| 
            PluginBase:: | 
                  protected | property | The plugin_id. | |
| 
            PluginBase:: | 
                  constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
| 
            PluginBase:: | 
                  public | function | 
            Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: | 
                  |
| 
            PluginBase:: | 
                  public | function | 
            Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: | 
                  |
| 
            PluginBase:: | 
                  public | function | 
            Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: | 
                  3 | 
| 
            PluginBase:: | 
                  public | function | 
            Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: | 
                  |
| 
            PluginBase:: | 
                  public | function | Determines if the plugin is configurable. | |
| 
            RefinableCacheableDependencyTrait:: | 
                  public | function | 1 | |
| 
            RefinableCacheableDependencyTrait:: | 
                  public | function | ||
| 
            RefinableCacheableDependencyTrait:: | 
                  public | function | ||
| 
            RefinableCacheableDependencyTrait:: | 
                  public | function | ||
| 
            StringTranslationTrait:: | 
                  protected | property | The string translation service. | 1 | 
| 
            StringTranslationTrait:: | 
                  protected | function | Formats a string containing a count of items. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Returns the number of plurals supported by a given language. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Gets the string translation service. | |
| 
            StringTranslationTrait:: | 
                  public | function | Sets the string translation service to use. | 2 | 
| 
            StringTranslationTrait:: | 
                  protected | function | Translates a string to the current language or to a given language. |