You are here

protected function ComponentSectionForm::XgetFormElementValue in Module Builder 8.3

File

src/Form/ComponentSectionForm.php, line 1587

Class

ComponentSectionForm
Generic form for entering a section of data for a component.

Namespace

Drupal\module_builder\Form

Code

protected function XgetFormElementValue($value_address, $value, FormStateInterface $form_state) {

  // Retrieve the handling type from the form state.
  $property_form_value_address_key = implode(':', $value_address);
  $handling = $form_state
    ->get([
    'element_handling',
    $property_form_value_address_key,
  ]);
  switch ($handling) {
    case 'textarea':

      // Array format, without options: textarea.
      if (empty($value)) {
        $value = [];
      }
      else {

        // Can't split on just "\n" because for FKW reasons, linebreaks come
        // back through POST as Windows-style "\r\n".
        $value = preg_split("@[\r\n]+@", $value);
      }
      break;
    case 'autocomplete':

      // Array format, with extra options: textfield with autocomplete.
      // Only explode a non-empty string, as explode() will turn '' into an
      // array!
      if (!empty($value)) {

        // Textfield with autocomplete.
        $value = preg_split("@,\\s*@", $value);
      }
      break;
    case 'checkboxes':

      // Array format, with options: checkboxes.
      // Filter out empty values. (FormAPI *still* doesn't do this???)
      $value = array_filter($value);

      // Don't store values also in the keys, as some of these have dots in
      // them, which ConfigAPI doesn't allow.
      $value = array_keys($value);
      break;
    case 'compound':

      // Remove the item count buttons from the values.
      unset($value['actions']);
      unset($value['table']);
      foreach ($value as $delta => $item_value) {
        $delta_value_address = $value_address;
        $delta_value_address[] = $delta;

        // Recurse into the child property values.
        foreach ($item_value as $child_key => $child_value) {
          $delta_child_value_address = $delta_value_address;
          $delta_child_value_address[] = $child_key;
          $value[$delta][$child_key] = $this
            ->getFormElementValue($delta_child_value_address, $child_value, $form_state);
        }
      }
      break;
    case 'checkbox':
    case 'select':
    case 'textfield':

      // Nothing to do in these cases: $value is fine as it is.
      break;
    default:
      throw new \Exception("Unknown handling type: {$handling}.");
  }
  return $value;
}