You are here

public function YamlFormCompositeBase::formatHtml in YAML Form 8

Format an element's value as HTML.

Parameters

array $element: An element.

array|mixed $value: A value.

array $options: An array of options.

Return value

array|string The element's value formatted as an HTML string or a render array.

Overrides YamlFormElementBase::formatHtml

1 call to YamlFormCompositeBase::formatHtml()
YamlFormCompositeBase::formatTableColumn in src/Plugin/YamlFormElement/YamlFormCompositeBase.php
Format an element's table column value.

File

src/Plugin/YamlFormElement/YamlFormCompositeBase.php, line 411

Class

YamlFormCompositeBase
Provides a base for composite elements.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

public function formatHtml(array &$element, $value, array $options = []) {

  // Return empty value.
  if (empty($value) || empty(array_filter($value))) {
    return '';
  }
  $format = $this
    ->getFormat($element);
  switch ($format) {
    case 'list':
      $items = [];
      $composite_elements = $this
        ->getInitializedCompositeElement($element);
      foreach (RenderElement::children($composite_elements) as $composite_key) {
        $composite_element = $composite_elements[$composite_key];
        $composite_title = isset($composite_element['#title']) ? $composite_element['#title'] : $composite_key;
        $composite_value = isset($value[$composite_key]) ? $value[$composite_key] : '';
        if ($composite_value !== '') {
          $items[$composite_key] = [
            '#markup' => "<b>{$composite_title}:</b> {$composite_value}",
          ];
        }
      }
      return [
        '#theme' => 'item_list',
        '#items' => $items,
      ];
    case 'raw':
      $items = [];
      $composite_elements = $this
        ->getInitializedCompositeElement($element);
      foreach (RenderElement::children($composite_elements) as $composite_key) {
        $composite_value = isset($value[$composite_key]) ? $value[$composite_key] : '';
        if ($composite_value !== '') {
          $items[$composite_key] = [
            '#markup' => "<b>{$composite_key}:</b> {$composite_value}",
          ];
        }
      }
      return [
        '#theme' => 'item_list',
        '#items' => $items,
      ];
    default:
      $lines = $this
        ->formatLines($element, $value);
      foreach ($lines as $key => $line) {
        if ($key == 'email') {
          $lines[$key] = [
            '#type' => 'link',
            '#title' => $line,
            '#url' => \Drupal::pathValidator()
              ->getUrlIfValid('mailto:' . $line),
          ];
        }
        else {
          $lines[$key] = [
            '#markup' => $line,
          ];
        }
        $lines[$key]['#suffix'] = '<br/>';
      }
      return $lines;
  }
}