Loading config/schema/views_random_seed.schema.yml +1 −1 Original line number Diff line number Diff line Loading @@ -9,5 +9,5 @@ views.sort.views_random_seed_random: type: integer label: 'Reset seed' reset_seed_custom: type: string type: integer label: 'Custom reset seed' src/Plugin/views/sort/ViewsRandomSeedRandom.php +20 −7 Original line number Diff line number Diff line Loading @@ -58,6 +58,20 @@ class ViewsRandomSeedRandom extends SortPluginBase { return $options; } /** * {@inheritdoc} */ public function canExpose() { return FALSE; } /** * {@inheritdoc} */ public function adminSummary() { return ""; } /** * {@inheritdoc} */ Loading @@ -82,11 +96,11 @@ class ViewsRandomSeedRandom extends SortPluginBase { '#type' => 'radios', '#title' => $this->t('Reset seed'), '#options' => [ 'never' => $this->t('Never'), 'custom' => $this->t('Custom'), '3600' => $this->t('Every hour'), '28800' => $this->t('Every eight hours'), '86400' => $this->t('Every day'), -1 => $this->t('Never'), 0 => $this->t('Custom'), 3600 => $this->t('Every hour'), 28800 => $this->t('Every eight hours'), 86400 => $this->t('Every day'), ], '#default_value' => $this->options['reset_seed_int'] ?? '3600', ]; Loading @@ -101,7 +115,7 @@ class ViewsRandomSeedRandom extends SortPluginBase { '#default_value' => $this->options['reset_seed_custom'] ?? '300', '#description' => $this->t('Define your own custom reset time, must be a number and is in seconds. Choose custom in the options above.'), '#states' => [ 'visible' => [':input[name="options[reset_seed_int]"]' => ['value' => 'custom']] 'visible' => [':input[name="options[reset_seed_int]"]' => ['value' => '0']] ], ]; } Loading Loading @@ -150,4 +164,3 @@ class ViewsRandomSeedRandom extends SortPluginBase { } } src/SeedCalculator.php +13 −15 Original line number Diff line number Diff line Loading @@ -43,7 +43,7 @@ class SeedCalculator { * @return int * Seed value which is a timestamp. */ public function calculateSeed($options, $view_name, $display, $db_type) { public function calculateSeed(array $options, string $view_name, string $display, string $db_type) { $time = (int) $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME'); $seed_name = 'views_seed_name-' . $view_name . '-' . $display; $seed_time = 'views_seed_time-' . $view_name . '-' . $display; Loading @@ -56,9 +56,9 @@ class SeedCalculator { $seed = $this->generateSeed($options['user_seed_type'], $seed_name, $seed_time, $time, $db_type); } // Reset seed or not ? if ($options['reset_seed_int'] !== 'never') { $reset_time = $options['reset_seed_int']; // Reset seed or not? -1 is never, 0 is custom. if ($options['reset_seed_int'] !== -1) { $reset_time = $options['reset_seed_int'] === 0 ? $options['reset_seed_custom'] : $options['reset_seed_int']; if (($this->getSession()->get($seed_time, FALSE) + $reset_time) < $time) { $this->keyValueStore->delete($seed_name); $seed = $this->generateSeed($options['user_seed_type'], $seed_name, $seed_time, $time, $db_type); Loading Loading @@ -88,7 +88,7 @@ class SeedCalculator { * @return int * The seed value. */ protected function generateSeed($user_seed_type, $seed_name, $seed_time, $time, $db_type) { protected function generateSeed(string $user_seed_type, string $seed_name, string $seed_time, int $time, string $db_type) { // Different per user, simply return $time. if ($user_seed_type === 'diff_per_user') { $seed = $this->createInt($time, $db_type); Loading @@ -108,25 +108,23 @@ class SeedCalculator { } /** * Helper function to create a seed based on db_type. MySQL can * handle any integer in the RAND() function, Postgres needs * an int between 0 and 1. * Helper function to create a seed based on db_type. MySQL can handle any * integer in the RAND() function, Postgres needs an int between 0 and 1. * * @param int $time Current timestamp. * @param string $db_type the current database type (mysql(i) - pgsql) * @param int $time * The current timestamp. * @param string $db_type * The current database type (mysql(i) - pgsql) * * @return int $seed timestamp or int between 0 and 1. */ protected function createInt($time, $db_type) { protected function createInt(int $time, string $db_type) { switch ($db_type) { case 'mysql': case 'mysqli': return $time; break; case 'pgsql': $seed = $time / 10000000000; return $seed; break; return $time / 10000000000; } } Loading views_random_seed.module +1 −1 Original line number Diff line number Diff line Loading @@ -18,7 +18,7 @@ function views_random_seed_views_data() { $data['views']['random_seed'] = [ 'title' => t('Random seed'), 'help' => t('Randomize the display order with a seed which makes paging possible.'), 'help' => t('Randomize the display order with a seed which makes paging possible.') . '<br />If you want to cache this view, use time-based caching.', 'sort' => [ 'id' => 'views_random_seed_random', ], Loading Loading
config/schema/views_random_seed.schema.yml +1 −1 Original line number Diff line number Diff line Loading @@ -9,5 +9,5 @@ views.sort.views_random_seed_random: type: integer label: 'Reset seed' reset_seed_custom: type: string type: integer label: 'Custom reset seed'
src/Plugin/views/sort/ViewsRandomSeedRandom.php +20 −7 Original line number Diff line number Diff line Loading @@ -58,6 +58,20 @@ class ViewsRandomSeedRandom extends SortPluginBase { return $options; } /** * {@inheritdoc} */ public function canExpose() { return FALSE; } /** * {@inheritdoc} */ public function adminSummary() { return ""; } /** * {@inheritdoc} */ Loading @@ -82,11 +96,11 @@ class ViewsRandomSeedRandom extends SortPluginBase { '#type' => 'radios', '#title' => $this->t('Reset seed'), '#options' => [ 'never' => $this->t('Never'), 'custom' => $this->t('Custom'), '3600' => $this->t('Every hour'), '28800' => $this->t('Every eight hours'), '86400' => $this->t('Every day'), -1 => $this->t('Never'), 0 => $this->t('Custom'), 3600 => $this->t('Every hour'), 28800 => $this->t('Every eight hours'), 86400 => $this->t('Every day'), ], '#default_value' => $this->options['reset_seed_int'] ?? '3600', ]; Loading @@ -101,7 +115,7 @@ class ViewsRandomSeedRandom extends SortPluginBase { '#default_value' => $this->options['reset_seed_custom'] ?? '300', '#description' => $this->t('Define your own custom reset time, must be a number and is in seconds. Choose custom in the options above.'), '#states' => [ 'visible' => [':input[name="options[reset_seed_int]"]' => ['value' => 'custom']] 'visible' => [':input[name="options[reset_seed_int]"]' => ['value' => '0']] ], ]; } Loading Loading @@ -150,4 +164,3 @@ class ViewsRandomSeedRandom extends SortPluginBase { } }
src/SeedCalculator.php +13 −15 Original line number Diff line number Diff line Loading @@ -43,7 +43,7 @@ class SeedCalculator { * @return int * Seed value which is a timestamp. */ public function calculateSeed($options, $view_name, $display, $db_type) { public function calculateSeed(array $options, string $view_name, string $display, string $db_type) { $time = (int) $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME'); $seed_name = 'views_seed_name-' . $view_name . '-' . $display; $seed_time = 'views_seed_time-' . $view_name . '-' . $display; Loading @@ -56,9 +56,9 @@ class SeedCalculator { $seed = $this->generateSeed($options['user_seed_type'], $seed_name, $seed_time, $time, $db_type); } // Reset seed or not ? if ($options['reset_seed_int'] !== 'never') { $reset_time = $options['reset_seed_int']; // Reset seed or not? -1 is never, 0 is custom. if ($options['reset_seed_int'] !== -1) { $reset_time = $options['reset_seed_int'] === 0 ? $options['reset_seed_custom'] : $options['reset_seed_int']; if (($this->getSession()->get($seed_time, FALSE) + $reset_time) < $time) { $this->keyValueStore->delete($seed_name); $seed = $this->generateSeed($options['user_seed_type'], $seed_name, $seed_time, $time, $db_type); Loading Loading @@ -88,7 +88,7 @@ class SeedCalculator { * @return int * The seed value. */ protected function generateSeed($user_seed_type, $seed_name, $seed_time, $time, $db_type) { protected function generateSeed(string $user_seed_type, string $seed_name, string $seed_time, int $time, string $db_type) { // Different per user, simply return $time. if ($user_seed_type === 'diff_per_user') { $seed = $this->createInt($time, $db_type); Loading @@ -108,25 +108,23 @@ class SeedCalculator { } /** * Helper function to create a seed based on db_type. MySQL can * handle any integer in the RAND() function, Postgres needs * an int between 0 and 1. * Helper function to create a seed based on db_type. MySQL can handle any * integer in the RAND() function, Postgres needs an int between 0 and 1. * * @param int $time Current timestamp. * @param string $db_type the current database type (mysql(i) - pgsql) * @param int $time * The current timestamp. * @param string $db_type * The current database type (mysql(i) - pgsql) * * @return int $seed timestamp or int between 0 and 1. */ protected function createInt($time, $db_type) { protected function createInt(int $time, string $db_type) { switch ($db_type) { case 'mysql': case 'mysqli': return $time; break; case 'pgsql': $seed = $time / 10000000000; return $seed; break; return $time / 10000000000; } } Loading
views_random_seed.module +1 −1 Original line number Diff line number Diff line Loading @@ -18,7 +18,7 @@ function views_random_seed_views_data() { $data['views']['random_seed'] = [ 'title' => t('Random seed'), 'help' => t('Randomize the display order with a seed which makes paging possible.'), 'help' => t('Randomize the display order with a seed which makes paging possible.') . '<br />If you want to cache this view, use time-based caching.', 'sort' => [ 'id' => 'views_random_seed_random', ], Loading