You are here

public function UserLinkApiToken::validate in API Tokens 8.2

Same name and namespace in other branches
  1. 8 api_tokens_example/src/Plugin/ApiToken/UserLinkApiToken.php \Drupal\api_tokens_example\Plugin\ApiToken\UserLinkApiToken::validate()

Validates the API token parameters.

This validation must be context-independent. For example, if some parameter is expected to be an entity ID, check only whether it is a valid entity ID, but don't check the entity existence/access (these must be checked in the build method).

Parameters

array $params: An array of named API token parameters. If an API token has more parameters then it is defined in build method, extra parameters will be named by parameter index. For example, if we have the API token [api:example[123, ["option1", "option2"], "extra1", "extra2"]/], and plugin's build method argument definition is: ...($id, $options), the $params will be:

[
  'id' => 123,
  'options' => [
    'option1',
    'option2',
  ],
  '2' => 'extra1',
  '3' => 'extra2',
];

Return value

bool

Overrides ApiTokenBase::validate

See also

\Drupal\api_tokens\ApiTokenPluginInterface::validateToken();

File

api_tokens_example/src/Plugin/ApiToken/UserLinkApiToken.php, line 27

Class

UserLinkApiToken
Provides a User Link API token.

Namespace

Drupal\api_tokens_example\Plugin\ApiToken

Code

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;
}