You are here

protected function ModuleBuilderComponentFormBase::getFormElementValue in Module Builder 7.2

Get the value for a property from the form values.

This performs various processing depending on the form element type and the property format:

  • explode textarea values
  • filter checkboxes and store only the keys
  • recurse into compound properties

The form build process leaves instructions for how to handle each value in the 'element_handling' form state setting, so that here we don't need to repeat the logic based on property info. Furthermore, we can't put this a property info array into form state storage, because it contains closures, which don't survive the serialization process in the database, and so the property info would need to be run through DCB's preparation process all over again.

Parameters

array $value_address: The address array of the value in the form state values array. The final element of this is name of the property and the form element.

$value: The incoming form value from the form element for this property.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

The processed value.

1 call to ModuleBuilderComponentFormBase::getFormElementValue()
ModuleBuilderComponentFormBase::copyFormValuesToEntity in includes/module_builder.form.inc
Copies top-level form values to entity properties

File

includes/module_builder.form.inc, line 726

Class

ModuleBuilderComponentFormBase
Backport of ComponentFormBase from 8.x-3.x version.

Code

protected function getFormElementValue($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.
      // Only explode a non-empty string, as explode() will turn '' into an
      // array!
      if (!empty($value)) {

        // Need to split on any whitespace rather than "\n" because for FKW
        // reasons, linebreaks come back through POST as Windows-style "\r\n".
        $value = preg_split("@\\s+@", $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;
}