You are here

class BrowseTokens in Views Token Argument 8

Same name and namespace in other branches
  1. 2.0.x src/BrowseTokens.php \Drupal\views_argument_token\BrowseTokens

Token handling service. Uses core token service or contributed Token.

Hierarchy

Expanded class hierarchy of BrowseTokens

1 string reference to 'BrowseTokens'
views_argument_token.services.yml in ./views_argument_token.services.yml
views_argument_token.services.yml
1 service uses BrowseTokens
views_argument_token.token in ./views_argument_token.services.yml
Drupal\views_argument_token\BrowseTokens

File

src/BrowseTokens.php, line 11

Namespace

Drupal\views_argument_token
View source
class BrowseTokens {

  /**
   * Module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Token service.
   *
   * @var \Drupal\Core\Utility\Token
   */
  protected $coreToken;

  /**
   * Constructs a new BrowseTokens object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   Module handler service.
   * @param \Drupal\Core\Utility\Token $token
   *   Token service.
   */
  public function __construct(ModuleHandlerInterface $module_handler, Token $token) {
    $this->coreToken = $token;
    $this->moduleHandler = $module_handler;
  }

  /**
   * Gatekeeper function to direct to either the core or contributed Token.
   *
   * {@inheritdoc}
   *
   * @todo: update with latest info from token modules.
   */
  public function tokenReplace($string, $data, $settings = array()) {
    if ($this->moduleHandler
      ->moduleExists('token')) {
      return $this
        ->contribReplace($string, $data, $settings);
    }
    else {
      return $this
        ->coreReplace($string, $data, $settings);
    }
  }

  /**
   * Gatekeeper function to direct to either the core or contributed Token.
   *
   * @return array
   *   If token module is installed, a popup browser plus a help text. If not
   *   only the help text.
   */
  public function tokenBrowser() {
    $form = array();
    $form['intro_text'] = array(
      '#markup' => '<p>' . t('Configure the Token argument. Use tokens to avoid redundant data. For example, a \'keyword\' value of "example" will be shown on all content using this configuration, whereas using the [node:title] automatically inserts the "keywords" values from the current entity (node, term, etc).') . '</p>',
    );
    if ($this->moduleHandler
      ->moduleExists('token')) {
      $form['tokens'] = array(
        '#theme' => 'token_tree_link',
        '#token_types' => 'all',
        '#global_types' => TRUE,
        '#click_insert' => TRUE,
        '#show_restricted' => FALSE,
        '#recursion_limit' => 3,
        '#text' => t('Browse available tokens'),
      );
    }
    return $form;
  }

  /**
   * Replace tokens with their values using the core token service.
   *
   * {@inheritdoc}
   *
   * @todo: update with latest info from token modules.
   */
  private function coreReplace($string, $data, $settings = array()) {

    // @TODO: Remove this temp code.
    // This is just here as a way to see all available tokens in debugger.
    $tokens = $this->coreToken
      ->getInfo();
    $options = array(
      'clear' => TRUE,
    );

    // Replace tokens with core Token service.
    $replaced = $this->coreToken
      ->replace($string, $data, $options);

    // Ensure that there are no double-slash sequences due to empty token
    // values.
    $replaced = preg_replace('/(?<!:)\\/+\\//', '/', $replaced);
    return $replaced;
  }

  /**
   * Replace tokens with their values using the contributed token module.
   *
   * {@inheritdoc}
   *
   * @todo: update with latest info from token modules.
   */
  private function contribReplace($string, $data, $settings = array()) {

    // @TODO: Add contrib Token integration when it is ready.
    // For now, just redirect to the core replacement to avoid breaking sites
    // where Token is installed.
    return $this
      ->coreReplace($string, $data, $settings);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BrowseTokens::$coreToken protected property Token service.
BrowseTokens::$moduleHandler protected property Module handler service.
BrowseTokens::contribReplace private function Replace tokens with their values using the contributed token module.
BrowseTokens::coreReplace private function Replace tokens with their values using the core token service.
BrowseTokens::tokenBrowser public function Gatekeeper function to direct to either the core or contributed Token.
BrowseTokens::tokenReplace public function Gatekeeper function to direct to either the core or contributed Token.
BrowseTokens::__construct public function Constructs a new BrowseTokens object.