You are here

protected function YamlFormElementBase::setConfigurationFormDefaultValue in YAML Form 8

Set an element's configuration form element default value.

Parameters

array $form: An element's configuration form.

array $element_properties: The element's properties without hash prefix.

array $property_element: The form input used to set an element's property.

string $property_name: THe property's name.

3 calls to YamlFormElementBase::setConfigurationFormDefaultValue()
DateList::setConfigurationFormDefaultValue in src/Plugin/YamlFormElement/DateList.php
Set an element's configuration form element default value.
ProcessedText::setConfigurationFormDefaultValue in src/Plugin/YamlFormElement/ProcessedText.php
Set an element's configuration form element default value.
YamlFormElementBase::setConfigurationFormDefaultValueRecursive in src/YamlFormElementBase.php
Set configuration form default values recursively.
2 methods override YamlFormElementBase::setConfigurationFormDefaultValue()
DateList::setConfigurationFormDefaultValue in src/Plugin/YamlFormElement/DateList.php
Set an element's configuration form element default value.
ProcessedText::setConfigurationFormDefaultValue in src/Plugin/YamlFormElement/ProcessedText.php
Set an element's configuration form element default value.

File

src/YamlFormElementBase.php, line 1352

Class

YamlFormElementBase
Provides a base class for a form element.

Namespace

Drupal\yamlform

Code

protected function setConfigurationFormDefaultValue(array &$form, array &$element_properties, array &$property_element, $property_name) {
  $default_value = $element_properties[$property_name];
  $type = isset($property_element['#type']) ? $property_element['#type'] : NULL;
  switch ($type) {
    case 'entity_autocomplete':
      $target_type = $property_element['#target_type'];
      $target_storage = $this->entityTypeManager
        ->getStorage($target_type);
      if (!empty($property_element['#tags'])) {
        $property_element['#default_value'] = $default_value ? $target_storage
          ->loadMultiple($default_value) : [];
      }
      else {
        $property_element['#default_value'] = $default_value ? $target_storage
          ->load($default_value) : NULL;
      }
      break;
    case 'radios':
    case 'select':

      // Handle invalid default_value throwing
      // "An illegal choice has been detected..." error.
      if (!is_array($default_value) && isset($property_element['#options'])) {
        $flattened_options = OptGroup::flattenOptions($property_element['#options']);
        if (!isset($flattened_options[$default_value])) {
          $default_value = NULL;
        }
      }
      $property_element['#default_value'] = $default_value;
      break;
    default:

      // Convert default_value array into a comma delimited list.
      // This is applicable to elements that support #multiple #options.
      if (is_array($default_value) && $property_name == 'default_value' && !$this
        ->isComposite()) {
        $property_element['#default_value'] = implode(', ', $default_value);
      }
      else {
        $property_element['#default_value'] = $default_value;
      }
      break;
  }
  $property_element['#parents'] = [
    'properties',
    $property_name,
  ];
  unset($element_properties[$property_name]);
}