class SettingsForm in Like & Dislike 8
Class SettingsForm.
@package Drupal\like_and_dislike\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\like_and_dislike\Form\SettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SettingsForm
1 string reference to 'SettingsForm'
File
- src/
Form/ SettingsForm.php, line 21
Namespace
Drupal\like_and_dislike\FormView source
class SettingsForm extends ConfigFormBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The bundle info service.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $bundleInfoService;
/**
* Constructs a \Drupal\like_and_dislike\Form\SettingsForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager .
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info_service
* The bundle info service.
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'like_and_dislike_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'like_and_dislike.settings',
];
}
/**
* Defines the settings form for each entity type bundles.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* Form definition array.
*/
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) {
// Only display content entities with a view builder.
if ($entity_type
->getGroup() != 'content' || !$entity_type
->hasHandlerClass('view_builder')) {
continue;
}
$form['enabled_types'][$entity_type_id] = [
'#type' => 'container',
'#tree' => TRUE,
];
// Checkbox to enable and disable the entity type.
$form['enabled_types'][$entity_type_id]['enabled'] = [
'#type' => 'checkbox',
'#title' => $entity_type
->getLabel(),
'#default_value' => is_array($config
->get('enabled_types.' . $entity_type_id)),
];
// Display entity type bundles.
if ($entity_type
->hasKey('bundle')) {
$bundles = $this->bundleInfoService
->getBundleInfo($entity_type_id);
// Get bundle label.
$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) ?: [],
];
}
}
// Checkbox to allow vote cancellation.
$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'),
];
// Checkbox to allow hiding of vote widgets on missing permission.
$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);
}
/**
* {@inheritdoc}
*/
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) {
// Skip disabled content entity types.
if (!$entity_type_settings['enabled']) {
continue;
}
// Put enabled content entity type into configuration, only save entity
// types with bundles if at least one bundle is checked.
if (isset($entity_type_settings['bundle_info']) && array_keys(array_filter($entity_type_settings['bundle_info']['bundles']))) {
// Filter-out non-selected 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);
// Settings changes might not immediately be updated for the view display.
// Thus clear the entity extra field caches.
$this->entityFieldManager
->clearCachedFieldDefinitions();
}
/**
* Returns the bundle type label for a given entity type.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return string
* The bundle type label.
*/
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(),
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SettingsForm:: |
protected | property | The bundle info service. | |
SettingsForm:: |
protected | property | The entity field manager. | |
SettingsForm:: |
protected | property | The entity type manager. | |
SettingsForm:: |
public | function |
Defines the settings form for each entity type bundles. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
SettingsForm:: |
protected | function | Returns the bundle type label for a given entity type. | |
SettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public | function |
Constructs a \Drupal\like_and_dislike\Form\SettingsForm object. Overrides ConfigFormBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |