You are here

public function CourierTokenElementTrait::courierTokenElement in Courier 2.x

Same name and namespace in other branches
  1. 8 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 44

Class

CourierTokenElementTrait
Token trait.

Namespace

Drupal\courier

Code

public function courierTokenElement(array $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),
      ]),
    ];
  }
}