You are here

public function YamlFormCompositeBase::formatText in YAML Form 8

Format an element's value as plain text.

Parameters

array $element: An element.

array|mixed $value: A value.

array $options: An array of options.

Return value

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

Overrides YamlFormElementBase::formatText

File

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

Class

YamlFormCompositeBase
Provides a base for composite elements.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

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

  // Return empty value.
  if (empty($value) || is_array($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 = $composite_element['#title'];
        $composite_value = $value[$composite_key];
        if ($composite_value !== '') {
          $items[$composite_key] = "{$composite_title}: {$composite_value}";
        }
      }
      return implode("\n", $items);
    case 'raw':
      $items = [];
      $composite_elements = $this
        ->getInitializedCompositeElement($element);
      foreach (RenderElement::children($composite_elements) as $composite_key) {
        $composite_value = $value[$composite_key];
        if ($composite_value !== '') {
          $items[$composite_key] = "{$composite_key}: {$composite_value}";
        }
      }
      return implode("\n", $items);
    default:
      $lines = $this
        ->formatLines($element, $value);
      return implode("\n", $lines);
  }
}