You are here

protected function ComponentSectionForm::componentPropertiesForm in Module Builder 8.3

Add form elements for the specified component properties.

Parameters

$form: The form array.

FormStateInterface $form_state: The form state object.

Return value

The form array.

1 call to ComponentSectionForm::componentPropertiesForm()
ComponentSectionForm::form in src/Form/ComponentSectionForm.php
Gets the actual form array to be built.

File

src/Form/ComponentSectionForm.php, line 80

Class

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

Namespace

Drupal\module_builder\Form

Code

protected function componentPropertiesForm($form, FormStateInterface $form_state) {
  if (!$form_state
    ->has('data')) {

    // The first time we show this form, create the typed data object.
    $component_data = $this
      ->getComponentDataObject();
    $first_load = TRUE;
    $form_state
      ->set('data', $component_data);
  }
  else {

    // During an AJAX rebuild, get the data from the form state.
    $component_data = $form_state
      ->get('data');
  }

  // Get the properties that this form section should show.
  $component_properties_to_use = $this
    ->getFormComponentProperties($component_data);
  if (!empty($first_load)) {

    // Warn about properties in the entity annotation that are not in the
    // data.
    $undefined_properties = array_diff($component_properties_to_use, array_keys($component_data
      ->getProperties()));
    foreach ($undefined_properties as $property_name) {
      $this
        ->messenger()
        ->addError(t("The property '@name' is not defined in Drupal Code Builder. You should ensure you are using an up-to-date version.", [
        '@name' => $property_name,
      ]));
    }
  }
  $component_properties_to_use = array_intersect($component_properties_to_use, array_keys($component_data
    ->getProperties()));

  // Set #tree on the data element.
  $form['module']['#tree'] = TRUE;
  foreach ($component_properties_to_use as $property_name) {
    $this
      ->buildFormElement($form['module'], $form_state, $component_data->{$property_name});
  }

  // Put the data back into the form state, as the building of the form
  // elements may have caused changes.
  $form_state
    ->set('data', $component_data);

  // Developer trapdoor: disable AJAX for easier debugging of the form.
  if (FALSE) {

    // TODO: AAAAAARGH why can't this be done with Iterator classes???
    static::removeAjax($form);
  }
  return $form;
}