You are here

protected function WebformElementBase::formatCustomItem in Webform 8.5

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

Format an element's item using custom HTML or plain text.

Parameters

string $type: The format type, HTML or Text.

array $element: An element.

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

array $options: An array of options.

array $context: (optional) Context to be passed to inline Twig template.

Return value

array|string The element's item formatted as plain text or a render array.

3 calls to WebformElementBase::formatCustomItem()
ContainerBase::formatCustomItem in src/Plugin/WebformElement/ContainerBase.php
Format an element's item using custom HTML or plain text.
WebformCompositeBase::formatCustomItem in src/Plugin/WebformElement/WebformCompositeBase.php
Format an element's item using custom HTML or plain text.
WebformElementBase::format in src/Plugin/WebformElementBase.php
Format an element's value as HTML or plain text.
2 methods override WebformElementBase::formatCustomItem()
ContainerBase::formatCustomItem in src/Plugin/WebformElement/ContainerBase.php
Format an element's item using custom HTML or plain text.
WebformCompositeBase::formatCustomItem in src/Plugin/WebformElement/WebformCompositeBase.php
Format an element's item using custom HTML or plain text.

File

src/Plugin/WebformElementBase.php, line 1651

Class

WebformElementBase
Provides a base class for a webform element.

Namespace

Drupal\webform\Plugin

Code

protected function formatCustomItem($type, array &$element, WebformSubmissionInterface $webform_submission, array $options = [], array $context = []) {
  $name = strtolower($type);

  // Get template.
  $template = trim($element['#format_' . $name]);

  // Get context.value.
  $context['value'] = $this
    ->getValue($element, $webform_submission, $options);

  // Get content.item.
  $context['item'] = [];

  // Parse item.format from template and add to context.
  if (preg_match_all("/item(?:\\[['\"]|\\.)([a-zA-Z0-9-_:]+)/", $template, $matches)) {
    $formats = array_unique($matches[1]);
    $item_function = 'format' . $type . 'Item';
    foreach ($formats as $format) {
      $context['item'][$format] = $this
        ->{$item_function}([
        '#format' => $format,
      ] + $element, $webform_submission, $options);
    }
  }

  // Return inline template.
  if ($type === 'Text') {
    return WebformTwigExtension::renderTwigTemplate($webform_submission, $template, $options, $context);
  }
  else {
    return WebformTwigExtension::buildTwigTemplate($webform_submission, $template, $options, $context);
  }
}