View source
<?php
namespace Drupal\better_field_descriptions\Form;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityFieldManager;
class BetterFieldDescriptionsFieldsForm extends ConfigFormBase {
private $entityFieldManager;
protected $bundleInfoService;
public function __construct(ConfigFactoryInterface $config_factory, EntityFieldManager $entityFieldManager, EntityTypeBundleInfoInterface $bundle_info_service) {
parent::__construct($config_factory);
$this->entityFieldManager = $entityFieldManager;
$this->bundleInfoService = $bundle_info_service;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_field.manager'), $container
->get('entity_type.bundle.info'));
}
protected function getEditableConfigNames() {
return [
'better_field_descriptions.settings',
];
}
public function getFormId() {
return 'better_field_descriptions_fields_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$bfds = $this
->config('better_field_descriptions.settings')
->get('better_field_descriptions_settings');
$bfd = $this
->config('better_field_descriptions.settings')
->get('better_field_descriptions');
if (isset($bfd['template']) == FALSE || empty($bfd['template'])) {
$bfd['template'] = 'better-field-descriptions-text';
}
$path = drupal_get_path('module', 'better_field_descriptions');
$files = glob("{$path}/templates/*.html.twig", GLOB_BRACE);
$templates = [];
foreach ($files as $key => $value) {
$templates[] = basename($value, ".html.twig");
}
$form['#templates'] = $templates;
$better_descriptions_templates = [];
foreach ($templates as $template) {
if (($pos = strpos($template, '.')) !== FALSE) {
$template = substr($template, 0, $pos);
}
$better_descriptions_templates[$template] = $template;
}
$positions = [
0 => $this
->t('Above title and input'),
1 => $this
->t('Below title and input'),
2 => $this
->t('Between title and input'),
];
$form['descriptions'] = [
'#type' => 'markup',
'#markup' => $this
->t('Add/edit better descriptions to the fields below.'),
];
$form['bundles'] = [
'#type' => 'item',
'#prefix' => '<div id="better-field-descriptions-form-id-wrapper">',
'#suffix' => '</div>',
'#tree' => TRUE,
];
$form['bundles']['template'] = [
'#type' => 'select',
'#title' => $this
->t('Select template for the descriptions'),
'#options' => $better_descriptions_templates,
'#default_value' => $bfd['template'],
'#description' => $this
->t('Changing this value will trigger a theme registry rebuild. You can also provide your own template, consult the documentation.'),
];
if (isset($bfd['default_label']) == FALSE) {
$default_label = $this
->t('Description');
}
else {
$default_label = $bfd['default_label'];
}
$form['bundles']['default_label'] = [
'#type' => 'textfield',
'#title' => 'Default label for all field descriptions.',
'#default_value' => $default_label,
'#description' => $this
->t('This label will be used if not set in each of the fields below.'),
];
$all_bundles = $this->bundleInfoService
->getAllBundleInfo();
foreach ($bfds as $entity_type => $entity_bundles) {
$form['bundles'][$entity_type] = [
'#type' => 'details',
'#title' => $entity_type,
];
foreach ($entity_bundles as $bundle_machine_name => $fields) {
$fields_instances = [];
$extra_fields = $this->entityFieldManager
->getExtraFields($entity_type, $bundle_machine_name, 'form');
if (isset($extra_fields['title'])) {
$fields_instances['title'] = $extra_fields['title'];
}
$fields_instances += $this->entityFieldManager
->getFieldDefinitions($entity_type, $bundle_machine_name);
$form['bundles'][$entity_type][$bundle_machine_name] = [
'#type' => 'details',
'#title' => $all_bundles[$entity_type][$bundle_machine_name]['label'],
];
foreach ($fields as $field_machine_name) {
if (!isset($fields_instances[$field_machine_name])) {
continue;
}
$bfd_description = '';
if (isset($bfd[$entity_type][$bundle_machine_name][$field_machine_name]['description'])) {
$bfd_description = $bfd[$entity_type][$bundle_machine_name][$field_machine_name]['description'];
}
$form['bundles'][$entity_type][$bundle_machine_name][$field_machine_name]['description'] = [
'#type' => 'textarea',
'#title' => $fields_instances[$field_machine_name]
->getLabel() . ' (' . $field_machine_name . ')',
'#default_value' => FieldFilteredMarkup::create($bfd_description),
'#description' => $this
->t('Add description for @machine_name.', [
'@machine_name' => $fields_instances[$field_machine_name]
->getLabel(),
]),
];
$bfd_label = '';
if (isset($bfd[$entity_type][$bundle_machine_name][$field_machine_name]['label'])) {
$bfd_label = $bfd[$entity_type][$bundle_machine_name][$field_machine_name]['label'];
}
$form['bundles'][$entity_type][$bundle_machine_name][$field_machine_name]['label'] = [
'#type' => 'textfield',
'#title' => 'Label for this field description',
'#default_value' => FieldFilteredMarkup::create($bfd_label),
'#description' => $this
->t('Label for this field description.'),
];
$position = 1;
if (isset($bfd[$entity_type][$bundle_machine_name][$field_machine_name]['position'])) {
$position = $bfd[$entity_type][$bundle_machine_name][$field_machine_name]['position'];
}
$form['bundles'][$entity_type][$bundle_machine_name][$field_machine_name]['position'] = [
'#type' => 'radios',
'#title' => 'Position of description.',
'#options' => $positions,
'#default_value' => $position,
'#description' => $this
->t('Position the description field above or below the input field. Using the between-option can cause unexpected results: any label set above will replace the label of the field, and for some kinds of fields the title may also be duplicated. Please review the relevant content form after saving these settings.'),
];
}
}
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$bfd = $this
->config('better_field_descriptions.settings')
->get('better_field_descriptions');
$template_bundle = $form_state
->getValue('bundles');
$path = drupal_get_path('module', 'better_field_descriptions');
$template = $template_bundle['template'];
$template_uri = $path . '/templates/' . $template . '.html.twig';
$form_state
->setValue([
'bundles',
'template_uri',
], $template_uri);
if (isset($bfd['template']) && $template != $bfd['template']) {
drupal_theme_rebuild();
}
$config = $this
->config('better_field_descriptions.settings')
->set('better_field_descriptions', $form_state
->getValue('bundles'));
$config
->save();
parent::submitForm($form, $form_state);
}
}