SocialSearchApiSplitProfileTerms.php in Open Social 10.3.x
File
modules/social_features/social_profile/src/Plugin/views/filter/SocialSearchApiSplitProfileTerms.php
View source
<?php
namespace Drupal\social_profile\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\search_api\Plugin\views\filter\SearchApiTerm;
use Drupal\social_profile\SocialProfileTagServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SocialSearchApiSplitProfileTerms extends SearchApiTerm {
protected $profileTagService;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$filter = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$filter
->setSocialProfileTagService($container
->get('social_profile.tag_service'));
return $filter;
}
protected function setSocialProfileTagService(SocialProfileTagServiceInterface $social_profile_tag_service) {
$this->profileTagService = $social_profile_tag_service;
}
protected function valueForm(&$form, FormStateInterface $form_state) {
parent::valueForm($form, $form_state);
if ($this->field !== 'field_profile_profile_tag') {
return;
}
$element =& $form['value'];
if (!$this->profileTagService
->isActive() || !$this->profileTagService
->hasContent() || empty($element['#options'])) {
$element = [];
}
if (!$this->profileTagService
->allowSplit()) {
$term_ids = [];
$element['#type'] = 'select2';
$options =& $element['#options'];
foreach ($options as $option) {
$term_ids[] = array_keys($option->option)[0];
}
$options = $this->profileTagService
->getTermOptionNames($term_ids);
return;
}
$identifier = $this->options['expose']['identifier'];
$default_value = $form_state
->getUserInput()[$identifier];
$element = [
'#type' => 'details',
'#open' => TRUE,
'#access' => FALSE,
];
$categories = $this->profileTagService
->getCategories();
foreach ($categories as $tid => $category) {
$field_name = 'profile_tagging_' . $this->profileTagService
->tagLabelToMachineName($category);
$options = $this->profileTagService
->getChildrens($tid);
if ($this->profileTagService
->useCategoryParent()) {
$options = [
$tid => $category,
] + $options;
}
if (count($options) > 0) {
$element[$field_name] = [
'#type' => 'select2',
'#title' => $category,
'#multiple' => TRUE,
'#value' => $default_value,
'#options' => $options,
'#name' => 'profile_tag',
];
$element['#access'] = TRUE;
}
}
}
public function validateExposed(&$form, FormStateInterface $form_state) {
if ($this->field !== 'field_profile_profile_tag' || !$this->profileTagService
->allowSplit()) {
parent::validateExposed($form, $form_state);
return;
}
$profile_tag_values = [];
$identifier = $this->options['expose']['identifier'];
$categories = $this->profileTagService
->getCategories();
$form_values = $form_state
->getValues();
foreach ($categories as $tid => $category) {
$field_name = 'profile_tagging_' . $this->profileTagService
->tagLabelToMachineName($category);
if (isset($form_values[$field_name])) {
$profile_tag_values += $form_values[$field_name];
unset($form_values[$field_name]);
}
}
$form_values[$identifier] = $profile_tag_values;
$form_state
->setValues($form_values);
parent::validateExposed($form, $form_state);
}
}