You are here

protected function ContainerBase::build in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/ContainerBase.php \Drupal\webform\Plugin\WebformElement\ContainerBase::build()

Build an element as text or HTML.

Parameters

string $format: Format of the element, text or html.

array $element: An element.

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

array $options: An array of options.

Return value

array A render array representing an element as text or HTML.

Overrides WebformElementBase::build

2 methods override ContainerBase::build()
WebformActions::build in src/Plugin/WebformElement/WebformActions.php
Build an element as text or HTML.
WebformFlexbox::build in src/Plugin/WebformElement/WebformFlexbox.php
Build an element as text or HTML.

File

src/Plugin/WebformElement/ContainerBase.php, line 81

Class

ContainerBase
Provides a base 'container' class.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function build($format, array &$element, WebformSubmissionInterface $webform_submission, array $options = []) {
  $format_function = 'format' . ucfirst($format);
  $formatted_value = $this
    ->{$format_function}($element, $webform_submission, $options);
  if (empty($formatted_value)) {
    return NULL;
  }
  if (is_array($formatted_value)) {

    // Add #first and #last property to $children.
    // This is used to remove returns from #last with multiple lines of
    // text.
    // @see webform-element-base-text.html.twig
    reset($formatted_value);
    $first_key = key($formatted_value);
    if (isset($formatted_value[$first_key]['#options'])) {
      $formatted_value[$first_key]['#options']['first'] = TRUE;
    }
    end($formatted_value);
    $last_key = key($formatted_value);
    if (isset($formatted_value[$last_key]['#options'])) {
      $formatted_value[$last_key]['#options']['last'] = TRUE;
    }
  }
  return [
    '#theme' => 'webform_container_base_' . $format,
    '#element' => $element,
    '#value' => $formatted_value,
    '#webform_submission' => $webform_submission,
    '#options' => $options,
  ];
}