CommentLazyBuilders.php 7.25 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\comment;

5
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
6
use Drupal\Core\Entity\EntityFormBuilderInterface;
7
use Drupal\Core\Entity\EntityInterface;
8
use Drupal\Core\Entity\EntityManagerInterface;
9
use Drupal\Core\Extension\ModuleHandlerInterface;
10
use Drupal\Core\Render\RendererInterface;
11 12
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
13 14

/**
15
 * Defines a service for comment #lazy_builder callbacks.
16
 */
17
class CommentLazyBuilders {
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

  /**
   * The entity manager service.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * The entity form builder service.
   *
   * @var \Drupal\Core\Entity\EntityFormBuilderInterface
   */
  protected $entityFormBuilder;

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  /**
   * Comment manager service.
   *
   * @var \Drupal\comment\CommentManagerInterface
   */
  protected $commentManager;

  /**
   * Current logged in user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

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

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

61
  /**
62
   * Constructs a new CommentLazyBuilders object.
63 64 65 66 67
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager service.
   * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
   *   The entity form builder service.
68 69 70 71 72 73
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current logged in user.
   * @param \Drupal\comment\CommentManagerInterface $comment_manager
   *   The comment manager service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
74
   * @param \Drupal\Core\Render\RendererInterface $renderer
75
   *   The renderer service.
76
   */
77
  public function __construct(EntityManagerInterface $entity_manager, EntityFormBuilderInterface $entity_form_builder, AccountInterface $current_user, CommentManagerInterface $comment_manager, ModuleHandlerInterface $module_handler, RendererInterface $renderer) {
78 79
    $this->entityManager = $entity_manager;
    $this->entityFormBuilder = $entity_form_builder;
80 81 82 83
    $this->currentUser = $current_user;
    $this->commentManager = $comment_manager;
    $this->moduleHandler = $module_handler;
    $this->renderer = $renderer;
84 85 86
  }

  /**
87
   * #lazy_builder callback; builds the comment form.
88
   *
89 90 91 92 93 94 95 96
   * @param string $commented_entity_type_id
   *   The commented entity type ID.
   * @param string $commented_entity_id
   *   The commented entity ID.
   * @param string $field_name
   *   The comment field name.
   * @param string $comment_type_id
   *   The comment type ID.
97 98 99 100
   *
   * @return array
   *   A renderable array containing the comment form.
   */
101
  public function renderForm($commented_entity_type_id, $commented_entity_id, $field_name, $comment_type_id) {
102
    $values = array(
103 104 105 106
      'entity_type' => $commented_entity_type_id,
      'entity_id' => $commented_entity_id,
      'field_name' => $field_name,
      'comment_type' => $comment_type_id,
107 108 109
      'pid' => NULL,
    );
    $comment = $this->entityManager->getStorage('comment')->create($values);
110
    return $this->entityFormBuilder->getForm($comment);
111 112
  }

113
  /**
114
   * #lazy_builder callback; builds a comment's links.
115
   *
116 117 118 119 120 121 122
   * @param string $comment_entity_id
   *   The comment entity ID.
   * @param string $view_mode
   *   The view mode in which the comment entity is being viewed.
   * @param string $langcode
   *   The language in which the comment entity is being viewed.
   * @param bool $is_in_preview
123
   *   Whether the comment is currently being previewed.
124 125 126 127
   *
   * @return array
   *   A renderable array representing the comment links.
   */
128
  public function renderLinks($comment_entity_id, $view_mode, $langcode, $is_in_preview) {
129 130 131 132 133 134
    $links = array(
      '#theme' => 'links__comment',
      '#pre_render' => array('drupal_pre_render_links'),
      '#attributes' => array('class' => array('links', 'inline')),
    );

135
    if (!$is_in_preview) {
136
      /** @var \Drupal\comment\CommentInterface $entity */
137
      $entity = $this->entityManager->getStorage('comment')->load($comment_entity_id);
138 139 140 141 142 143
      $commented_entity = $entity->getCommentedEntity();

      $links['comment'] = $this->buildLinks($entity, $commented_entity);

      // Allow other modules to alter the comment links.
      $hook_context = array(
144 145
        'view_mode' => $view_mode,
        'langcode' => $langcode,
146 147 148 149
        'commented_entity' => $commented_entity,
      );
      $this->moduleHandler->alter('comment_links', $links, $entity, $hook_context);
    }
150
    return $links;
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  }

  /**
   * Build the default links (reply, edit, delete …) for a comment.
   *
   * @param \Drupal\comment\CommentInterface $entity
   *   The comment object.
   * @param \Drupal\Core\Entity\EntityInterface $commented_entity
   *   The entity to which the comment is attached.
   *
   * @return array
   *   An array that can be processed by drupal_pre_render_links().
   */
  protected function buildLinks(CommentInterface $entity, EntityInterface $commented_entity) {
    $links = array();
    $status = $commented_entity->get($entity->getFieldName())->status;

    if ($status == CommentItemInterface::OPEN) {
      if ($entity->access('delete')) {
        $links['comment-delete'] = array(
          'title' => t('Delete'),
          'url' => $entity->urlInfo('delete-form'),
        );
      }

      if ($entity->access('update')) {
        $links['comment-edit'] = array(
          'title' => t('Edit'),
          'url' => $entity->urlInfo('edit-form'),
        );
      }
      if ($entity->access('create')) {
        $links['comment-reply'] = array(
          'title' => t('Reply'),
          'url' => Url::fromRoute('comment.reply', [
            'entity_type' => $entity->getCommentedEntityTypeId(),
            'entity' => $entity->getCommentedEntityId(),
            'field_name' => $entity->getFieldName(),
            'pid' => $entity->id(),
          ]),
        );
      }
      if (!$entity->isPublished() && $entity->access('approve')) {
        $links['comment-approve'] = array(
          'title' => t('Approve'),
          'url' => Url::fromRoute('comment.approve', ['comment' => $entity->id()]),
        );
      }
      if (empty($links) && $this->currentUser->isAnonymous()) {
        $links['comment-forbidden']['title'] = $this->commentManager->forbiddenMessage($commented_entity, $entity->getFieldName());
      }
    }

    // Add translations link for translation-enabled comment bundles.
    if ($this->moduleHandler->moduleExists('content_translation') && $this->access($entity)->isAllowed()) {
      $links['comment-translations'] = array(
        'title' => t('Translate'),
        'url' => $entity->urlInfo('drupal:content-translation-overview'),
      );
    }

    return array(
      '#theme' => 'links__comment__comment',
      // The "entity" property is specified to be present, so no need to check.
      '#links' => $links,
      '#attributes' => array('class' => array('links', 'inline')),
    );
  }

  /**
   * Wraps content_translation_translate_access.
   */
  protected function access(EntityInterface $entity) {
    return content_translation_translate_access($entity);
  }

227
}