class FilterSettingsForm in Open Social 10.2.x
Same name and namespace in other branches
- 10.3.x modules/social_features/social_activity/modules/social_activity_filter/src/Form/FilterSettingsForm.php \Drupal\social_activity_filter\Form\FilterSettingsForm
- 10.0.x modules/social_features/social_activity/modules/social_activity_filter/src/Form/FilterSettingsForm.php \Drupal\social_activity_filter\Form\FilterSettingsForm
- 10.1.x modules/social_features/social_activity/modules/social_activity_filter/src/Form/FilterSettingsForm.php \Drupal\social_activity_filter\Form\FilterSettingsForm
Provides a settings form of activity filter.
@package Drupal\social_activity_filter\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\social_activity_filter\Form\FilterSettingsForm implements ContainerInjectionInterface
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of FilterSettingsForm
1 string reference to 'FilterSettingsForm'
- social_activity_filter.routing.yml in modules/
social_features/ social_activity/ modules/ social_activity_filter/ social_activity_filter.routing.yml - modules/social_features/social_activity/modules/social_activity_filter/social_activity_filter.routing.yml
File
- modules/
social_features/ social_activity/ modules/ social_activity_filter/ src/ Form/ FilterSettingsForm.php, line 19
Namespace
Drupal\social_activity_filter\FormView source
class FilterSettingsForm extends ConfigFormBase implements ContainerInjectionInterface {
/**
* The Module Handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $moduleHandler, EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($config_factory);
$this->moduleHandler = $moduleHandler;
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('module_handler'), $container
->get('entity_field.manager'), $container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'social_activity_filter_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'social_activity_filter.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Get the configuration file.
$config = $this
->config('social_activity_filter.settings');
$form['social_activity_filter'] = [
'#type' => 'fieldset',
];
$displays = [];
foreach (social_activity_default_views_list() as $views_id) {
$displays = array_merge($displays, $this
->getDisplayBlocks($views_id));
}
$form['social_activity_filter']['blocks'] = [
'#type' => 'checkboxes',
'#markup' => '<div class="fieldset__description">' . $this
->t('Please select the blocks in which the taxonomy filters can be used.') . '</div>',
'#title' => $this
->t('Select blocks'),
'#options' => $displays,
'#default_value' => $config
->get('blocks'),
'#required' => TRUE,
];
$storage = $this->entityTypeManager
->getStorage('taxonomy_vocabulary');
$taxonomy_vocabularies = $storage
->loadMultiple();
$vocabulariesList = [];
/** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
foreach ($taxonomy_vocabularies as $vid => $vocabulary) {
$vocabulariesList[$vid] = $vocabulary
->get('name');
}
$form['social_activity_filter']['vocabulary'] = [
'#type' => 'checkboxes',
'#markup' => '<div class="fieldset__description">' . $this
->t('Please select the taxonomy vocabularies that can be used in the taxonomy filters.') . '</div>',
'#title' => $this
->t('Select taxonomy vocabularies'),
'#options' => $vocabulariesList,
'#default_value' => $config
->get('vocabulary'),
'#required' => TRUE,
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Get the configuration file.
$config = $this
->config('social_activity_filter.settings');
$vocabularies = array_filter($form_state
->getValue('vocabulary'));
$fields = $this
->getReferencedTaxonomyFields($vocabularies);
$blocks = $form_state
->getValue('blocks');
$config
->set('vocabulary', $vocabularies);
$config
->set('taxonomy_fields', $fields);
$config
->set('blocks', $blocks);
$config
->save();
foreach ($blocks as $id => $block) {
$tag_filter = $block ? TRUE : FALSE;
[
$viws_id,
$display_id,
] = explode('__', $id);
$this
->updateDisplayBlock($viws_id, $display_id, $tag_filter);
}
parent::submitForm($form, $form_state);
}
/**
* Helper function to find all referenced taxonomy fields.
*
* @param array $vocabulary_list
* Array of vocabulary id's.
*
* @return array
* Mapped array: vid => taxonomy_field.
*/
public function getReferencedTaxonomyFields(array $vocabulary_list) {
$content_types = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
$field_names = [];
foreach ($vocabulary_list as $vocabulary) {
foreach ($content_types as $content_type => $type) {
$field_definitions = $this->entityFieldManager
->getFieldDefinitions('node', $content_type);
foreach ($field_definitions as $field_definition) {
if ($field_definition
->getType() == 'entity_reference' && $field_definition
->getSetting('target_type') == 'taxonomy_term') {
$handler_settings = $field_definition
->getSetting('handler_settings');
if (isset($handler_settings['target_bundles'][$vocabulary])) {
if (isset($field_names[$vocabulary])) {
continue;
}
$field_names[$vocabulary] = $field_definition
->getName();
}
}
}
}
}
return $field_names;
}
/**
* Gets all displays blocks of views.
*
* @param string $views_id
* Views ID.
*
* @return array
* Mapped array of views displays.
*/
public function getDisplayBlocks($views_id) {
$view = $this->entityTypeManager
->getStorage('view')
->load($views_id);
$blocks = [];
foreach ($view
->get('display') as $display) {
if ($display['display_plugin'] === 'block') {
$blocks["{$views_id}__{$display['id']}"] = $display['display_title'];
}
}
return $blocks;
}
/**
* Update settings of displays views blocks.
*
* @param string $views_id
* Views ID.
* @param string $display_id
* Display ID.
* @param bool $enabled
* Flag to update/cleanup values.
*/
public function updateDisplayBlock($views_id, $display_id, $enabled = FALSE) {
$config = $this->configFactory
->getEditable("views.view.{$views_id}");
$override_tags_filter = "display.{$display_id}.display_options.override_tags_filter";
$activity_filter_tags = "display.{$display_id}.display_options.filters.activity_filter_tags";
if ($enabled) {
$config
->set($override_tags_filter, 1);
$config
->set($activity_filter_tags, social_activity_get_tag_filter_data());
}
else {
$config
->clear($override_tags_filter);
$config
->clear($activity_filter_tags);
}
$config
->save();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FilterSettingsForm:: |
protected | property | The entity field manager. | |
FilterSettingsForm:: |
protected | property | The entity manager. | |
FilterSettingsForm:: |
protected | property | The Module Handler. | |
FilterSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
FilterSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
FilterSettingsForm:: |
public | function | Gets all displays blocks of views. | |
FilterSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
FilterSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
FilterSettingsForm:: |
public | function | Helper function to find all referenced taxonomy fields. | |
FilterSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
FilterSettingsForm:: |
public | function | Update settings of displays views blocks. | |
FilterSettingsForm:: |
public | function |
Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase:: |
|
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
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. | |
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:: |
72 |
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. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
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. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
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. |