0

I used the approach provided here to reverse comments order in Drupal 8:

    /** Implements hook_query_TAG_alter().
 *
 * Alter comments query to order by DESC as well as the default ASC.
 */
function MYMODULENAME_query_comment_filter_alter(QueryAlterableInterface $query) {
  $orderby = &$query->getOrderBy();

  // Sorting for threaded comments.
  if (isset($orderby['torder'])) {
    // Sort by root parent first, then normal threaded.
    $query->addExpression("SUBSTRING_INDEX(c.thread, '.', 1)", 'rparent');
    $orderby = array('rparent' => 'DESC') + $orderby;
  }
  // Sorting for flat comments.
  else if (isset($orderby['c.cid'])) {
    $direction = 'DESC';

    $orderby['c.cid'] = $direction;
    $orderby = array('c.created' => $direction) + $orderby;
  }
}

but I receive the followig error when run the module:

Recoverable fatal error: Argument 1 passed to mymd_query_comment_filter_alter() must be an instance of QueryAlterableInterface, instance of Drupal\Core\Database\Query\PagerSelectExtender given, called in D:\wamp95\www\drupal83\core\lib\Drupal\Core\Extension\ModuleHandler.php on line 501 and defined in mymd_query_comment_filter_alter()

what is the problem and how should I solve it?

1
  • I think I should use: Drupal\Core\Database\Query\AlterableInterface $query as argument.
    – Mdeh
    Mar 14, 2017 at 14:35

1 Answer 1

1

I used Drupal\Core\Database\Query\AlterableInterface $query as argument.

It solved the problem.

2
  • Yes see the docs. It should be AlterableInterface. api.drupal.org/api/drupal/…
    – Kevin
    Mar 14, 2017 at 14:43
  • Yes, you need to use "AlterableInterface" instead of "QueryAlterableInterface". use Drupal\Core\Database\Query\AlterableInterface; function MYMODULENAME_query_comment_filter_alter(AlterableInterface$query) { //..... } May 31, 2021 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.