You are here

class SocialCommentLazyRenderer in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_comment/src/SocialCommentLazyRenderer.php \Drupal\social_comment\SocialCommentLazyRenderer
  2. 10.3.x modules/social_features/social_comment/src/SocialCommentLazyRenderer.php \Drupal\social_comment\SocialCommentLazyRenderer
  3. 10.1.x modules/social_features/social_comment/src/SocialCommentLazyRenderer.php \Drupal\social_comment\SocialCommentLazyRenderer
  4. 10.2.x modules/social_features/social_comment/src/SocialCommentLazyRenderer.php \Drupal\social_comment\SocialCommentLazyRenderer

Render comments for the lazy builder.

@package Drupal\social_comment

Hierarchy

Expanded class hierarchy of SocialCommentLazyRenderer

1 string reference to 'SocialCommentLazyRenderer'
social_comment.services.yml in modules/social_features/social_comment/social_comment.services.yml
modules/social_features/social_comment/social_comment.services.yml
1 service uses SocialCommentLazyRenderer
social_comment.lazy_renderer in modules/social_features/social_comment/social_comment.services.yml
Drupal\social_comment\SocialCommentLazyRenderer

File

modules/social_features/social_comment/src/SocialCommentLazyRenderer.php, line 16

Namespace

Drupal\social_comment
View source
class SocialCommentLazyRenderer implements TrustedCallbackInterface {

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

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

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  private $routeMatch;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * SocialCommentLazyRenderer constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match, ModuleHandlerInterface $module_handler) {
    $this->entityTypeManager = $entity_type_manager;
    $this->routeMatch = $route_match;
    $this->moduleHandler = $module_handler;
  }

  /**
   * Render comments for lazy builder.
   *
   * @param string $entity_type
   *   The entity type.
   * @param string|int $entity_id
   *   The entity id.
   * @param string $view_mode
   *   The view mode from field settings.
   * @param string $field_name
   *   The field name.
   * @param string|int|null $num_comments
   *   The number of comments.
   * @param int $pager_id
   *   Pager id to use in case of multiple pagers on the one page.
   * @param string $build_view_mode
   *   The build view mode.
   *
   * @return mixed
   *   The render array.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function renderComments($entity_type, $entity_id, $view_mode, $field_name, $num_comments, $pager_id, $build_view_mode = 'default') {

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $this->entityTypeManager
      ->getStorage($entity_type)
      ->load($entity_id);

    /** @var \Drupal\comment\CommentInterface[] $comments */
    $comments = $this->entityTypeManager
      ->getStorage('comment')
      ->loadThread($entity, $field_name, $view_mode, $num_comments, $pager_id);
    if (!$comments) {
      return [];
    }
    $build_comments = $this->entityTypeManager
      ->getViewBuilder('comment')
      ->viewMultiple($comments, $build_view_mode);
    if ($build_comments) {
      $build_comments['pager']['#type'] = 'pager';
      $build_comments['pager']['#route_name'] = $this->routeMatch
        ->getRouteObject();
      $build_comments['pager']['#route_parameters'] = $this->routeMatch
        ->getRawParameters()
        ->all();
      if ($pager_id) {
        $build_comments['pager']['#element'] = $pager_id;
      }
    }

    // Since we are rendering it as lazy builder, make sure we attach classes
    // required by ajax_comments. In order to render reply forms etc.
    if (!empty($build_comments) && $this->moduleHandler
      ->moduleExists('ajax_comments')) {
      Utility::addCommentClasses($build_comments);
    }
    return $build_comments;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SocialCommentLazyRenderer::$entityTypeManager private property The entity type manager.
SocialCommentLazyRenderer::$moduleHandler protected property The module handler.
SocialCommentLazyRenderer::$routeMatch private property The current route match.
SocialCommentLazyRenderer::renderComments public function Render comments for lazy builder.
SocialCommentLazyRenderer::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
SocialCommentLazyRenderer::__construct public function SocialCommentLazyRenderer constructor.
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.