CommentDefaultFormatter.php 8.31 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter.
6
7
 */

8
namespace Drupal\comment\Plugin\Field\FieldFormatter;
9
10

use Drupal\comment\CommentStorageControllerInterface;
11
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
12
use Drupal\Core\Entity\EntityViewBuilderInterface;
13
use Drupal\Core\Field\FieldItemListInterface;
14
use Drupal\Core\Session\AccountInterface;
15
use Drupal\Core\Field\FieldDefinitionInterface;
16
use Drupal\Core\Field\FormatterBase;
17
18
19
20
21
22
23
24
25
26
27
28
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a default comment formatter.
 *
 * @FieldFormatter(
 *   id = "comment_default",
 *   module = "comment",
 *   label = @Translation("Comment list"),
 *   field_types = {
 *     "comment"
29
30
31
 *   },
 *   edit = {
 *     "editor" = "disabled"
32
33
34
 *   },
 *   settings = {
 *     "pager_id" = 0
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 *   }
 * )
 */
class CommentDefaultFormatter extends FormatterBase implements ContainerFactoryPluginInterface {

  /**
   * The comment storage controller.
   *
   * @var \Drupal\comment\CommentStorageControllerInterface
   */
  protected $storageController;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The comment render controller.
   *
57
   * @var \Drupal\Core\Entity\EntityViewBuilderInterface
58
   */
59
  protected $viewBuilder;
60
61
62
63
64
65
66
67
68
69
70
71
72
73

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
    return new static(
      $plugin_id,
      $plugin_definition,
      $configuration['field_definition'],
      $configuration['settings'],
      $configuration['label'],
      $configuration['view_mode'],
      $container->get('current_user'),
      $container->get('entity.manager')->getStorageController('comment'),
74
      $container->get('entity.manager')->getViewBuilder('comment')
75
76
77
78
79
80
81
82
83
84
    );
  }

  /**
   * Constructs a new CommentDefaultFormatter.
   *
   * @param string $plugin_id
   *   The plugin_id for the formatter.
   * @param array $plugin_definition
   *   The plugin implementation definition.
85
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
86
87
88
89
90
91
92
93
94
   *   The definition of the field to which the formatter is associated.
   * @param array $settings
   *   The formatter settings.
   * @param string $label
   *   The formatter label display setting.
   * @param string $view_mode
   *   The view mode.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
95
   * @param \Drupal\comment\CommentStorageControllerInterface $comment_storage_controller
96
   *   The comment storage controller.
97
   * @param \Drupal\Core\Entity\EntityViewBuilderInterface $comment_view_builder
98
   *   The comment view builder.
99
   */
100
  public function __construct($plugin_id, array $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, AccountInterface $current_user, CommentStorageControllerInterface $comment_storage_controller, EntityViewBuilderInterface $comment_view_builder) {
101
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode);
102
    $this->viewBuilder = $comment_view_builder;
103
104
105
106
107
108
109
    $this->storageController = $comment_storage_controller;
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
110
  public function viewElements(FieldItemListInterface $items) {
111
112
113
    $elements = array();
    $output = array();

114
    $field_name = $this->fieldDefinition->getName();
115
116
117
118
    $entity = $items->getEntity();

    $status = $items->status;

119
    if ($status != CommentItemInterface::HIDDEN && empty($entity->in_preview) &&
120
121
122
123
124
125
126
127
128
129
130
131
132
133
      // Comments are added to the search results and search index by
      // comment_node_update_index() instead of by this formatter, so don't
      // return anything if the view mode is search_index or search_result.
      !in_array($this->viewMode, array('search_result', 'search_index'))) {
      $comment_settings = $this->getFieldSettings();

      // Only attempt to render comments if the entity has visible comments.
      // Unpublished comments are not included in
      // $entity->get($field_name)->comment_count, but unpublished comments
      // should display if the user is an administrator.
      if ((($entity->get($field_name)->comment_count && $this->currentUser->hasPermission('access comments')) ||
        $this->currentUser->hasPermission('administer comments'))) {
        $mode = $comment_settings['default_mode'];
        $comments_per_page = $comment_settings['per_page'];
134
        if ($cids = comment_get_thread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id'))) {
135
136
          $comments = $this->storageController->loadMultiple($cids);
          comment_prepare_thread($comments);
137
          $build = $this->viewBuilder->viewMultiple($comments);
138
          $build['pager']['#theme'] = 'pager';
139
140
141
          if (!empty($this->settings['pager_id'])) {
            $build['pager']['#element'] = $this->settings['pager_id'];
          }
142
143
144
145
146
          $output['comments'] = $build;
        }
      }

      // Append comment form if the comments are open and the form is set to
147
      // display below the entity. Do not show the form for the print view mode.
148
      if ($status == CommentItemInterface::OPEN && $comment_settings['form_location'] == COMMENT_FORM_BELOW && $this->viewMode != 'print') {
149
150
        // Only show the add comment form if the user has permission.
        if ($this->currentUser->hasPermission('post comments')) {
151
152
153
154
155
156
157
158
159
160
          // All users in the "anonymous" role can use the same form: it is fine
          // for this form to be stored in the render cache.
          if ($this->currentUser->isAnonymous()) {
            $output['comment_form'] = comment_add($entity, $field_name);
          }
          // All other users need a user-specific form, which would break the
          // render cache: hence use a #post_render_cache callback.
          else {
            $output['comment_form'] = array(
              '#type' => 'render_cache_placeholder',
161
              '#callback' => '\Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter::renderForm',
162
              '#context' => array(
163
                'entity_type' => $entity->getEntityTypeId(),
164
                'entity_id' => $entity->id(),
165
                'field_name' => $field_name,
166
167
168
              ),
            );
          }
169
170
171
172
        }
      }

      $elements[] = $output + array(
173
        '#theme' => 'comment_wrapper__' . $entity->getEntityTypeId() . '__' . $entity->bundle() . '__' . $field_name,
174
175
176
177
178
179
180
181
182
183
        '#entity' => $entity,
        '#display_mode' => $this->getFieldSetting('default_mode'),
        'comments' => array(),
        'comment_form' => array(),
      );
    }

    return $elements;
  }

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  /**
   * #post_render_cache callback; replaces placeholder with comment form.
   *
   * @param array $context
   *   An array with the following keys:
   *   - entity_type: an entity type
   *   - entity_id: an entity ID
   *   - field_name: a comment field name
   *
   * @return array
   *   A renderable array containing the comment form.
   */
  public static function renderForm(array $context) {
    $entity = entity_load($context['entity_type'], $context['entity_id']);
    return comment_add($entity, $context['field_name']);
  }

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
227
228
  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, array &$form_state) {
    $element = array();
    $element['pager_id'] = array(
      '#type' => 'select',
      '#title' => $this->t('Pager ID'),
      '#options' => range(0, 10),
      '#default_value' => empty($this->settings['pager_id']) ? 0 : $this->settings['pager_id'],
      '#description' => $this->t("Unless you're experiencing problems with pagers related to this field, you should leave this at 0. If using multiple pagers on one page you may need to set this number to a higher value so as not to conflict within the ?page= array. Large values will add a lot of commas to your URLs, so avoid if possible."),
    );
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    // Only show a summary if we're using a non-standard pager id.
    if (!empty($this->settings['pager_id'])) {
      return array($this->t('Pager ID: @id', array(
        '@id' => $this->settings['pager_id'],
      )));
    }
    return array();
  }

229
}