You are here

public function YamlFormTableTrait::prepare in YAML Form 8

File

src/Plugin/YamlFormElement/YamlFormTableTrait.php, line 16

Class

YamlFormTableTrait
Provides a 'table' trait.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

public function prepare(array &$element, YamlFormSubmissionInterface $yamlform_submission) {
  parent::prepare($element, $yamlform_submission);

  // Add missing element class.
  $element['#attributes']['class'][] = str_replace('_', '-', $element['#type']);

  // Add one column header is not #header is specified.
  if (!isset($element['#header'])) {
    $element['#header'] = [
      isset($element['#title']) ? $element['#title'] : '',
    ];
  }

  // Convert associative array of options into one column row.
  if (isset($element['#options'])) {
    foreach ($element['#options'] as $options_key => $options_value) {
      if (is_string($options_value)) {
        $element['#options'][$options_key] = [
          [
            'value' => $options_value,
          ],
        ];
      }
    }
  }
  if ($this
    ->hasMultipleValues($element)) {
    $element['#element_validate'][] = [
      get_class($this),
      'validateMultipleOptions',
    ];
  }
  $element['#attached']['library'][] = 'yamlform/yamlform.element.' . $element['#type'];
}