You are here

function _webform_token_get_submission_value in Webform 6.x

Same name and namespace in other branches
  1. 8.5 webform.tokens.inc \_webform_token_get_submission_value()

Get webform submission token value.

Parameters

string $value_token: A [webform_submission:value:?] token.

array $options: An array of token options.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

\Drupal\webform\Plugin\WebformElementManagerInterface $element_manager: The webform element manager.

\Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata: (optional) Object to collect route processors' bubbleable metadata.

Return value

\Drupal\Component\Render\MarkupInterface|string Webform submission token value.

1 call to _webform_token_get_submission_value()
webform_tokens in ./webform.tokens.inc
Implements hook_tokens().

File

./webform.tokens.inc, line 884
Builds placeholder replacement tokens for webforms and submissions.

Code

function _webform_token_get_submission_value($value_token, array $options, WebformSubmissionInterface $webform_submission, WebformElementManagerInterface $element_manager, BubbleableMetadata $bubbleable_metadata) {
  $submission_data = $webform_submission
    ->getData();

  // Formats:
  // [html]
  // [values:{element_key}:{format}]
  // [values:{element_key}:{format}:{items}]
  // [values:{element_key}:{format}:html]
  // [values:{element_key}:{format}:{items}:html]
  // [values:{element_key}:{format}:urlencode]
  // [values:{element_key}:{format}:{items}:urlencode]
  // [values:{element_key}:{delta}:{format}]
  // [values:{element_key}:{delta}:{sub-element}]
  $keys = explode(':', $value_token);
  $element_key = array_shift($keys);

  // Build HTML values.
  if ($element_key === 'html' && empty($keys)) {
    $options['html'] = TRUE;
    return _webform_token_get_submission_values($options, $webform_submission);
  }

  // Set default options.
  $options += [
    'html' => FALSE,
  ];

  // Parse suffixes and set options.
  $suffixes = [
    // Indicates the tokens should be formatted as HTML instead of plain text.
    'html',
  ];
  foreach ($suffixes as $suffix) {
    if ($keys && in_array($suffix, $keys)) {
      $keys = array_diff($keys, [
        $suffix,
      ]);
      $options[$suffix] = TRUE;
    }
  }
  $element = $webform_submission
    ->getWebform()
    ->getElement($element_key, TRUE);

  // Exit if form element does not exist.
  if (!$element) {
    return NULL;
  }
  $element_plugin = $element_manager
    ->getElementInstance($element);

  // Always get value for a computed element.
  if ($element_plugin instanceof WebformComputedBase) {
    return $element_plugin
      ->getValue($element, $webform_submission);
  }

  // Always get rendered markup for a markup element.
  if ($element_plugin instanceof WebformMarkupBase) {
    $format_method = empty($options['html']) ? 'buildText' : 'buildHtml';
    $element['#display_on'] = WebformMarkupBase::DISPLAY_ON_BOTH;
    $token_value = $element_manager
      ->invokeMethod($format_method, $element, $webform_submission, $options);
    return \Drupal::service('renderer')
      ->renderPlain($token_value);
  }

  // Exit if no submission data and form element is not a container.
  if (!isset($submission_data[$element_key]) && !$element_plugin
    ->isContainer($element)) {
    return NULL;
  }

  // If multiple value element look for delta.
  if ($keys && $element_plugin
    ->hasMultipleValues($element) && is_numeric($keys[0])) {
    $delta = array_shift($keys);
    $options['delta'] = $delta;
  }
  else {
    $delta = NULL;
  }

  // If composite element look for sub-element key.
  if ($keys && $element_plugin
    ->isComposite() && method_exists($element_plugin, 'getInitializedCompositeElement') && $element_plugin
    ->getInitializedCompositeElement($element, $keys[0])) {
    $composite_key = array_shift($keys);
    $options['composite_key'] = $composite_key;
  }
  else {
    $composite_key = NULL;
  }

  /****************************************************************************/

  // Get value.

  /****************************************************************************/

  // Set entity reference chaining.
  if ($keys && $keys[0] === 'entity' && $element_plugin instanceof WebformElementEntityReferenceInterface) {

    // Remove entity from keys.
    array_shift($keys);

    // Get entity value, type, instance, and token.
    if ($entity = $element_plugin
      ->getTargetEntity($element, $webform_submission, $options)) {
      $entity_type = $entity
        ->getEntityTypeId();

      // Map entity type id to entity token name.
      $entity_token_names = [
        // Taxonomy tokens are not prefixed with 'taxonomy_'.
        // @see taxonomy_token_info()
        'taxonomy_term' => 'term',
        'taxonomy_vocabulary' => 'vocabulary',
      ];
      $entity_token_name = isset($entity_token_names[$entity_type]) ? $entity_token_names[$entity_type] : $entity_type;
      $entity_token = implode(':', $keys);
      $token_value = Markup::create(\Drupal::token()
        ->replace("[{$entity_token_name}:{$entity_token}]", [
        $entity_token_name => $entity,
      ], $options, $bubbleable_metadata));
      return $token_value;
    }
    else {
      return '';
    }
  }

  // Set checked/selected for an options elements.
  if ($keys && in_array($keys[0], [
    'checked',
    'selected',
  ]) && $element_plugin
    ->hasProperty('options')) {
    $token_values = (array) $element_plugin
      ->getValue($element, $webform_submission);
    return $token_values && in_array($keys[1], $token_values) ? '1' : '0';
  }

  // Set format and items format.
  if ($keys) {
    if ($composite_key) {

      // Must set '#webform_composite_elements' format.
      // @see \Drupal\webform\Plugin\WebformElement\WebformCompositeBase::initialize
      // @see \Drupal\webform\Plugin\WebformElement\WebformCompositeBase::getInitializedCompositeElement
      $element['#webform_composite_elements'][$composite_key]['#format'] = array_shift($keys);
    }
    else {
      $element['#format'] = array_shift($keys);
    }
  }
  if ($keys) {
    $element['#format_items'] = array_shift($keys);
  }
  $token = "[webform_submission:values:{$value_token}]";
  if (WebformLogicHelper::startRecursionTracking($token) === FALSE) {
    return '';
  }
  $format_method = empty($options['html']) ? 'formatText' : 'formatHtml';
  $token_value = $element_manager
    ->invokeMethod($format_method, $element, $webform_submission, $options);
  if (is_array($token_value)) {

    // Note, tokens can't include CSS and JS libraries since they will
    // can be included in an email.
    $token_value = \Drupal::service('renderer')
      ->renderPlain($token_value);
  }
  elseif (isset($element['#format']) && $element['#format'] === 'raw') {

    // Make sure raw tokens are always rendered AS-IS.
    $token_value = Markup::create((string) $token_value);
  }
  elseif (!$token_value instanceof MarkupInterface) {

    // All strings will be escaped as HtmlEscapedText.
    // @see \Drupal\Core\Utility\Token::replace
    // @see \Drupal\Component\Render\HtmlEscapedText
    $token_value = (string) $token_value;
  }
  if (WebformLogicHelper::stopRecursionTracking($token) === FALSE) {
    return '';
  }
  return $token_value;
}