You are here

public static function WebformTwigExtension::buildTwigHelp in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Twig/WebformTwigExtension.php \Drupal\webform\Twig\WebformTwigExtension::buildTwigHelp()

Build reusable Twig help.

Parameters

array $variables: An array of available variable names.

Return value

array A renderable array container Twig help.

5 calls to WebformTwigExtension::buildTwigHelp()
EmailWebformHandler::buildConfigurationForm in src/Plugin/WebformHandler/EmailWebformHandler.php
Form constructor.
WebformAttachmentTwig::form in modules/webform_attachment/src/Plugin/WebformElement/WebformAttachmentTwig.php
Gets the actual configuration webform array to be built.
WebformComputedTwig::form in src/Plugin/WebformElement/WebformComputedTwig.php
Gets the actual configuration webform array to be built.
WebformElementBase::form in src/Plugin/WebformElementBase.php
Gets the actual configuration webform array to be built.
WebformEntityPrintAttachment::form in modules/webform_entity_print_attachment/src/Plugin/WebformElement/WebformEntityPrintAttachment.php
Gets the actual configuration webform array to be built.

File

src/Twig/WebformTwigExtension.php, line 155

Class

WebformTwigExtension
Twig extension with some useful functions and filters.

Namespace

Drupal\webform\Twig

Code

public static function buildTwigHelp(array $variables = []) {

  /** @var \Drupal\webform\WebformSubmissionStorageInterface $submission_storage */
  $submission_storage = \Drupal::entityTypeManager()
    ->getStorage('webform_submission');
  $field_definitions = $submission_storage
    ->getFieldDefinitions();

  // Bold all the passed variable names.
  foreach ($variables as $index => $item) {
    $variables[$index] = [
      '#markup' => $item,
      '#prefix' => '<strong>',
      '#suffix' => '</strong>',
    ];
  }
  $variables = array_merge($variables, [
    '{{ data.element_key }}',
    '{{ data[\'element_key\'] }}',
    '{{ data.element_key.delta }}',
    '{{ data[\'element_key\'][\'delta\'] }}',
    '{{ data.composite_element_key.subelement_key }}',
    '{{ data.composite_element_key.delta.subelement_key }}',
    '{{ original_data }}',
    '{{ elements }}',
    '{{ elements_flattened }}',
  ]);
  foreach (array_keys($field_definitions) as $field_name) {
    $variables[] = "{{ {$field_name} }}";
  }
  $variables = array_merge($variables, [
    '{{ webform }}',
    '{{ webform_submission }}',
  ]);
  $t_args = [
    ':twig_href' => 'https://twig.sensiolabs.org/',
    ':drupal_href' => 'https://www.drupal.org/docs/8/theming/twig',
  ];
  $output = [];
  $output[] = [
    '#markup' => '<p>' . t('Learn about <a href=":twig_href">Twig</a> and how it is used in <a href=":drupal_href">Drupal</a>.', $t_args) . '</p>',
  ];
  $output[] = [
    '#markup' => '<p>' . t("The following variables are available:") . '</p>',
  ];
  $output[] = [
    '#theme' => 'item_list',
    '#items' => $variables,
  ];
  $output[] = [
    '#markup' => '<p>' . t("You can also output tokens using the <code>webform_token()</code> function.") . '</p>',
  ];
  $output[] = [
    '#markup' => "<pre>{{ webform_token('[webform_submission:values:element_value]', webform_submission, [], options) }}</pre>",
  ];
  $output[] = [
    '#markup' => '<p>' . t("You can debug data using the <code>webform_debug()</code> function.") . '</p>',
  ];
  $output[] = [
    '#markup' => "<pre>{{ webform_debug(data) }}</pre>",
  ];
  if (\Drupal::currentUser()
    ->hasPermission('administer modules') && !\Drupal::moduleHandler()
    ->moduleExists('twig_tweak')) {
    $t_args = [
      ':module_href' => 'https://www.drupal.org/project/twig_tweak',
      ':documentation_href' => 'https://www.drupal.org/docs/8/modules/twig-tweak/cheat-sheet-8x-2x',
    ];
    $output[] = [
      '#type' => 'webform_message',
      '#message_type' => 'info',
      '#message_message' => t('Install the <a href=":module_href">Twig tweak</a> module, which provides a Twig extension with some <a href=":documentation_href">useful functions and filters</a> that can improve development experience.', $t_args),
      '#message_close' => TRUE,
      '#storage' => WebformMessage::STORAGE_SESSION,
    ];
  }
  return [
    '#type' => 'details',
    '#title' => t('Help using Twig'),
    'description' => $output,
  ];
}