You are here

protected function WebformCompositeBase::formatCustomItem in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/WebformCompositeBase.php \Drupal\webform\Plugin\WebformElement\WebformCompositeBase::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.

Overrides WebformElementBase::formatCustomItem

2 calls to WebformCompositeBase::formatCustomItem()
WebformCompositeBase::formatHtmlItem in src/Plugin/WebformElement/WebformCompositeBase.php
Format an element's value as HTML.
WebformCompositeBase::formatTextItem in src/Plugin/WebformElement/WebformCompositeBase.php
Format an element's value as text.

File

src/Plugin/WebformElement/WebformCompositeBase.php, line 323

Class

WebformCompositeBase
Provides a base for composite elements.

Namespace

Drupal\webform\Plugin\WebformElement

Code

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

  // Parse element.composite_key from template and add composite element
  // to context.
  $template = trim($element['#format_' . $name]);
  if (preg_match_all("/element(?:\\[['\"]|\\.)([a-zA-Z0-9-_:]+)/", $template, $matches)) {
    $composite_elements = $this
      ->getInitializedCompositeElement($element);
    $composite_keys = array_unique($matches[1]);
    $item_function = 'format' . $type;
    $context['element'] = [];
    foreach ($composite_keys as $composite_key) {
      if (isset($composite_elements[$composite_key])) {
        $context['element'][$composite_key] = $this
          ->{$item_function}([
          '#format' => NULL,
        ] + $element, $webform_submission, [
          'composite_key' => $composite_key,
        ] + $options);
      }
    }
  }
  return parent::formatCustomItem($type, $element, $webform_submission, $options, $context);
}