You are here

public function BetterFieldDescriptionsSettingsForm::buildForm in Better Field Descriptions 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/BetterFieldDescriptionsSettingsForm.php, line 69

Class

BetterFieldDescriptionsSettingsForm
Displays the better_field_descriptions settings form.

Namespace

Drupal\better_field_descriptions\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Get info on bundles.
  $all_bundles = $this->bundleInfoService
    ->getAllBundleInfo();

  // Sort into order on entity ids.
  ksort($all_bundles);

  // Get editable config.
  $config = $this
    ->config('better_field_descriptions.settings');

  // Get list of entity types selected for better desctiptions.
  $bfde = $config
    ->get('better_field_descriptions_entities');

  // Get list of fields selected for better descriptions.
  $bfds = $config
    ->get('better_field_descriptions_settings');
  $form['descriptions'] = [
    '#type' => 'markup',
    '#markup' => $this
      ->t('Select fields that should have better descriptions.'),
  ];
  $form['bundles'] = [
    '#type' => 'item',
    '#prefix' => '<div id="better-descriptions-form-id-wrapper">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  ];
  foreach ($all_bundles as $entity_type => $bundles) {
    if (in_array($entity_type, $bfde)) {
      foreach ($bundles as $bundle => $label) {

        // Array to hold fields in the node.
        $fields_instances = [];

        // Get info on pseudo fields, like title.
        $extra_fields = $this->entityFieldManager
          ->getExtraFields($entity_type, $bundle, 'form');
        if (isset($extra_fields['title'])) {
          $fields_instances['title'] = $extra_fields['title']['label'];
        }

        // Get info on regular fields to the bundle.
        $fields = $this->entityFieldManager
          ->getFieldDefinitions($entity_type, $bundle);
        foreach ($fields as $field_machine_name => $field) {
          if ($field
            ->getFieldStorageDefinition()
            ->isBaseField() == FALSE) {
            $fields_instances[$field_machine_name] = $field
              ->getLabel() . ' (' . $field_machine_name . ')';
          }
        }
        foreach ($fields_instances as $field => $label) {
          $enabled = isset($bfds[$entity_type][$bundle][$field]);
          $form['bundles'][$entity_type][$bundle][$field] = [
            '#type' => 'checkbox',
            '#title' => $label,
            '#default_value' => $enabled,
          ];
          $form['bundles'][$entity_type][$bundle][$field]['#parents'] = [
            'bundles',
            $entity_type,
            $bundle,
            $field,
          ];
        }
      }
    }
  }
  foreach (Element::children($form['bundles']) as $entity_type) {
    $form['bundles'][$entity_type] += [
      '#type' => 'details',
      '#title' => $entity_type,
    ];
    foreach (Element::children($form['bundles'][$entity_type]) as $bundle) {
      $form['bundles'][$entity_type][$bundle] += [
        '#type' => 'details',
        '#title' => $bundle,
      ];
    }
    uasort($form['bundles'][$entity_type], [
      '\\Drupal\\Component\\Utility\\SortArray',
      'sortByTitleProperty',
    ]);
  }

  // Lastly, sort all packages by title.
  uasort($form['bundles'], [
    '\\Drupal\\Component\\Utility\\SortArray',
    'sortByTitleProperty',
  ]);
  return parent::buildForm($form, $form_state);
}