You are here

class SocialFollowTagLazyBuilder in Open Social 10.0.x

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_follow_taxonomy/modules/social_follow_tag/src/SocialFollowTagLazyBuilder.php \Drupal\social_follow_tag\SocialFollowTagLazyBuilder
  2. 10.1.x modules/social_features/social_follow_taxonomy/modules/social_follow_tag/src/SocialFollowTagLazyBuilder.php \Drupal\social_follow_tag\SocialFollowTagLazyBuilder
  3. 10.2.x modules/social_features/social_follow_taxonomy/modules/social_follow_tag/src/SocialFollowTagLazyBuilder.php \Drupal\social_follow_tag\SocialFollowTagLazyBuilder

Provide service for lazy rendering.

@package Drupal\social_follow_tag

Hierarchy

Expanded class hierarchy of SocialFollowTagLazyBuilder

1 string reference to 'SocialFollowTagLazyBuilder'
social_follow_tag.services.yml in modules/social_features/social_follow_taxonomy/modules/social_follow_tag/social_follow_tag.services.yml
modules/social_features/social_follow_taxonomy/modules/social_follow_tag/social_follow_tag.services.yml
1 service uses SocialFollowTagLazyBuilder
social_follow_tag.lazy_builder in modules/social_features/social_follow_taxonomy/modules/social_follow_tag/social_follow_tag.services.yml
Drupal\social_follow_tag\SocialFollowTagLazyBuilder

File

modules/social_features/social_follow_taxonomy/modules/social_follow_tag/src/SocialFollowTagLazyBuilder.php, line 20

Namespace

Drupal\social_follow_tag
View source
class SocialFollowTagLazyBuilder implements TrustedCallbackInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The form builder.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * The route match.
   *
   * @var \Drupal\social_tagging\SocialTaggingService
   */
  protected $tagService;

  /**
   * Flag service.
   *
   * @var \Drupal\flag\FlagServiceInterface
   */
  protected $flagService;

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The builder for flag links.
   *
   * @var \Drupal\flag\FlagLinkBuilderInterface
   */
  protected $flagLinkBuilder;

  /**
   * The Current User object.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * SocialFollowTagLazyBuilder constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Form\FormBuilderInterface $formBuilder
   *   The form builder.
   * @param \Drupal\social_tagging\SocialTaggingService $tagging_service
   *   The tag service.
   * @param \Drupal\flag\FlagServiceInterface $flag_service
   *   Flag service.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   * @param \Drupal\flag\FlagLinkBuilderInterface $flag_link_builder
   *   The builder for flag links.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $formBuilder, SocialTaggingService $tagging_service, FlagServiceInterface $flag_service, RendererInterface $renderer, FlagLinkBuilderInterface $flag_link_builder, AccountInterface $current_user) {
    $this->entityTypeManager = $entity_type_manager;
    $this->formBuilder = $formBuilder;
    $this->tagService = $tagging_service;
    $this->flagService = $flag_service;
    $this->renderer = $renderer;
    $this->flagLinkBuilder = $flag_link_builder;
    $this->currentUser = $current_user;
  }

  /**
   * Returns tags for lazy builder.
   */
  public function lazyBuild() {
    $identifiers = [];

    // Get the tag category identifier that is used as a parameter in the URL.
    // It takes on the value of the parent term if the allow_category_split
    // settings is enabled or equal to the default name of the filter (tag).
    if ($this->tagService
      ->allowSplit()) {
      foreach ($this->tagService
        ->getCategories() as $tid => $value) {
        if (!empty($this->tagService
          ->getChildren($tid))) {
          $identifiers[] = social_tagging_to_machine_name($value);
        }
      }
    }
    else {
      $identifiers = [
        'tag',
      ];
    }

    // Get term id from url parameters.
    $term_ids = [];
    foreach ($identifiers as $identifier) {
      if (isset($_GET[$identifier])) {
        $term_ids = array_merge($term_ids, $_GET[$identifier]);
      }
    }
    $tags = [];

    /** @var \Drupal\taxonomy\TermStorageInterface $term_storage */
    $term_storage = $this->entityTypeManager
      ->getStorage('taxonomy_term');
    foreach ($term_ids as $term_id) {

      /** @var \Drupal\taxonomy\Entity\Term $term */
      $term = $term_storage
        ->load($term_id);

      // Show only tags followed by user.
      if ($term && social_follow_taxonomy_term_followed($term)) {
        $tags[$term_id] = [
          'name' => $term
            ->getName(),
          'flag' => social_follow_taxonomy_flag_link($term),
          'related_entity_count' => social_follow_taxonomy_related_entity_count($term, 'social_tagging'),
          'followers_count' => social_follow_taxonomy_term_followers_count($term),
        ];
      }
    }
    if (!empty($tags)) {
      $build = [
        '#theme' => 'search_follow_tag',
        '#tagstitle' => $this
          ->t('Tags'),
        '#tags' => $tags,
      ];

      // Generate cache tags.
      foreach ($tags as $tag_id => $tag) {
        $build['#cache']['tags'][] = "follow_tag_node:{$tag_id}";
      }
      return $build;
    }
    return [];
  }

  /**
   * Returns render array for tag follow popup.
   *
   * @param string $url
   *   ULR of related content.
   * @param string|int $term_id
   *   Taxonomy term ID.
   * @param string $field
   *   Entity field name related to taxonomy.
   * @param string $entity_type
   *   Entity type for related content counter.
   *
   * @return array
   *   Render array.
   */
  public function popupLazyBuild($url, $term_id, $field, $entity_type) {

    /** @var \Drupal\taxonomy\TermInterface $term */
    $term = $this->entityTypeManager
      ->getStorage('taxonomy_term')
      ->load($term_id);
    return [
      '#theme' => 'social_tagging_popup',
      '#url' => $url,
      '#name' => $term
        ->label(),
      '#flag' => social_follow_taxonomy_flag_link($term),
      '#followers_count' => social_follow_taxonomy_term_followers_count($term),
      '#related_entity_count' => social_follow_taxonomy_related_entity_count($term, $field, $entity_type),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'lazyBuild',
      'popupLazyBuild',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SocialFollowTagLazyBuilder::$currentUser protected property The Current User object.
SocialFollowTagLazyBuilder::$entityTypeManager protected property The entity type manager.
SocialFollowTagLazyBuilder::$flagLinkBuilder protected property The builder for flag links.
SocialFollowTagLazyBuilder::$flagService protected property Flag service.
SocialFollowTagLazyBuilder::$formBuilder protected property The form builder.
SocialFollowTagLazyBuilder::$renderer protected property The renderer service.
SocialFollowTagLazyBuilder::$tagService protected property The route match.
SocialFollowTagLazyBuilder::lazyBuild public function Returns tags for lazy builder.
SocialFollowTagLazyBuilder::popupLazyBuild public function Returns render array for tag follow popup.
SocialFollowTagLazyBuilder::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
SocialFollowTagLazyBuilder::__construct public function SocialFollowTagLazyBuilder constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.