You are here

public static function WebformSubmissionLimitBlock::buildTokens in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/Block/WebformSubmissionLimitBlock.php \Drupal\webform\Plugin\Block\WebformSubmissionLimitBlock::buildTokens()

Build available tokens for submission limit type and source entity.

NOTE: Using inline style attributes of fix in-place block editing UX.

Parameters

string $type: The submission type which can be 'webform' or 'user'.

string $source_entity: Flag indicating if source entity should be included in available tokens.

Return value

array A render array containing a list of available tokens.

2 calls to WebformSubmissionLimitBlock::buildTokens()
WebformSubmissionLimitBlock::blockForm in src/Plugin/Block/WebformSubmissionLimitBlock.php
WebformSubmissionLimitBlock::tokenAjaxCallback in src/Plugin/Block/WebformSubmissionLimitBlock.php
Ajax callback that returns the block form.

File

src/Plugin/Block/WebformSubmissionLimitBlock.php, line 548

Class

WebformSubmissionLimitBlock
Provides a 'Webform submission limit' block.

Namespace

Drupal\webform\Plugin\Block

Code

public static function buildTokens($type, $source_entity) {

  /** @var \Drupal\webform\WebformTokenManagerInterface $token_manager */
  $token_manager = \Drupal::service('webform.token_manager');

  // Get token name and descriptions.
  module_load_include('inc', 'webform', 'webform.tokens');
  $token_info = webform_token_info();
  $tokens = $token_info['tokens']['webform_submission'];
  $token_types = [
    'limit',
    'interval',
    'total',
    'remaining',
  ];
  $rows = [];
  foreach ($token_types as $token_type) {
    $token_name = static::getTokenName($token_type, $type, $source_entity);
    $rows[] = [
      [
        'data' => '[' . $token_type . ']',
        'style' => 'vertical-align: top',
      ],
      [
        'data' => [
          'name' => [
            '#markup' => $tokens[$token_name]['name'],
            '#prefix' => '<strong>',
            '#suffix' => '</strong><br/>',
          ],
          'description' => [
            '#markup' => $tokens[$token_name]['description'],
          ],
        ],
        'style' => 'vertical-align: top',
      ],
    ];
  }
  return [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'webform-submission-limit-block-tokens',
    ],
    'details' => [
      '#type' => 'details',
      '#title' => t('Available tokens'),
      '#open' => TRUE,
      'table' => [
        '#type' => 'table',
        '#header' => [
          [
            'data' => t('Token'),
            'style' => 'width: auto',
          ],
          [
            'data' => t('Name / Description'),
            'style' => 'width: 100%',
          ],
        ],
        '#rows' => $rows,
        '#attributes' => [
          'style' => 'margin: 1em 0',
        ],
      ],
      'token_tree_link' => [
        'token' => $token_manager
          ->buildTreeElement(),
      ],
    ],
  ];
}