You are here

public static function YamlFormCompositeBase::processYamlFormComposite in YAML Form 8

Processes a composite form element.

1 call to YamlFormCompositeBase::processYamlFormComposite()
YamlFormLocation::processYamlFormComposite in src/Element/YamlFormLocation.php
Processes a composite form element.
1 method overrides YamlFormCompositeBase::processYamlFormComposite()
YamlFormLocation::processYamlFormComposite in src/Element/YamlFormLocation.php
Processes a composite form element.

File

src/Element/YamlFormCompositeBase.php, line 88

Class

YamlFormCompositeBase
Provides an base composite form element.

Namespace

Drupal\yamlform\Element

Code

public static function processYamlFormComposite(&$element, FormStateInterface $form_state, &$complete_form) {
  if (isset($element['#initialize'])) {
    return $element;
  }
  $element['#initialize'] = TRUE;
  $element['#tree'] = TRUE;
  $composite_elements = static::getCompositeElements();
  foreach ($composite_elements as $composite_key => &$composite_element) {

    // Transfer '#{composite_key}_{property}' from main element to composite
    // element.
    foreach ($element as $property_key => $property_value) {
      if (strpos($property_key, '#' . $composite_key . '__') === 0) {
        $composite_property_key = str_replace('#' . $composite_key . '__', '#', $property_key);
        $composite_element[$composite_property_key] = $property_value;
      }
    }
    if (isset($element['#value'][$composite_key])) {
      $composite_element['#value'] = $element['#value'][$composite_key];
    }

    // Always set #access which is used to hide/show the elements container.
    $composite_element += [
      '#access' => TRUE,
    ];

    // Never required hidden composite elements.
    if ($composite_element['#access'] == FALSE) {
      unset($composite_element['#required']);
    }

    // Load options.
    if (isset($composite_element['#options'])) {
      $composite_element['#options'] = YamlFormOptionsEntity::getElementOptions($composite_element);
    }

    // Handle #type specific customizations.
    if (isset($composite_element['#type'])) {
      switch ($composite_element['#type']) {
        case 'select':
        case 'yamlform_select_other':

          // Always include an empty option, even if the composite element
          // is not required.
          // @see https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!Element!Select.php/class/Select/8.2.x
          // Use placeholder as empty option.
          if (!isset($composite_element['#empty_option'])) {
            if (isset($composite_element['#placeholder'])) {
              $composite_element['#empty_option'] = $composite_element['#placeholder'];
            }
            elseif (empty($composite_element['#required'])) {
              $composite_element['#empty_option'] = t('- None -');
            }
          }
          break;
      }
    }
  }
  $element += $composite_elements;
  $element['#element_validate'] = [
    [
      get_called_class(),
      'validateYamlFormComposite',
    ],
  ];
  if (!empty($element['#flexbox'])) {
    $element['#attached']['library'][] = 'yamlform/yamlform.element.flexbox';
  }
  return $element;
}