annotate core/modules/comment/src/Form/CommentAdminOverview.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\comment\Form;
Chris@0 4
Chris@0 5 use Drupal\comment\CommentInterface;
Chris@0 6 use Drupal\Component\Utility\Unicode;
Chris@0 7 use Drupal\Core\Datetime\DateFormatterInterface;
Chris@0 8 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@0 9 use Drupal\Core\Extension\ModuleHandlerInterface;
Chris@0 10 use Drupal\Core\Form\FormBase;
Chris@0 11 use Drupal\Core\Form\FormStateInterface;
Chris@14 12 use Drupal\Core\TempStore\PrivateTempStoreFactory;
Chris@0 13 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Provides the comments overview administration form.
Chris@14 17 *
Chris@14 18 * @internal
Chris@0 19 */
Chris@0 20 class CommentAdminOverview extends FormBase {
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The entity type manager.
Chris@0 24 *
Chris@0 25 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@0 26 */
Chris@0 27 protected $entityTypeManager;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * The comment storage.
Chris@0 31 *
Chris@0 32 * @var \Drupal\comment\CommentStorageInterface
Chris@0 33 */
Chris@0 34 protected $commentStorage;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * The date formatter service.
Chris@0 38 *
Chris@0 39 * @var \Drupal\Core\Datetime\DateFormatterInterface
Chris@0 40 */
Chris@0 41 protected $dateFormatter;
Chris@0 42
Chris@0 43 /**
Chris@0 44 * The module handler.
Chris@0 45 *
Chris@0 46 * @var \Drupal\Core\Extension\ModuleHandlerInterface
Chris@0 47 */
Chris@0 48 protected $moduleHandler;
Chris@0 49
Chris@0 50 /**
Chris@0 51 * The tempstore factory.
Chris@0 52 *
Chris@14 53 * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
Chris@0 54 */
Chris@0 55 protected $tempStoreFactory;
Chris@0 56
Chris@0 57 /**
Chris@0 58 * Creates a CommentAdminOverview form.
Chris@0 59 *
Chris@0 60 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@0 61 * The entity manager service.
Chris@0 62 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
Chris@0 63 * The date formatter service.
Chris@0 64 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
Chris@0 65 * The module handler.
Chris@14 66 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
Chris@0 67 * The tempstore factory.
Chris@0 68 */
Chris@0 69 public function __construct(EntityTypeManagerInterface $entity_type_manager, DateFormatterInterface $date_formatter, ModuleHandlerInterface $module_handler, PrivateTempStoreFactory $temp_store_factory) {
Chris@0 70 $this->entityTypeManager = $entity_type_manager;
Chris@0 71 $this->commentStorage = $entity_type_manager->getStorage('comment');
Chris@0 72 $this->dateFormatter = $date_formatter;
Chris@0 73 $this->moduleHandler = $module_handler;
Chris@0 74 $this->tempStoreFactory = $temp_store_factory;
Chris@0 75 }
Chris@0 76
Chris@0 77 /**
Chris@0 78 * {@inheritdoc}
Chris@0 79 */
Chris@0 80 public static function create(ContainerInterface $container) {
Chris@0 81 return new static(
Chris@0 82 $container->get('entity_type.manager'),
Chris@0 83 $container->get('date.formatter'),
Chris@0 84 $container->get('module_handler'),
Chris@14 85 $container->get('tempstore.private')
Chris@0 86 );
Chris@0 87 }
Chris@0 88
Chris@0 89 /**
Chris@0 90 * {@inheritdoc}
Chris@0 91 */
Chris@0 92 public function getFormId() {
Chris@0 93 return 'comment_admin_overview';
Chris@0 94 }
Chris@0 95
Chris@0 96 /**
Chris@0 97 * Form constructor for the comment overview administration form.
Chris@0 98 *
Chris@0 99 * @param array $form
Chris@0 100 * An associative array containing the structure of the form.
Chris@0 101 * @param \Drupal\Core\Form\FormStateInterface $form_state
Chris@0 102 * The current state of the form.
Chris@0 103 * @param string $type
Chris@0 104 * The type of the overview form ('approval' or 'new').
Chris@0 105 *
Chris@0 106 * @return array
Chris@0 107 * The form structure.
Chris@0 108 */
Chris@0 109 public function buildForm(array $form, FormStateInterface $form_state, $type = 'new') {
Chris@0 110
Chris@0 111 // Build an 'Update options' form.
Chris@0 112 $form['options'] = [
Chris@0 113 '#type' => 'details',
Chris@0 114 '#title' => $this->t('Update options'),
Chris@0 115 '#open' => TRUE,
Chris@0 116 '#attributes' => ['class' => ['container-inline']],
Chris@0 117 ];
Chris@0 118
Chris@0 119 if ($type == 'approval') {
Chris@0 120 $options['publish'] = $this->t('Publish the selected comments');
Chris@0 121 }
Chris@0 122 else {
Chris@0 123 $options['unpublish'] = $this->t('Unpublish the selected comments');
Chris@0 124 }
Chris@0 125 $options['delete'] = $this->t('Delete the selected comments');
Chris@0 126
Chris@0 127 $form['options']['operation'] = [
Chris@0 128 '#type' => 'select',
Chris@0 129 '#title' => $this->t('Action'),
Chris@0 130 '#title_display' => 'invisible',
Chris@0 131 '#options' => $options,
Chris@0 132 '#default_value' => 'publish',
Chris@0 133 ];
Chris@0 134 $form['options']['submit'] = [
Chris@0 135 '#type' => 'submit',
Chris@0 136 '#value' => $this->t('Update'),
Chris@0 137 ];
Chris@0 138
Chris@0 139 // Load the comments that need to be displayed.
Chris@0 140 $status = ($type == 'approval') ? CommentInterface::NOT_PUBLISHED : CommentInterface::PUBLISHED;
Chris@0 141 $header = [
Chris@0 142 'subject' => [
Chris@0 143 'data' => $this->t('Subject'),
Chris@0 144 'specifier' => 'subject',
Chris@0 145 ],
Chris@0 146 'author' => [
Chris@0 147 'data' => $this->t('Author'),
Chris@0 148 'specifier' => 'name',
Chris@0 149 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
Chris@0 150 ],
Chris@0 151 'posted_in' => [
Chris@0 152 'data' => $this->t('Posted in'),
Chris@0 153 'class' => [RESPONSIVE_PRIORITY_LOW],
Chris@0 154 ],
Chris@0 155 'changed' => [
Chris@0 156 'data' => $this->t('Updated'),
Chris@0 157 'specifier' => 'changed',
Chris@0 158 'sort' => 'desc',
Chris@0 159 'class' => [RESPONSIVE_PRIORITY_LOW],
Chris@0 160 ],
Chris@0 161 'operations' => $this->t('Operations'),
Chris@0 162 ];
Chris@0 163 $cids = $this->commentStorage->getQuery()
Chris@0 164 ->condition('status', $status)
Chris@0 165 ->tableSort($header)
Chris@0 166 ->pager(50)
Chris@0 167 ->execute();
Chris@0 168
Chris@0 169 /** @var $comments \Drupal\comment\CommentInterface[] */
Chris@0 170 $comments = $this->commentStorage->loadMultiple($cids);
Chris@0 171
Chris@0 172 // Build a table listing the appropriate comments.
Chris@0 173 $options = [];
Chris@0 174 $destination = $this->getDestinationArray();
Chris@0 175
Chris@0 176 $commented_entity_ids = [];
Chris@0 177 $commented_entities = [];
Chris@0 178
Chris@0 179 foreach ($comments as $comment) {
Chris@0 180 $commented_entity_ids[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId();
Chris@0 181 }
Chris@0 182
Chris@0 183 foreach ($commented_entity_ids as $entity_type => $ids) {
Chris@0 184 $commented_entities[$entity_type] = $this->entityTypeManager
Chris@0 185 ->getStorage($entity_type)
Chris@0 186 ->loadMultiple($ids);
Chris@0 187 }
Chris@0 188
Chris@0 189 foreach ($comments as $comment) {
Chris@0 190 /** @var $commented_entity \Drupal\Core\Entity\EntityInterface */
Chris@0 191 $commented_entity = $commented_entities[$comment->getCommentedEntityTypeId()][$comment->getCommentedEntityId()];
Chris@0 192 $comment_permalink = $comment->permalink();
Chris@0 193 if ($comment->hasField('comment_body') && ($body = $comment->get('comment_body')->value)) {
Chris@0 194 $attributes = $comment_permalink->getOption('attributes') ?: [];
Chris@0 195 $attributes += ['title' => Unicode::truncate($body, 128)];
Chris@0 196 $comment_permalink->setOption('attributes', $attributes);
Chris@0 197 }
Chris@0 198 $options[$comment->id()] = [
Chris@0 199 'title' => ['data' => ['#title' => $comment->getSubject() ?: $comment->id()]],
Chris@0 200 'subject' => [
Chris@0 201 'data' => [
Chris@0 202 '#type' => 'link',
Chris@0 203 '#title' => $comment->getSubject(),
Chris@0 204 '#url' => $comment_permalink,
Chris@0 205 ],
Chris@0 206 ],
Chris@0 207 'author' => [
Chris@0 208 'data' => [
Chris@0 209 '#theme' => 'username',
Chris@0 210 '#account' => $comment->getOwner(),
Chris@0 211 ],
Chris@0 212 ],
Chris@0 213 'posted_in' => [
Chris@0 214 'data' => [
Chris@0 215 '#type' => 'link',
Chris@0 216 '#title' => $commented_entity->label(),
Chris@0 217 '#access' => $commented_entity->access('view'),
Chris@18 218 '#url' => $commented_entity->toUrl(),
Chris@0 219 ],
Chris@0 220 ],
Chris@0 221 'changed' => $this->dateFormatter->format($comment->getChangedTimeAcrossTranslations(), 'short'),
Chris@0 222 ];
Chris@18 223 $comment_uri_options = $comment->toUrl()->getOptions() + ['query' => $destination];
Chris@0 224 $links = [];
Chris@0 225 $links['edit'] = [
Chris@0 226 'title' => $this->t('Edit'),
Chris@18 227 'url' => $comment->toUrl('edit-form', $comment_uri_options),
Chris@0 228 ];
Chris@0 229 if ($this->moduleHandler->moduleExists('content_translation') && $this->moduleHandler->invoke('content_translation', 'translate_access', [$comment])->isAllowed()) {
Chris@0 230 $links['translate'] = [
Chris@0 231 'title' => $this->t('Translate'),
Chris@18 232 'url' => $comment->toUrl('drupal:content-translation-overview', $comment_uri_options),
Chris@0 233 ];
Chris@0 234 }
Chris@0 235 $options[$comment->id()]['operations']['data'] = [
Chris@0 236 '#type' => 'operations',
Chris@0 237 '#links' => $links,
Chris@0 238 ];
Chris@0 239 }
Chris@0 240
Chris@0 241 $form['comments'] = [
Chris@0 242 '#type' => 'tableselect',
Chris@0 243 '#header' => $header,
Chris@0 244 '#options' => $options,
Chris@0 245 '#empty' => $this->t('No comments available.'),
Chris@0 246 ];
Chris@0 247
Chris@0 248 $form['pager'] = ['#type' => 'pager'];
Chris@0 249
Chris@0 250 return $form;
Chris@0 251 }
Chris@0 252
Chris@0 253 /**
Chris@0 254 * {@inheritdoc}
Chris@0 255 */
Chris@0 256 public function validateForm(array &$form, FormStateInterface $form_state) {
Chris@0 257 $form_state->setValue('comments', array_diff($form_state->getValue('comments'), [0]));
Chris@0 258 // We can't execute any 'Update options' if no comments were selected.
Chris@0 259 if (count($form_state->getValue('comments')) == 0) {
Chris@0 260 $form_state->setErrorByName('', $this->t('Select one or more comments to perform the update on.'));
Chris@0 261 }
Chris@0 262 }
Chris@0 263
Chris@0 264 /**
Chris@0 265 * {@inheritdoc}
Chris@0 266 */
Chris@0 267 public function submitForm(array &$form, FormStateInterface $form_state) {
Chris@0 268 $operation = $form_state->getValue('operation');
Chris@0 269 $cids = $form_state->getValue('comments');
Chris@0 270 /** @var \Drupal\comment\CommentInterface[] $comments */
Chris@0 271 $comments = $this->commentStorage->loadMultiple($cids);
Chris@0 272 if ($operation != 'delete') {
Chris@0 273 foreach ($comments as $comment) {
Chris@0 274 if ($operation == 'unpublish') {
Chris@0 275 $comment->setUnpublished();
Chris@0 276 }
Chris@0 277 elseif ($operation == 'publish') {
Chris@0 278 $comment->setPublished();
Chris@0 279 }
Chris@0 280 $comment->save();
Chris@0 281 }
Chris@17 282 $this->messenger()->addStatus($this->t('The update has been performed.'));
Chris@0 283 $form_state->setRedirect('comment.admin');
Chris@0 284 }
Chris@0 285 else {
Chris@0 286 $info = [];
Chris@0 287 /** @var \Drupal\comment\CommentInterface $comment */
Chris@0 288 foreach ($comments as $comment) {
Chris@0 289 $langcode = $comment->language()->getId();
Chris@0 290 $info[$comment->id()][$langcode] = $langcode;
Chris@0 291 }
Chris@0 292 $this->tempStoreFactory
Chris@17 293 ->get('entity_delete_multiple_confirm')
Chris@17 294 ->set($this->currentUser()->id() . ':comment', $info);
Chris@17 295 $form_state->setRedirect('entity.comment.delete_multiple_form');
Chris@0 296 }
Chris@0 297 }
Chris@0 298
Chris@0 299 }