View source
<?php
namespace Drupal\module_builder\Form;
use Drupal\Core\Link;
use Drupal\Core\Render\Element;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\module_builder\ExceptionHandler;
use DrupalCodeBuilder\Exception\SanityException;
use MutableTypedData\Data\DataItem;
class ModuleHooksForm extends ComponentSectionForm {
protected function getFormComponentProperties(DataItem $data) {
return [];
}
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.'),
],
];
$mb_task_handler_report = \Drupal::service('module_builder.drupal_code_builder')
->getTask('ReportHookData');
$hook_info = $mb_task_handler_report
->listHookOptionsStructured();
$form['groups'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'module-builder-hooks',
],
],
];
$entity_component_data = $this->entity
->get('data');
foreach ($hook_info as $group => $hook_group_info) {
$form['groups'][$group] = array(
'#type' => 'details',
'#title' => $group,
);
$hook_names = array_keys($hook_group_info);
$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']) {
$core_version_parts = explode('.', \Drupal::VERSION);
$core_version_parts[2] = 'x';
$core_version_minor = implode('.', $core_version_parts);
$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;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$groups = $form_state
->get('module_builder_groups');
$hooks = [];
foreach ($groups as $group) {
$group_values = $values[$group . '_hooks'];
$group_hooks = array_filter($group_values);
$group_hooks = array_keys($group_hooks);
$hooks = array_merge($group_hooks, $hooks);
}
$data = $entity
->get('data');
$data['hooks'] = $hooks;
$entity
->set('data', $data);
}
}