BetterFieldDescriptionsEntitiesForm.php in Better Field Descriptions 8
File
src/Form/BetterFieldDescriptionsEntitiesForm.php
View source
<?php
namespace Drupal\better_field_descriptions\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BetterFieldDescriptionsEntitiesForm extends ConfigFormBase {
protected $bundleInfoService;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeBundleInfoInterface $bundle_info_service) {
parent::__construct($config_factory);
$this->bundleInfoService = $bundle_info_service;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.bundle.info'));
}
protected function getEditableConfigNames() {
return [
'better_field_descriptions.settings',
];
}
public function getFormId() {
return 'better_field_descriptions_entities_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$all_bundles = $this->bundleInfoService
->getAllBundleInfo();
ksort($all_bundles);
$config = $this
->config('better_field_descriptions.settings');
$bfde = $config
->get('better_field_descriptions_entities');
$form['descriptions'] = [
'#type' => 'markup',
'#markup' => $this
->t('Select entity types that should have better descriptions.'),
];
$form['entities'] = [
'#type' => 'item',
'#prefix' => '<div id="better-descriptions-form-id-wrapper">',
'#suffix' => '</div>',
'#tree' => TRUE,
];
foreach ($all_bundles as $entity_type => $bundles) {
$form['entities'][$entity_type] = [
'#type' => 'checkbox',
'#title' => $entity_type,
'#default_value' => isset($bfde[$entity_type]),
];
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('better_field_descriptions.settings');
$selected = array_filter($form_state
->getValue('entities'));
$bfde = [];
foreach ($selected as $entity_type => $checked) {
$bfde[$entity_type] = $entity_type;
}
$config
->set('better_field_descriptions_entities', $bfde);
$config
->save();
parent::submitForm($form, $form_state);
}
}