You are here

class SocialTaggingService in Open Social 10.3.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService
  2. 8 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService
  3. 8.2 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService
  4. 8.3 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService
  5. 8.4 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService
  6. 8.5 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService
  7. 8.6 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService
  8. 8.7 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService
  9. 8.8 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService
  10. 10.0.x modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService
  11. 10.1.x modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService
  12. 10.2.x modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService

Provides a custom tagging service.

Hierarchy

Expanded class hierarchy of SocialTaggingService

3 files declare their use of SocialTaggingService
SocialFollowTagLazyBuilder.php in modules/social_features/social_follow_taxonomy/modules/social_follow_tag/src/SocialFollowTagLazyBuilder.php
SocialGroupTagsBlock.php in modules/social_features/social_tagging/src/Plugin/Block/SocialGroupTagsBlock.php
SocialTagsBlock.php in modules/social_features/social_tagging/src/Plugin/Block/SocialTagsBlock.php
1 string reference to 'SocialTaggingService'
social_tagging.services.yml in modules/social_features/social_tagging/social_tagging.services.yml
modules/social_features/social_tagging/social_tagging.services.yml
1 service uses SocialTaggingService
social_tagging.tag_service in modules/social_features/social_tagging/social_tagging.services.yml
Drupal\social_tagging\SocialTaggingService

File

modules/social_features/social_tagging/src/SocialTaggingService.php, line 14

Namespace

Drupal\social_tagging
View source
class SocialTaggingService {

  /**
   * The taxonomy storage.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * SocialTaggingService constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Injection of the entityTypeManager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   Injection of the configFactory.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   Injection of the languageManager.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, ConfigFactoryInterface $configFactory, LanguageManagerInterface $language_manager) {
    $this->entityTypeManager = $entityTypeManager;
    $this->configFactory = $configFactory;
    $this->languageManager = $language_manager;
  }

  /**
   * Returns whether the feature is turned on or not.
   *
   * @return bool
   *   Whether tagging is turned on or not.
   */
  public function active() {
    return (bool) $this->configFactory
      ->get('social_tagging.settings')
      ->get('enable_content_tagging');
  }

  /**
   * Returns whether the feature is turned on for groups or not.
   *
   * @return bool
   *   Whether tagging is turned on or not for groups.
   */
  public function groupActive() {
    return (bool) $this->configFactory
      ->get('social_tagging.settings')
      ->get('tag_type_group');
  }

  /**
   * Returns whether the feature is turned on for profiles or not.
   *
   * @return bool
   *   Whether tagging is turned on or not for profiles.
   */
  public function profileActive() {
    return (bool) $this->configFactory
      ->get('social_tagging.settings')
      ->get('tag_type_profile');
  }

  /**
   * Returns if there are any taxonomy items available.
   *
   * @return bool
   *   If there are tags available.
   */
  public function hasContent() {
    if (count($this
      ->getCategories()) == 0) {
      return FALSE;
    }
    if (count($this
      ->getAllChildren()) == 0) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * Returns whether splitting of fields is allowed.
   *
   * @return bool
   *   Whether category split on field level is turned on or not.
   */
  public function allowSplit() {
    return (bool) ($this
      ->active() && $this->configFactory
      ->get('social_tagging.settings')
      ->get('allow_category_split'));
  }

  /**
   * Returns whether using a parent of categories is allowed.
   *
   * @return bool
   *   Whether using categories parent is turned on or not..
   */
  public function useCategoryParent() {
    return (bool) ($this
      ->active() && $this->configFactory
      ->get('social_tagging.settings')
      ->get('use_category_parent'));
  }

  /**
   * Returns all the top level term items, that are considered categories.
   *
   * @return array
   *   An array of top level category items.
   */
  public function getCategories() {

    // Define as array.
    $options = [];

    // Get the site's current language.
    $current_lang = $this->languageManager
      ->getCurrentLanguage()
      ->getId();

    // Fetch main categories.
    // If the website is multilingual, we want to first check for the terms
    // in current language. At the moment, users do not add proper language to
    // vocabulary terms which may result in return of empty array on loadTree()
    // function. So, we want to check for the terms also in default language if
    // we don't find terms in current language.
    if (!empty($current_lang_terms = $this->entityTypeManager
      ->getStorage('taxonomy_term')
      ->loadTree('social_tagging', 0, 1, FALSE, $current_lang))) {
      $options = $this
        ->prepareTermOptions($current_lang_terms);
    }
    elseif (!empty($default_lang_terms = $this->entityTypeManager
      ->getStorage('taxonomy_term')
      ->loadTree('social_tagging', 0, 1, FALSE))) {
      $options = $this
        ->prepareTermOptions($default_lang_terms);
    }

    // Return array.
    return $options;
  }

  /**
   * Returns the children of top level term items.
   *
   * @param int $category
   *   The category you want to fetch the child items from.
   *
   * @return array
   *   An array of child items.
   */
  public function getChildren($category) {

    // Define as array.
    $options = [];

    // Get the site's current language.
    $current_lang = $this->languageManager
      ->getCurrentLanguage()
      ->getId();
    if (!empty($current_lang_terms = $this->entityTypeManager
      ->getStorage('taxonomy_term')
      ->loadTree('social_tagging', $category, 1, FALSE, $current_lang))) {
      $options = $this
        ->prepareTermOptions($current_lang_terms);
    }
    elseif (!empty($default_lang_terms = $this->entityTypeManager
      ->getStorage('taxonomy_term')
      ->loadTree('social_tagging', $category, 1, FALSE))) {
      $options = $this
        ->prepareTermOptions($default_lang_terms);
    }

    // Return array.
    return $options;
  }

  /**
   * Returns all the children of top level term items.
   *
   * @return array
   *   An array of child items.
   */
  public function getAllChildren() {

    // Define as array.
    $options = [];

    // Fetch main categories.
    foreach (array_keys($this
      ->getCategories()) as $category) {
      $options = array_merge($options, $this
        ->getChildren($category));
    }

    // Return array.
    return $options;
  }

  /**
   * Returns a multilevel tree.
   *
   * @param array $term_ids
   *   An array of items that are selected.
   * @param string $entity_type
   *   The entity type these tags are for.
   *
   * @return array
   *   An hierarchy array of items with their parent.
   */
  public function buildHierarchy(array $term_ids, $entity_type) {
    $tree = [];

    // Load all the terms together.
    if (!empty($terms = $this->entityTypeManager
      ->getStorage('taxonomy_term')
      ->loadMultiple(array_column($term_ids, 'target_id')))) {

      // Get current language.
      // This is used to get the translated term, if available.
      $langcode = $this->languageManager
        ->getCurrentLanguage()
        ->getId();

      // Get splitting of fields option.
      $allowSplit = $this
        ->allowSplit();

      // Set the route.
      $route = $entity_type == 'group' ? 'view.search_groups.page_no_value' : 'view.search_content.page_no_value';
      if ($entity_type == 'profile') {
        $route = 'view.search_users.page_no_value';
      }

      // Build the hierarchy.
      foreach ($terms as $current_term) {

        // Must be a valid Term.
        if (!$current_term instanceof TermInterface) {
          continue;
        }

        // Get current terms parents.
        if ($parents = $this->entityTypeManager
          ->getStorage('taxonomy_term')
          ->loadParents($current_term
          ->id())) {
          $parent = reset($parents);
          $category_label = $parent
            ->hasTranslation($langcode) ? $parent
            ->getTranslation($langcode)
            ->getName() : $parent
            ->getName();
        }
        else {
          $category_label = $current_term
            ->hasTranslation($langcode) ? $current_term
            ->getTranslation($langcode)
            ->getName() : $current_term
            ->getName();
          $parent = $current_term;
        }

        // Prepare the parameter;.
        $parameter = $allowSplit ? social_tagging_to_machine_name($category_label) : 'tag';
        $route_parameters = [
          $parameter . '[]' => $current_term
            ->id(),
        ];
        if ($entity_type == 'profile') {
          $route_parameters['created_op'] = '<';
        }

        // Prepare the URL for the search by term.
        $url = Url::fromRoute($route, $route_parameters)
          ->toString();

        // Finally, prepare the hierarchy.
        $tree[$parent
          ->id()]['title'] = $category_label;
        $tree[$parent
          ->id()]['tags'][$current_term
          ->id()] = [
          'url' => $url,
          'name' => $current_term
            ->hasTranslation($langcode) ? $current_term
            ->getTranslation($langcode)
            ->getName() : $current_term
            ->getName(),
        ];
      }
    }

    // Return the tree.
    return $tree;
  }

  /**
   * Helper function to prepare term options.
   *
   * @param array $terms
   *   Array of terms.
   *
   * @return array
   *   Returns a list of terms options.
   */
  private function prepareTermOptions(array $terms) {
    $options = [];
    foreach ($terms as $category) {
      $options[$category->tid] = $category->name;
    }
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SocialTaggingService::$configFactory protected property The configuration factory.
SocialTaggingService::$entityTypeManager protected property The taxonomy storage.
SocialTaggingService::$languageManager protected property The language manager.
SocialTaggingService::active public function Returns whether the feature is turned on or not.
SocialTaggingService::allowSplit public function Returns whether splitting of fields is allowed.
SocialTaggingService::buildHierarchy public function Returns a multilevel tree.
SocialTaggingService::getAllChildren public function Returns all the children of top level term items.
SocialTaggingService::getCategories public function Returns all the top level term items, that are considered categories.
SocialTaggingService::getChildren public function Returns the children of top level term items.
SocialTaggingService::groupActive public function Returns whether the feature is turned on for groups or not.
SocialTaggingService::hasContent public function Returns if there are any taxonomy items available.
SocialTaggingService::prepareTermOptions private function Helper function to prepare term options.
SocialTaggingService::profileActive public function Returns whether the feature is turned on for profiles or not.
SocialTaggingService::useCategoryParent public function Returns whether using a parent of categories is allowed.
SocialTaggingService::__construct public function SocialTaggingService constructor.