diff --git a/core/modules/comment/comment.admin.inc b/core/modules/comment/comment.admin.inc deleted file mode 100644 index 6502873..0000000 --- a/core/modules/comment/comment.admin.inc +++ /dev/null @@ -1,217 +0,0 @@ -request->all(); - - if (isset($edit['operation']) && ($edit['operation'] == 'delete') && isset($edit['comments']) && $edit['comments']) { - return drupal_get_form(ConfirmDeleteMultiple::create(\Drupal::getContainer()), $request); - } - else { - return drupal_get_form('comment_admin_overview', $type); - } -} - -/** - * Form constructor for the comment overview administration form. - * - * @param $arg - * The type of overview form ('approval' or 'new'). - * - * @ingroup forms - * @see comment_admin() - * @see comment_admin_overview_validate() - * @see comment_admin_overview_submit() - * @see theme_comment_admin_overview() - */ -function comment_admin_overview($form, &$form_state, $arg) { - // Build an 'Update options' form. - $form['options'] = array( - '#type' => 'details', - '#title' => t('Update options'), - '#attributes' => array('class' => array('container-inline')), - ); - - if ($arg == 'approval') { - $options['publish'] = t('Publish the selected comments'); - } - else { - $options['unpublish'] = t('Unpublish the selected comments'); - } - $options['delete'] = t('Delete the selected comments'); - - $form['options']['operation'] = array( - '#type' => 'select', - '#title' => t('Action'), - '#title_display' => 'invisible', - '#options' => $options, - '#default_value' => 'publish', - ); - $form['options']['submit'] = array( - '#type' => 'submit', - '#value' => t('Update'), - ); - - // Load the comments that need to be displayed. - $status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED; - $header = array( - 'subject' => array('data' => t('Subject'), 'field' => 'subject'), - 'author' => array('data' => t('Author'), 'field' => 'name', 'class' => array(RESPONSIVE_PRIORITY_MEDIUM)), - 'posted_in' => array('data' => t('Posted in'), 'field' => 'node_title', 'class' => array(RESPONSIVE_PRIORITY_LOW)), - 'changed' => array('data' => t('Updated'), 'field' => 'c.changed', 'sort' => 'desc', 'class' => array(RESPONSIVE_PRIORITY_LOW)), - 'operations' => t('Operations'), - ); - - $query = db_select('comment', 'c') - ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('Drupal\Core\Database\Query\TableSortExtender'); - $query->join('node_field_data', 'n', 'n.nid = c.nid'); - $query->addTag('node_access'); - $result = $query - ->fields('c', array('cid', 'nid', 'subject', 'name', 'changed')) - ->condition('c.status', $status) - ->limit(50) - ->orderByHeader($header) - ->execute(); - - $nids = array(); - $cids = array(); - - // We collect a sorted list of node_titles during the query to attach to the - // comments later. - foreach ($result as $row) { - $nids[] = $row->nid; - $cids[] = $row->cid; - } - // Ensure all nodes are statically cached so that we do not have to load them - // individually when getting their labels below. - node_load_multiple($nids); - $comments = comment_load_multiple($cids); - - // Build a table listing the appropriate comments. - $options = array(); - $destination = drupal_get_destination(); - - foreach ($comments as $comment) { - // Remove the first node title from the node_titles array and attach to - // the comment. - $node_title = $comment->nid->entity->label(); - $username = array( - '#theme' => 'username', - '#account' => comment_prepare_author($comment), - ); - $options[$comment->id()] = array( - 'title' => array('data' => array('#title' => $comment->subject->value ?: $comment->id())), - 'subject' => array( - 'data' => array( - '#type' => 'link', - '#title' => $comment->subject->value, - '#href' => 'comment/' . $comment->id(), - '#options' => array('attributes' => array('title' => truncate_utf8($comment->comment_body->value, 128)), 'fragment' => 'comment-' . $comment->id()), - ), - ), - 'author' => drupal_render($username), - 'posted_in' => array( - 'data' => array( - '#type' => 'link', - '#title' => $node_title, - '#href' => 'node/' . $comment->nid->target_id, - ), - ), - 'changed' => format_date($comment->changed->value, 'short'), - ); - $links = array(); - $links['edit'] = array( - 'title' => t('edit'), - 'href' => 'comment/' . $comment->id() . '/edit', - 'query' => $destination, - ); - if (module_invoke('content_translation', 'translate_access', $comment)) { - $links['translate'] = array( - 'title' => t('translate'), - 'href' => 'comment/' . $comment->id() . '/translations', - 'query' => $destination, - ); - } - $options[$comment->id()]['operations']['data'] = array( - '#type' => 'operations', - '#links' => $links, - ); - } - - $form['comments'] = array( - '#type' => 'tableselect', - '#header' => $header, - '#options' => $options, - '#empty' => t('No comments available.'), - ); - - $form['pager'] = array('#theme' => 'pager'); - - return $form; -} - -/** - * Form validation handler for comment_admin_overview(). - * - * @see comment_admin_overview_submit() - */ -function comment_admin_overview_validate($form, &$form_state) { - $form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0)); - // We can't execute any 'Update options' if no comments were selected. - if (count($form_state['values']['comments']) == 0) { - form_set_error('', t('Select one or more comments to perform the update on.')); - } -} - -/** - * Form submission handler for comment_admin_overview(). - * - * Executes the chosen 'Update option' on the selected comments, such as - * publishing, unpublishing or deleting. - * - * @see comment_admin_overview_validate() - */ -function comment_admin_overview_submit($form, &$form_state) { - $operation = $form_state['values']['operation']; - $cids = $form_state['values']['comments']; - - if ($operation == 'delete') { - entity_delete_multiple('comment', $cids); - } - else { - foreach ($cids as $cid => $value) { - $comment = comment_load($value); - - if ($operation == 'unpublish') { - $comment->status->value = COMMENT_NOT_PUBLISHED; - } - elseif ($operation == 'publish') { - $comment->status->value = COMMENT_PUBLISHED; - } - $comment->save(); - } - } - drupal_set_message(t('The update has been performed.')); - $form_state['redirect'] = 'admin/content/comment'; - cache_invalidate_tags(array('content' => TRUE)); -} diff --git a/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php b/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php index 2470b2a..37f7c41 100644 --- a/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php +++ b/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php @@ -8,12 +8,12 @@ namespace Drupal\comment\Controller; use Drupal\comment\CommentInterface; -use Drupal\comment\Entity\Comment; +use Drupal\comment\Form\ConfirmDeleteMultiple; +use Drupal\comment\Form\CommentAdminOverview; +use Drupal\node\NodeInterface; use Drupal\Core\Access\CsrfTokenGenerator; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; -use Drupal\Node\NodeInterface; -use Drupal\Core\Session\AccountInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -44,26 +44,16 @@ class CommentController extends ControllerBase implements ContainerInjectionInte protected $csrfToken; /** - * The current user service. - * - * @var \Drupal\Core\Session\AccountInterface - */ - protected $currentUser; - - /** * Constructs a CommentController object. * * @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel * HTTP kernel to handle requests. * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token * The CSRF token manager service. - * @param \Drupal\Core\Session\AccountInterface $current_user - * The current user service. */ - public function __construct(HttpKernelInterface $httpKernel, CsrfTokenGenerator $csrf_token, AccountInterface $current_user) { + public function __construct(HttpKernelInterface $httpKernel, CsrfTokenGenerator $csrf_token) { $this->httpKernel = $httpKernel; $this->csrfToken = $csrf_token; - $this->currentUser = $current_user; } /** * {@inheritdoc} @@ -71,8 +61,7 @@ public function __construct(HttpKernelInterface $httpKernel, CsrfTokenGenerator public static function create(ContainerInterface $container) { return new static( $container->get('http_kernel'), - $container->get('csrf_token'), - $container->get('current_user') + $container->get('csrf_token') ); } @@ -243,11 +232,11 @@ public function getReplyForm(Request $request, NodeInterface $node, $pid = NULL) * @param \Symfony\Component\HttpFoundation\Request $request * The request of the page. * - * @return Symfony\Component\HttpFoundation\JsonResponse + * @return \Symfony\Component\HttpFoundation\JsonResponse * The JSON response. */ public function renderNewCommentsNodeLinks(Request $request) { - if ($this->currentUser->isAnonymous()) { + if ($this->currentUser()->isAnonymous()) { throw new AccessDeniedHttpException(); } @@ -273,11 +262,22 @@ public function renderNewCommentsNodeLinks(Request $request) { } /** - * @todo Remove comment_admin(). + * Presents an administrative comment listing. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The request of the page. + * @param string $type + * The type of the overview form ('approval' or 'new'). */ - public function adminPage($type) { - module_load_include('admin.inc', 'comment'); - return comment_admin($type); + public function adminPage(Request $request, $type) { + $edit = $request->request->all(); + + if (isset($edit['operation']) && ($edit['operation'] == 'delete') && isset($edit['comments']) && $edit['comments']) { + return drupal_get_form(ConfirmDeleteMultiple::create($this->container()), $request); + } + else { + return drupal_get_form(CommentAdminOverview::create($this->container()), $type); + } } } diff --git a/core/modules/comment/lib/Drupal/comment/Form/CommentAdminOverview.php b/core/modules/comment/lib/Drupal/comment/Form/CommentAdminOverview.php new file mode 100644 index 0000000..234467c --- /dev/null +++ b/core/modules/comment/lib/Drupal/comment/Form/CommentAdminOverview.php @@ -0,0 +1,266 @@ +commentStorage = $comment_storage; + $this->connection = $connection; + $this->date = $date; + $this->moduleHandler = $module_handler; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager')->getStorageController('comment'), + $container->get('database'), + $container->get('date'), + $container->get('module_handler') + + ); + } + + /** + * {@inheritdoc} + */ + public function getFormID() { + return 'comment_admin_overview'; + } + + /** + * Form constructor for the comment overview administration form. + * + * @param array $form + * An associative array containing the structure of the form. + * @param array $form_state + * An associative array containing the current state of the form. + * @param string $type + * The type of the overview form ('approval' or 'new'). + * + * @return array + * The form structure. + */ + public function buildForm(array $form, array &$form_state, $type = 'new') { + + // Build an 'Update options' form. + $form['options'] = array( + '#type' => 'details', + '#title' => $this->t('Update options'), + '#attributes' => array('class' => array('container-inline')), + ); + + if ($type == 'approval') { + $options['publish'] = $this->t('Publish the selected comments'); + } + else { + $options['unpublish'] = $this->t('Unpublish the selected comments'); + } + $options['delete'] = $this->t('Delete the selected comments'); + + $form['options']['operation'] = array( + '#type' => 'select', + '#title' => $this->t('Action'), + '#title_display' => 'invisible', + '#options' => $options, + '#default_value' => 'publish', + ); + $form['options']['submit'] = array( + '#type' => 'submit', + '#value' => $this->t('Update'), + ); + + // Load the comments that need to be displayed. + $status = ($type == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED; + $header = array( + 'subject' => array('data' => $this->t('Subject'), 'field' => 'subject'), + 'author' => array('data' => $this->t('Author'), 'field' => 'name', 'class' => array(RESPONSIVE_PRIORITY_MEDIUM)), + 'posted_in' => array('data' => $this->t('Posted in'), 'field' => 'node_title', 'class' => array(RESPONSIVE_PRIORITY_LOW)), + 'changed' => array('data' => $this->t('Updated'), 'field' => 'c.changed', 'sort' => 'desc', 'class' => array(RESPONSIVE_PRIORITY_LOW)), + 'operations' => $this->t('Operations'), + ); + + $query = $this->connection->select('comment', 'c') + ->extend('\Drupal\Core\Database\Query\PagerSelectExtender') + ->extend('\Drupal\Core\Database\Query\TableSortExtender'); + $query->join('node_field_data', 'n', 'n.nid = c.nid'); + $query->addTag('node_access'); + $result = $query + ->fields('c', array('cid', 'nid', 'subject', 'name', 'changed')) + ->condition('c.status', $status) + ->limit(50) + ->orderByHeader($header) + ->execute(); + + $cids = array(); + + // We collect a sorted list of node_titles during the query to attach to the + // comments later. + foreach ($result as $row) { + $cids[] = $row->cid; + } + $comments = $this->commentStorage->loadMultiple($cids); + + // Build a table listing the appropriate comments. + $options = array(); + $destination = drupal_get_destination(); + + foreach ($comments as $comment) { + // Remove the first node title from the node_titles array and attach to + // the comment. + $node_title = $comment->nid->entity->label(); + $username = array( + '#theme' => 'username', + '#account' => comment_prepare_author($comment), + ); + $options[$comment->id()] = array( + 'title' => array('data' => array('#title' => $comment->subject->value ?: $comment->id())), + 'subject' => array( + 'data' => array( + '#type' => 'link', + '#title' => $comment->subject->value, + '#href' => 'comment/' . $comment->id(), + '#options' => array('attributes' => array('title' => truncate_utf8($comment->comment_body->value, 128)), 'fragment' => 'comment-' . $comment->id()), + ), + ), + 'author' => drupal_render($username), + 'posted_in' => array( + 'data' => array( + '#type' => 'link', + '#title' => $node_title, + '#href' => 'node/' . $comment->nid->target_id, + ), + ), + 'changed' => $this->date->format($comment->changed->value, 'short'), + ); + $links = array(); + $links['edit'] = array( + 'title' => $this->t('edit'), + 'href' => 'comment/' . $comment->id() . '/edit', + 'query' => $destination, + ); + if ($this->moduleHandler->invoke('content_translation', 'translate_access', $comment)) { + $links['translate'] = array( + 'title' => $this->t('translate'), + 'href' => 'comment/' . $comment->id() . '/translations', + 'query' => $destination, + ); + } + $options[$comment->id()]['operations']['data'] = array( + '#type' => 'operations', + '#links' => $links, + ); + } + + $form['comments'] = array( + '#type' => 'tableselect', + '#header' => $header, + '#options' => $options, + '#empty' => $this->t('No comments available.'), + ); + + $form['pager'] = array('#theme' => 'pager'); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state){ + $form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0)); + // We can't execute any 'Update options' if no comments were selected. + if (count($form_state['values']['comments']) == 0) { + form_set_error('', $this->t('Select one or more comments to perform the update on.')); + } + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $operation = $form_state['values']['operation']; + $cids = $form_state['values']['comments']; + + if ($operation == 'delete') { + $comments = $this->commentStorage->loadMultiple($cids); + $this->commentStorage->delete($comments); + } + else { + foreach ($cids as $cid) { + $comment = $this->commentStorage->load($cid); + + if ($operation == 'unpublish') { + $comment->status->value = COMMENT_NOT_PUBLISHED; + } + elseif ($operation == 'publish') { + $comment->status->value = COMMENT_PUBLISHED; + } + $comment->save(); + } + } + drupal_set_message($this->t('The update has been performed.')); + $form_state['redirect'] = 'admin/content/comment'; + Cache::invalidateTags(array('content' => TRUE)); + } + +} diff --git a/core/modules/comment/lib/Drupal/comment/Form/ConfirmDeleteMultiple.php b/core/modules/comment/lib/Drupal/comment/Form/ConfirmDeleteMultiple.php index 08fb6d0..100fecc 100644 --- a/core/modules/comment/lib/Drupal/comment/Form/ConfirmDeleteMultiple.php +++ b/core/modules/comment/lib/Drupal/comment/Form/ConfirmDeleteMultiple.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\comment\Form\DeleteConfirmMultiple. + * Contains \Drupal\comment\Form\ConfirmDeleteMultiple. */ namespace Drupal\comment\Form; @@ -71,6 +71,9 @@ public function getQuestion() { * {@inheritdoc} */ public function getCancelRoute() { + return array( + 'route_name' => 'comment.admin', + ); } /** @@ -111,11 +114,7 @@ public function buildForm(array $form, array &$form_state, Request $request = NU $form_state['redirect'] = 'admin/content/comment'; } - $form = parent::buildForm($form, $form_state); - - // @todo Convert to getCancelRoute() after http://drupal.org/node/1986606. - $form['actions']['cancel']['#href'] = 'admin/content/comment'; - return $form; + return parent::buildForm($form, $form_state); } /**