You are here

protected function ComponentSectionForm::XelementArray in Module Builder 8.3

Set form element properties specific to array component properties.

Parameters

&$element: The form element for the component property.

FormStateInterface $form_state: The form state.

$property_info: The info array for the component property.

$form_default_value: The default value for the form element.

Return value

string The handling type to be applied to this element's value on submit.

File

src/Form/ComponentSectionForm.php, line 1257

Class

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

Namespace

Drupal\module_builder\Form

Code

protected function XelementArray(&$element, FormStateInterface $form_state, $property_info, $form_default_value) {
  if (isset($property_info['options'])) {
    if (isset($property_info['options_extra'])) {

      // Show an autocomplete textfield.
      // TODO: use Select or Other module for this when it has a stable
      // release.
      $element['#type'] = 'textfield';
      $element['#maxlength'] = 512;
      $element['#description'] = (isset($element['#description']) ? $element['#description'] . ' ' : '') . t("Enter multiple values separated with a comma.");
      $element['#autocomplete_route_name'] = 'module_builder.autocomplete';
      $element['#autocomplete_route_parameters'] = [
        'property_address' => implode(':', $element['#mb_property_address']),
      ];
      if ($form_default_value) {
        $form_default_value = implode(', ', $form_default_value);
      }
      $handling = 'autocomplete';
    }
    else {
      $element['#type'] = 'checkboxes';
      $element['#options'] = $property_info['options'];
      if (is_null($form_default_value)) {
        $form_default_value = [];
      }
      else {
        $form_default_value = array_combine($form_default_value, $form_default_value);
      }
      $handling = 'checkboxes';
    }
  }
  else {
    $element['#type'] = 'textarea';
    if (isset($element['#description'])) {
      $element['#description'] .= ' ';
    }
    else {
      $element['#description'] = '';
    }
    $element['#description'] .= t("Enter one item per line.");

    // Handle a property that DCB has added since the component was saved.
    if (empty($form_default_value) && !is_array($form_default_value)) {
      $form_default_value = [];
    }
    $form_default_value = implode("\n", $form_default_value);
    $handling = 'textarea';
  }
  $element['#default_value'] = $form_default_value;
  return $handling;
}