You are here

public function YamlFormSubmissionViewBuilder::buildElements in YAML Form 8

Build element display items from elements and submitted data.

Parameters

array $elements: Form elements.

array $data: Submission data.

array $options:

  • excluded_elements: An array of elements to be excluded.
  • email: Format element to be send via email.

string $format: Output format set to html or text.

Return value

array A render array displaying the submitted values.

Overrides YamlFormSubmissionViewBuilderInterface::buildElements

File

src/YamlFormSubmissionViewBuilder.php, line 130

Class

YamlFormSubmissionViewBuilder
Render controller for form submissions.

Namespace

Drupal\yamlform

Code

public function buildElements(array $elements, array $data, array $options = [], $format = 'html') {
  $build_method = 'build' . ucfirst($format);
  $build = [];
  foreach ($elements as $key => $element) {
    if (!is_array($element) || Element::property($key) || !$this
      ->isVisibleElement($element) || isset($options['excluded_elements'][$key])) {
      continue;
    }
    $plugin_id = $this->elementManager
      ->getElementPluginId($element);

    /** @var \Drupal\yamlform\YamlFormElementInterface $yamlform_element */
    $yamlform_element = $this->elementManager
      ->createInstance($plugin_id);

    // Check element view access.
    if (!$yamlform_element
      ->checkAccessRules('view', $element)) {
      continue;
    }
    if ($yamlform_element
      ->isContainer($element)) {
      $children = $this
        ->buildElements($element, $data, $options, $format);
      if ($children) {

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

      // Build the container but make sure it is not empty. Containers
      // (ie details, fieldsets, etc...) without children will be empty
      // but markup should always be rendered.
      if ($build_container = $yamlform_element
        ->{$build_method}($element, $children, $options)) {
        $build[$key] = $build_container;
      }
    }
    else {
      $value = isset($data[$key]) ? $data[$key] : NULL;
      if ($build_element = $yamlform_element
        ->{$build_method}($element, $value, $options)) {
        $build[$key] = $build_element;
      }
    }
  }
  return $build;
}