You are here

public function CourierTokenElementTrait::courierTokenElement in Courier 8

Same name and namespace in other branches
  1. 2.x src/CourierTokenElementTrait.php \Drupal\courier\CourierTokenElementTrait::courierTokenElement()

Render a token element.

This function will determine if Token module is enabled, and use an AJAX token tree element. Otherwise it will render the list of tokens in plain text.

Parameters

array $tokens: An array of global token types.

Return value

array A render array.

2 calls to CourierTokenElementTrait::courierTokenElement()
CourierTokenElementTrait::templateCollectionTokenElement in src/CourierTokenElementTrait.php
Render a token element for a template collection.
MessageForm::buildForm in courier_message_composer/src/Form/MessageForm.php
Form constructor.

File

src/CourierTokenElementTrait.php, line 47
Contains \Drupal\courier\CourierTokenElementTrait.

Class

CourierTokenElementTrait
Defines a trait for adding a token element based on available tokens for a template collection.

Namespace

Drupal\courier

Code

public function courierTokenElement($tokens = []) {
  if (!in_array('identity', $tokens)) {
    $tokens[] = 'identity';
  }
  if (\Drupal::moduleHandler()
    ->moduleExists('token')) {
    return [
      '#theme' => 'token_tree_link',
      '#token_types' => $tokens,
    ];
  }
  else {

    // Add global token types.
    $token_info = \Drupal::token()
      ->getInfo();
    foreach ($token_info['types'] as $type => $type_info) {
      if (empty($type_info['needs-data'])) {
        $tokens[] = $type;
      }
    }
    foreach ($tokens as &$token) {
      $token = '[' . $token . ':*]';
    }
    return [
      '#markup' => $this
        ->t('Available tokens: @token_types', [
        '@token_types' => implode(', ', $tokens),
      ]),
    ];
  }
}