Commit 0fd24bd9 authored by Tauany Bueno's avatar Tauany Bueno Committed by Ide Braakman
Browse files

Issue #3293335 by idebr, Tauany Bueno, alanmoreira, damiaosj: Update code to...

Issue #3293335 by idebr, Tauany Bueno, alanmoreira, damiaosj: Update code to follow Drupal coding standards
parent c5dbd751
Loading
Loading
Loading
Loading
+16 −19
Original line number Diff line number Diff line
@@ -6,28 +6,20 @@
 */

use Drupal\ajax_comments\Controller\AjaxCommentsController;
use Drupal\ajax_comments\FieldSettingsHelper;
use Drupal\ajax_comments\Utility;
use Drupal\comment\CommentInterface;
use Drupal\node\NodeInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Html;
use Drupal\Core\Ajax;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Ajax\CloseDialogCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FormatterInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatch;
use Symfony\Component\HttpFoundation\Request;

/**
 * Implements hook_comment_links_alter().
@@ -35,8 +27,7 @@ use Symfony\Component\HttpFoundation\Request;
 * For entity bundles with ajax comments enabled, alter the comment link
 * classes to allow ajax behaviors to be attached.
 */
function ajax_comments_comment_links_alter(array &$links, CommentInterface &$entity, array &$context) {
  $request = \Drupal::request();
function ajax_comments_comment_links_alter(array &$links, CommentInterface $entity, array &$context) {
  /** @var \Drupal\ajax_comments\FieldSettingsHelper $field_settings_helper */
  $field_settings_helper = \Drupal::service('ajax_comments.field_settings_helper');
  $comment_formatter = $field_settings_helper->getFieldFormatterFromComment($entity, $context['view_mode']);
@@ -135,7 +126,11 @@ function ajax_comments_node_links_alter(array &$node_links, NodeInterface $node,
  $links = \Drupal::service('comment.link_builder')->buildCommentedEntityLinks($node, $context);
  foreach ($links as $link_name => $link) {
    $field_name = substr($link_name, strlen('comment__'));
    $classes = ['js-use-ajax-comments', 'js-ajax-comments-reply', 'js-ajax-comments-reply-' . $node->id() . '-' . $field_name . '-0'];
    $classes = [
      'js-use-ajax-comments',
      'js-ajax-comments-reply',
      'js-ajax-comments-reply-' . $node->id() . '-' . $field_name . '-0',
    ];
    if (!empty($link['#links']['comment-add'])) {
      if (empty($link['#links']['comment-add']['attributes']['class'])) {
        $links[$link_name]['#links']['comment-add']['attributes']['class'] = $classes;
@@ -218,7 +213,10 @@ function ajax_comments_form_comment_form_alter(&$form, FormStateInterface $form_
  // If the user is on the standalone comment reply page, it means JavaScript
  // is disabled or the ajax functionality is not working. Do not proceed with
  // the form alter.
  if (in_array($current_route->getRouteName(), ['comment.reply', 'entity.comment.edit_form'])) {
  if (in_array($current_route->getRouteName(), [
    'comment.reply',
    'entity.comment.edit_form',
  ])) {
    return;
  }

@@ -226,7 +224,6 @@ function ajax_comments_form_comment_form_alter(&$form, FormStateInterface $form_
  $comment = $form_state->getFormObject()->getEntity();
  /** @var \Drupal\Core\Entity\EntityInterface $commented_entity */
  $commented_entity = $comment->getCommentedEntity();
  $field_name = $comment->getFieldName();

  // Check to see if this node type uses ajax comments.
  $comment_formatter = $field_settings_helper->getFieldFormatterFromComment($comment, 'full');
@@ -299,11 +296,11 @@ function ajax_comments_preprocess_pager(&$variables) {
  // Query parameters are added in
  // core/includes/pager.inc in the template_preprocess_pager(),
  // where pager_query_add_page() calls pager_get_query_parameters(),
  // which will pick up the ajax wrapper format from the ajax request, which breaks
  // the pager.
  // Unfortunately there is no way to remove this parameter before it is rendered to text,
  // so this preprocess function removes the parameter with string replacement.

  // which will pick up the ajax wrapper format from the ajax request,
  // which breaks the pager.
  // Unfortunately there is no way to remove this parameter before it is
  // rendered to text, so this preprocess function removes the parameter with
  // string replacement.
  // Remove ajax wrapper format from first, previous.
  if (isset($variables['items']['first'])) {
    $variables['items']['first']['href'] = str_replace('_wrapper_format=drupal_ajax&', '', $variables['items']['first']['href']);
+2 −7
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Contains \Drupal\ajax_comments\Ajax\ajaxCommentsScrollToElementCommand.
 */

namespace Drupal\ajax_comments\Ajax;

use Drupal\Core\Ajax\CommandInterface;
@@ -14,7 +9,7 @@ use Drupal\Core\Ajax\CommandInterface;
 *
 * @ingroup ajax
 */
class ajaxCommentsScrollToElementCommand Implements CommandInterface {
class AjaxCommentsScrollToElementCommand implements CommandInterface {

  /**
   * The CSS selector for the element.
@@ -27,7 +22,7 @@ class ajaxCommentsScrollToElementCommand Implements CommandInterface {
   * Constructs a ajaxCommentsScrollToElementCommand object.
   *
   * @param string $selector
   *
   *   The selector for the command.
   */
  public function __construct($selector) {
    $this->selector = $selector;
+49 −20
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\PrependCommand;
use Drupal\Core\Ajax\RemoveCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
@@ -72,6 +73,20 @@ class AjaxCommentsController extends ControllerBase {
   */
  protected $messenger;

  /**
   * The Configuration Factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

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

  /**
   * Constructs a AjaxCommentsController object.
   *
@@ -87,14 +102,17 @@ class AjaxCommentsController extends ControllerBase {
   *   The TempStore service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The Messenger service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The Configuration Factory service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user, RendererInterface $renderer, RouterInterface $router, TempStore $temp_store, MessengerInterface $messenger) {
  public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user, RendererInterface $renderer, RouterInterface $router, TempStore $temp_store, MessengerInterface $messenger, ConfigFactoryInterface $config_factory) {
    $this->entityTypeManager = $entity_type_manager;
    $this->currentUser = $current_user;
    $this->renderer = $renderer;
    $this->router = $router;
    $this->tempStore = $temp_store;
    $this->messenger = $messenger;
    $this->configFactory = $config_factory;
  }

  /**
@@ -107,7 +125,8 @@ class AjaxCommentsController extends ControllerBase {
      $container->get('renderer'),
      $container->get('router.no_access_checks'),
      $container->get('ajax_comments.temp_store'),
      $container->get('messenger')
      $container->get('messenger'),
      $container->get('config.factory')
    );
  }

@@ -228,7 +247,7 @@ class AjaxCommentsController extends ControllerBase {
   *   The modified ajax response.
   */
  protected function addMessages(Request $request, AjaxResponse $response, $selector = '', $position = 'prepend') {
    $settings = \Drupal::config('ajax_comments.settings');
    $settings = $this->configFactory->get('ajax_comments.settings');
    $notify = $settings->get('notify');

    if ($notify || !empty($this->messenger->messagesByType(MessengerInterface::TYPE_ERROR))) {
@@ -332,15 +351,17 @@ class AjaxCommentsController extends ControllerBase {
      $form = $this->entityFormBuilder()->getForm($comment);
      $response->addCommand(new AfterCommand(static::getCommentSelectorPrefix() . $comment->id(), $form));

      // TODO: Get this custom ajax command working later.
      // @todo Get this custom ajax command working later.
      // @codingStandardsIgnoreStart
      // if (\Drupal::config('ajax_comments.settings')->get('enable_scroll')) {
      //   $response->addCommand(new ajaxCommentsScrollToElementCommand('.ajax-comments-reply-form-' . $comment->getCommentedEntityId() . '-' . $comment->get('pid')->target_id . '-' . $comment->id()));
      // }

      // @codingStandardsIgnoreEnd
      // Don't delete the tempStore variables here; we need them
      // to persist for the save() method below, where the form returned
      // here will be submitted.
      // Instead, return the response without calling $this->tempStore->deleteAll().
      // Instead, return the response without calling
      // $this->tempStore->deleteAll().
      return $response;
    }
    else {
@@ -416,7 +437,7 @@ class AjaxCommentsController extends ControllerBase {
      $cid = $this->tempStore->getCid();

      // Try to insert the message above the new comment.
      if (!empty($cid) && !$errors && \Drupal::currentUser()->hasPermission('skip comment approval')) {
      if (!empty($cid) && !$errors && $this->currentUser()->hasPermission('skip comment approval')) {
        $selector = static::getCommentSelectorPrefix() . $cid;
        $response = $this->addMessages(
          $request,
@@ -454,10 +475,10 @@ class AjaxCommentsController extends ControllerBase {
    // core/misc/drupal.js is reinserted into the DOM, the following line of
    // code will execute, causing Drupal.attachBehaviors() to run on the entire
    // document, and reattach behaviors to DOM elements that already have them:
    // @code
    // @codingStandardsIgnoreStart
    // // Attach all behaviors.
    // domready(function () { Drupal.attachBehaviors(document, drupalSettings); });
    // @endcode
    // @codingStandardsIgnoreEnd
    $attachments = $response->getAttachments();
    // Need to have only 'core/drupalSettings' in the asset library list.
    // If neither 'core/drupalSettings', nor a library with a dependency on it,
@@ -494,10 +515,17 @@ class AjaxCommentsController extends ControllerBase {

    if ($cid != 0) {
      // Show the hidden anchor.
      $response->addCommand(new InvokeCommand('a#comment-' . $cid, 'show', [200, 'linear']));
      $response->addCommand(new InvokeCommand(
        'a#comment-' . $cid, 'show', [200, 'linear']
        )
      );

      // Show the hidden comment.
      $response->addCommand(new InvokeCommand(static::getCommentSelectorPrefix() . $cid, 'show', [200, 'linear']));
      $response->addCommand(new InvokeCommand(
        static::getCommentSelectorPrefix() . $cid, 'show',
        [200, 'linear']
        )
      );
    }

    // Remove the form.
@@ -658,10 +686,10 @@ class AjaxCommentsController extends ControllerBase {
    // core/misc/drupal.js is reinserted into the DOM, the following line of
    // code will execute, causing Drupal.attachBehaviors() to run on the entire
    // document, and reattach behaviors to DOM elements that already have them:
    // @code
    // @codingStandardsIgnoreStart
    // // Attach all behaviors.
    // domready(function () { Drupal.attachBehaviors(document, drupalSettings); });
    // @endcode
    // @codingStandardsIgnoreEnd
    $attachments = $response->getAttachments();
    // Need to have only 'core/drupalSettings' in the asset library list.
    // If neither 'core/drupalSettings', nor a library with a dependency on it,
@@ -738,7 +766,8 @@ class AjaxCommentsController extends ControllerBase {
      // Don't delete the tempStore variables here; we need them
      // to persist for the saveReply() method, where the form returned
      // here will be submitted.
      // Instead, return the response without calling $this->tempStore->deleteAll().
      // Instead, return the response without calling
      // $this->tempStore->deleteAll().
      return $response;
    }
    else {
@@ -794,21 +823,21 @@ class AjaxCommentsController extends ControllerBase {

    // Build a dummy comment entity to pass to $this->save(), which will use
    // it to rebuild the comment entity form to trigger form submission.
    // @code
    // @codingStandardsIgnoreStart
    // $form = $this->entityFormBuilder()->getForm($comment, 'default', ['editing' => TRUE]);
    // @endcode
    // @codingStandardsIgnoreEnd
    // Note that this approach will correctly process the form submission
    // even though we are passing in an empty, dummy comment, because two steps
    // later in the call stack, \Drupal\Core\Form\FormBuilder::buildForm() is
    // called, and it checks the current request object for form submission
    // values if there aren't any in the form state, yet:
    // @code
    // @codingStandardsIgnoreStart
    // $input = $form_state->getUserInput();
    // if (!isset($input)) {
    //   $input = $form_state->isMethodType('get') ? $request->query->all() : $request->request->all();
    //   $form_state->setUserInput($input);
    // }
    // @endcode
    // @codingStandardsIgnoreEnd
    // This approach is very similar to the one taken in
    // \Drupal\comment\CommentLazyBuilders::renderForm().
    $comment = $this->entityTypeManager()->getStorage('comment')->create([
@@ -836,7 +865,7 @@ class AjaxCommentsController extends ControllerBase {
   *   (optional) Some comments are replies to other comments. In those cases,
   *   $pid is the parent comment's comment ID. Defaults to NULL.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse $response
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   The ajax response, if access is denied.
   */
  public function replyAccess(Request $request, AjaxResponse $response, EntityInterface $entity, $field_name, $pid = NULL) {
@@ -855,7 +884,7 @@ class AjaxCommentsController extends ControllerBase {
      if (empty($selector)) {
        $selector = $wrapper_html_id;
      }
      $this->messenger->addError(t('You do not have permission to post a comment.'));
      $this->messenger->addError($this->t('You do not have permission to post a comment.'));
      // If this is a new top-level comment (not a reply to another comment so
      // no $pid), replace the comment form with the error message.
      if (empty($pid)) {
+3 −6
Original line number Diff line number Diff line
@@ -10,9 +10,7 @@ use Drupal\Core\Field\FormatterInterface;
use Drupal\Core\Field\FormatterPluginManager;

/**
 * Class FieldSettingsHelper.
 *
 * @package Drupal\ajax_comments
 * Provides various methods to work with the fields.
 */
class FieldSettingsHelper {

@@ -31,7 +29,7 @@ class FieldSettingsHelper {
  protected $fieldFormatterManager;

  /**
   * AjaxCommentsFieldSettings constructor.
   * FieldSettingsHelper constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
@@ -154,8 +152,7 @@ class FieldSettingsHelper {
   *   entity view mode.
   *
   * @return bool
   *   Whether or not ajax comments is enabled on the comment field's display
   *   settings.
   *   Whether ajax comments is enabled on the comment field's display settings.
   */
  public function isEnabled(FormatterInterface $comment_formatter) {
    return $comment_formatter->getThirdPartySetting('ajax_comments', 'enable_ajax_comments', '1');
+0 −2
Original line number Diff line number Diff line
@@ -15,8 +15,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides ajax enhancements to core Comment delete form.
 *
 * @package Drupal\ajax_comments
 */
class AjaxCommentsDeleteForm extends DeleteForm {

Loading