SocialProfileTagService.php in Open Social 10.2.x
File
modules/social_features/social_profile/src/SocialProfileTagService.php
View source
<?php
namespace Drupal\social_profile;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Url;
use Drupal\taxonomy\TermInterface;
class SocialProfileTagService implements SocialProfileTagServiceInterface {
protected $taxonomyStorage;
protected $profileConfig;
protected $languageManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager) {
$this->taxonomyStorage = $entity_type_manager
->getStorage('taxonomy_term');
$this->profileConfig = $config_factory
->get('social_profile.settings');
$this->languageManager = $language_manager;
}
public function isActive() {
return $this->profileConfig
->get('enable_profile_tagging');
}
public function hasContent() {
if (count($this
->getCategories()) == 0) {
return FALSE;
}
return TRUE;
}
public function allowSplit() {
return $this
->isActive() && $this->profileConfig
->get('allow_category_split');
}
public function getCategories() {
$options = [];
$current_lang = $this->languageManager
->getCurrentLanguage()
->getId();
if (!empty($current_lang_terms = $this->taxonomyStorage
->loadTree('profile_tag', 0, 1, FALSE, $current_lang))) {
$options = $this
->prepareTermOptions($current_lang_terms);
}
elseif (!empty($default_lang_terms = $this->taxonomyStorage
->loadTree('profile_tag', 0, 1, FALSE))) {
$options = $this
->prepareTermOptions($default_lang_terms);
}
return $options;
}
public function getChildrens($category) {
$options = [];
$current_lang = $this->languageManager
->getCurrentLanguage()
->getId();
if (!empty($current_lang_terms = $this->taxonomyStorage
->loadTree('profile_tag', $category, 1, FALSE, $current_lang))) {
$options = $this
->prepareTermOptions($current_lang_terms);
}
elseif (!empty($default_lang_terms = $this->taxonomyStorage
->loadTree('profile_tag', $category, 1, FALSE))) {
$options = $this
->prepareTermOptions($default_lang_terms);
}
return $options;
}
public function useCategoryParent() {
return $this->profileConfig
->get('use_category_parent');
}
public function tagLabelToMachineName($label) {
return strtolower(str_replace(' ', '', $label));
}
public function buildHierarchy(array $term_ids) {
$tree = [];
$terms = $this->taxonomyStorage
->loadMultiple(array_column($term_ids, 'target_id'));
if (empty($terms)) {
return [];
}
foreach ($terms as $term) {
if (!$term instanceof TermInterface) {
continue;
}
$parents = $this->taxonomyStorage
->loadParents($term
->id());
if ($parents) {
$parent = reset($parents);
}
else {
$parent = $term;
}
$parent_label = $parent
->getName();
$route = 'view.search_users.page_no_value';
$route_parameters = [
'created_op' => '<',
'profile_tag[]' => $term
->id(),
];
$url = Url::fromRoute($route, $route_parameters)
->toString();
$tree[$parent
->id()]['title'] = $parent_label;
$tree[$parent
->id()]['tags'][$term
->id()] = [
'url' => $url,
'name' => $term
->getName(),
];
}
return $tree;
}
public function getTermOptionNames(array $term_ids) {
$options = [];
if (empty($term_ids)) {
return $options;
}
$terms = $this->taxonomyStorage
->loadMultiple($term_ids);
foreach ($terms as $term) {
$options[$term
->id()] = $term
->label();
}
return $options;
}
private function prepareTermOptions(array $terms) {
$options = [];
foreach ($terms as $category) {
$options[$category->tid] = $category->name;
}
return $options;
}
}