You are here

public function TokenArgument::getArgument in Views Token Argument 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/views/argument_default/TokenArgument.php \Drupal\views_argument_token\Plugin\views\argument_default\TokenArgument::getArgument()

Return the default argument.

This needs to be overridden by every default argument handler to properly do what is needed.

Overrides ArgumentDefaultPluginBase::getArgument

File

src/Plugin/views/argument_default/TokenArgument.php, line 91

Class

TokenArgument
The Token argument default handler.

Namespace

Drupal\views_argument_token\Plugin\views\argument_default

Code

public function getArgument() {
  $token_service = \Drupal::token();
  $argument = $this->options['argument'];
  $process = $this->options['process'];
  $debug = $this->options['debug'];
  $all_option = $this->options['all_option'];
  $tokens = $this
    ->tokenScan($argument);

  // Presence of a token concerning current user.
  if (isset($tokens['current-user'])) {

    // Case of processing with raw values.
    if ($process) {

      // Get current user value.
      // @todo : dependency injection.
      $current_user = \Drupal::currentUser();
      $account = user_load($current_user
        ->id());

      // Process raw value (even multiple values).
      $argument = $this
        ->processToken($account, 'current-user', $argument);
    }

    // If there is still a current user token.
    if (strpos($argument, 'current-user')) {
      if (!isset($current_user)) {

        // @todo : dependency injection.
        $current_user = \Drupal::currentUser();
      }

      // If the token cannot be translated, it will be removed from
      // the final text.
      $argument = $token_service
        ->replace($argument, [
        'current-user' => $current_user,
      ], [
        'clear' => TRUE,
      ]);
    }
  }

  // Get type current entity.
  $current_path = \Drupal::service('path.current')
    ->getPath();
  $params = Url::fromUri("internal:" . $current_path)
    ->getRouteParameters();
  $entity_type = key($params);

  // If existing token, try to get current entity and replace tokens with
  // the right data.
  // @todo : dependency injection.
  if (isset($tokens[$entity_type]) && ($entity = \Drupal::request()->attributes
    ->get($entity_type))) {

    // Process with raw values for fields.
    if ($process) {
      $argument = $this
        ->processToken($entity, $entity_type, $argument);
    }

    // If still a token, try to replace with token.
    if (strpos($argument, $entity_type)) {

      // If the token cannot be translated, it will be removed from
      // the final text.
      $argument = $token_service
        ->replace($argument, [
        $entity_type => $entity,
      ], [
        'clear' => TRUE,
      ]);
    }
  }

  // Decode final argument HTML entities, as token returns encoded values.
  $argument = PlainTextOutput::renderFromHtml($argument);

  // Show debug for checking the value in the current context.
  if ($debug) {
    drupal_set_message($argument);
  }
  if (!$argument) {
    if (!$all_option) {
      return '';
    }
    $argument = 'all';
  }

  // Clean value (if + or , at the begining or at the end).
  // @todo : remove.
  $argument = $this
    ->cleanArgumentValue($argument);
  return $argument;
}