class UserLinkApiToken in API Tokens 8
Same name and namespace in other branches
- 8.2 api_tokens_example/src/Plugin/ApiToken/UserLinkApiToken.php \Drupal\api_tokens_example\Plugin\ApiToken\UserLinkApiToken
Provides a User Link API token.
Token examples:
- [api:user-link/]
- [api:user-link[123]/]
Plugin annotation
@ApiToken(
id = "user-link",
label = @Translation("User link"),
description = @Translation("Renders a user link.")
)
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\UserLinkApiToken
- class \Drupal\api_tokens\ApiTokenBase implements ApiTokenPluginInterface uses RefinableCacheableDependencyTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of UserLinkApiToken
File
- api_tokens_example/
src/ Plugin/ ApiToken/ UserLinkApiToken.php, line 22
Namespace
Drupal\api_tokens_example\Plugin\ApiTokenView source
class UserLinkApiToken extends ApiTokenBase {
/**
* {@inheritdoc}
*/
public function validate(array $params) {
// For [api:user-link/] token:
//$params = [
// 'id' => NULL,
//];
// For [api:user-link[123]/] token:
//$params = [
// 'id' => 123,
//];
// If "id" is provided, check that it is a valid user ID.
if (isset($params['id']) && !preg_match('@\\d+@', $params['id'])) {
return FALSE;
}
return TRUE;
}
/**
* Build callback.
*
* @param int $id
* (optional) The user ID.
*
* return array
* A renderable array.
*
* @see \Drupal\api_tokens\ApiTokenPluginInterface::build();
*/
public function build($id = NULL) {
$build = [];
if ($id) {
$user = User::load($id);
if ($user && $user
->access('view')) {
$build = [
'#markup' => $user
->link(),
];
$this
->addCacheContexts([
'user',
]);
$this
->addCacheableDependency($user);
}
}
else {
$build = Link::createFromRoute(\Drupal::currentUser()
->getDisplayName(), 'user.page')
->toRenderable();
$this
->addCacheContexts([
'user',
]);
}
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. | |
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. | |
UserLinkApiToken:: |
public | function | Build callback. | |
UserLinkApiToken:: |
public | function |
Validates the API token parameters. Overrides ApiTokenBase:: |