View source
<?php
namespace Drupal\like_and_dislike\Form;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
class SettingsForm extends ConfigFormBase {
protected $entityTypeManager;
protected $entityFieldManager;
protected $bundleInfoService;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EntityTypeBundleInfoInterface $bundle_info_service) {
parent::__construct($config_factory);
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->bundleInfoService = $bundle_info_service;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('entity_type.bundle.info'));
}
public function getFormId() {
return 'like_and_dislike_settings';
}
protected function getEditableConfigNames() {
return [
'like_and_dislike.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('like_and_dislike.settings');
$form['enabled_types'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Entity types with Like & Dislike widgets enabled'),
'#description' => $this
->t('If you disable any type here, already existing data will remain untouched.'),
'#tree' => TRUE,
];
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
if ($entity_type
->getGroup() != 'content' || !$entity_type
->hasHandlerClass('view_builder')) {
continue;
}
$form['enabled_types'][$entity_type_id] = [
'#type' => 'container',
'#tree' => TRUE,
];
$form['enabled_types'][$entity_type_id]['enabled'] = [
'#type' => 'checkbox',
'#title' => $entity_type
->getLabel(),
'#default_value' => is_array($config
->get('enabled_types.' . $entity_type_id)),
];
if ($entity_type
->hasKey('bundle')) {
$bundles = $this->bundleInfoService
->getBundleInfo($entity_type_id);
$bundles = array_map(function ($bundle_info) {
return $bundle_info['label'];
}, $bundles);
$form['enabled_types'][$entity_type_id]['bundle_info'] = [
'#title' => $this
->getBundleTypeLabel($entity_type),
'#type' => 'details',
'#open' => TRUE,
'#states' => [
'invisible' => [
'input[name="enabled_types[' . $entity_type_id . '][enabled]"]' => [
'checked' => FALSE,
],
],
],
];
$form['enabled_types'][$entity_type_id]['bundle_info']['bundles'] = [
'#type' => 'checkboxes',
'#options' => $bundles,
'#default_value' => $config
->get('enabled_types.' . $entity_type_id) ?: [],
];
}
}
$form['allow_cancel_vote'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Allow vote cancellation'),
'#description' => $this
->t('Whether the users should be allowed to cancel their own votes by voting again for the same choice of the same poll.'),
'#default_value' => $config
->get('allow_cancel_vote'),
];
$form['hide_vote_widget'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Hide vote widget instead of disabling it'),
'#description' => $this
->t('If checked then instead of disabled widget user will not see widget at all if vote permission is missing.'),
'#default_value' => $config
->get('hide_vote_widget'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('like_and_dislike.settings');
$enabled_types = [];
foreach ($form_state
->getValue('enabled_types') as $entity_type_id => $entity_type_settings) {
if (!$entity_type_settings['enabled']) {
continue;
}
if (isset($entity_type_settings['bundle_info']) && array_keys(array_filter($entity_type_settings['bundle_info']['bundles']))) {
$enabled_types[$entity_type_id] = array_keys(array_filter($entity_type_settings['bundle_info']['bundles']));
}
else {
$enabled_types[$entity_type_id] = [];
}
}
$config
->set('enabled_types', $enabled_types);
$config
->set('allow_cancel_vote', $form_state
->getValue('allow_cancel_vote'));
$config
->set('hide_vote_widget', $form_state
->getValue('hide_vote_widget'));
$config
->save();
parent::submitForm($form, $form_state);
$this->entityFieldManager
->clearCachedFieldDefinitions();
}
protected function getBundleTypeLabel(EntityTypeInterface $entity_type) {
if ($bundle_type_label = $entity_type
->getBundleLabel()) {
return $bundle_type_label;
}
if ($bundle_entity_type = $entity_type
->getBundleEntityType()) {
return $this->entityTypeManager
->getDefinition($bundle_entity_type)
->getLabel();
}
return $this
->t('@label type', [
'@label' => $entity_type
->getLabel(),
]);
}
}