You are here

protected function WebformElementBase::setConfigurationFormDefaultValue in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElementBase.php \Drupal\webform\Plugin\WebformElementBase::setConfigurationFormDefaultValue()

Set an element's configuration webform element default value.

Parameters

array $form: An element's configuration webform.

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

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

string $property_name: THe property's name.

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

File

src/Plugin/WebformElementBase.php, line 3614

Class

WebformElementBase
Provides a base class for a webform element.

Namespace

Drupal\webform\Plugin

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);
      }
      elseif (is_bool($default_value) && $property_name === 'default_value') {
        $property_element['#default_value'] = $default_value ? 1 : 0;
      }
      elseif (is_null($default_value) && $property_name === 'default_value') {
        $property_element['#default_value'] = (string) $default_value;
      }
      else {
        $property_element['#default_value'] = $default_value;
      }
      break;
  }
  $property_element['#parents'] = [
    'properties',
    $property_name,
  ];
  unset($element_properties[$property_name]);
}