This issue is a duplicate of https://www.drupal.org/node/2461099, but for Drupal 8.

attached is a patch that fixes the Drupal 8 version. Please review!

Comments

sagesolutions created an issue. See original summary.

zero2one’s picture

diff --git a/modules/contrib/views_random_seed/src/SeedCalculator.php b/modules/contrib/views_random_seed/src/SeedCalculator.php
index 73ed9ed20..bf753e115 100644
--- a/src/SeedCalculator.php
+++ b/src/SeedCalculator.php
@@ -131,7 +131,7 @@ protected function createInt($time, $db_type) {
    * @return \Symfony\Component\HttpFoundation\Session\SessionInterface
    */
   protected function getSession() {
-    return $this->requestStack->getCurrentRequest()->getSession();
+    return \Drupal::service('session');
   }

The current session is now retrieved (using Dependency injection) from the injected requestStack.

Don't access services using \Drupal::service() in classes, it makes it hard/impossible to write unit tests.

zero2one’s picture

The second problem with the suggested patch is that you create a hard link between the views_random_seed module and the Search API module. So you can no longer install the views_random_seed without the Search API module…

I fixed the missing random sort functionality by implementing the hook_views_query_alter().

/**
 * Implements hook_views_query_alter().
 *
 * Use SearchAPI random sorting with seed if the query object is an instance of
 * SearchApiViewsQuery (or a subclass of it). Pass along the seed as options for
 * the SearchApiQuery class.
 *
 * @see https://www.drupal.org/node/1197538#comment-10190520
 * @see https://www.drupal.org/project/views_random_seed/issues/2939507
 */
function MODULENAME_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  // Only for SearchApiQueries.
  if (!$query instanceof SearchApiQuery) {
    return;
  }

  // Only if ViewsRandomSeedRandom is in use.
  $random_sort = NULL;
  foreach ($view->sort as $key => $sort) {
    if ($sort instanceof ViewsRandomSeedRandom) {
      $random_sort = $sort;
      break;
    }
  }
  if (!$random_sort) {
    return;
  }

  /* @var $random_sort \Drupal\views_random_seed\Plugin\views\sort\ViewsRandomSeedRandom */
  $seed = \Drupal::service('views_random_seed.seed_calculator')
    ->calculateSeed(
      $random_sort->options,
      $view->id(),
      $view->current_display,
      \Drupal::database()->driver()
    );

  $query->addOrderBy('rand', NULL, NULL, '', ['seed' => $seed]);
}

A permanent fix should be a custom views sort plugin?

NewZeal’s picture

StatusFileSize
new2.96 KB

Thanks SageSolutions, for the awesome patch. I tried it on 8.x-1.0-alpha1 but it failed to fire. The patch is expecting different code. So attached is a rewrite for 8.x-1.0-alpha1 that works for me. Also injects that session service and removes logger.

maskedjellybean’s picture

StatusFileSize
new2.93 KB

If anyone needs to combine this with https://www.drupal.org/project/views_random_seed/issues/2860769#comment-..., the attached patch will work for 8.x-1.0-alpha1.

mathiasgmeiner’s picture

I've had to change the getSession() part to be able to apply the patch #5

from

   protected function getSession() {
-    return \Drupal::service('session');
+    return $this->session;
   }

to

   protected function getSession() {
-    return $this->requestStack->getCurrentRequest()->getSession();
+    return $this->session;
   }

  • swentel committed 6e97dde on 8.x-1.x
    Issue #2939507 by sagesolutions, New Zeal, maskedjellybean: Search API...
swentel’s picture

Status: Needs review » Fixed

committed and pushed (with slight changes)
Available in dev!

swentel’s picture

Status: Fixed » Closed (fixed)