You are here

public function ModuleHooksForm::form in Module Builder 8.3

Gets the actual form array to be built.

Overrides ComponentSectionForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/ModuleHooksForm.php, line 33

Class

ModuleHooksForm
Form for selecting hooks.

Namespace

Drupal\module_builder\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $form['filter'] = [
    '#type' => 'search',
    '#title' => $this
      ->t('Filter'),
    '#title_display' => 'invisible',
    '#size' => 60,
    '#placeholder' => $this
      ->t('Filter by hook name'),
    '#attributes' => [
      'class' => [
        'hooks-filter-text',
      ],
      'data-container' => '.module-builder-hooks',
      'autocomplete' => 'off',
      'title' => $this
        ->t('Enter a part of the hook name to filter by.'),
    ],
  ];

  // Get the Task handler.
  // No need to catch DCB exceptions; create() has already done that.
  // TODO: inject.
  $mb_task_handler_report = \Drupal::service('module_builder.drupal_code_builder')
    ->getTask('ReportHookData');

  // Call a method in the Task handler to perform the operation.
  $hook_info = $mb_task_handler_report
    ->listHookOptionsStructured();
  $form['groups'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'module-builder-hooks',
      ],
    ],
  ];
  $entity_component_data = $this->entity
    ->get('data');

  // Create a fieldset for each group, containing checkboxes.
  foreach ($hook_info as $group => $hook_group_info) {
    $form['groups'][$group] = array(
      '#type' => 'details',
      '#title' => $group,
    );
    $hook_names = array_keys($hook_group_info);

    // Need to differentiate the key, otherwise FormAPI treats this as an
    // error on submit.
    $group_default_value = isset($entity_component_data['hooks']) ? array_intersect($hook_names, $entity_component_data['hooks']) : [];
    $form['groups'][$group][$group . '_hooks'] = array(
      '#type' => 'checkboxes',
      '#options' => array_combine($hook_names, array_column($hook_group_info, 'name')),
      '#default_value' => $group_default_value,
    );
    if (!empty($group_default_value)) {
      $form['groups'][$group]['#open'] = TRUE;
    }
    foreach ($hook_group_info as $hook => $hook_info_single) {
      $description = $hook_info_single['description'];
      if ($hook_info_single['core']) {

        // Get MAJOR, MINOR and PATCH components from the current version string.
        $core_version_parts = explode('.', \Drupal::VERSION);

        // Change the PATCH part to be just 'x'.
        $core_version_parts[2] = 'x';

        // Build MAJOR.MINOR.x version string.
        $core_version_minor = implode('.', $core_version_parts);

        // External Uri.
        $url = Url::fromUri('https://api.drupal.org/api/function/' . $hook . '/' . $core_version_minor);
        $description .= ' ' . Link::fromTextAndUrl(t('[documentation]'), $url)
          ->toString();
      }
      $form['groups'][$group][$group . '_hooks'][$hook]['#description'] = $description;
    }
  }
  $form_state
    ->set('module_builder_groups', array_keys($hook_info));
  $form['#attached']['library'][] = 'module_builder/hooks';
  return $form;
}