You are here

public function ViewsRandomSeedRandom::buildOptionsForm in Views random seed 8

Basic options for all sort criteria

Overrides SortPluginBase::buildOptionsForm

File

src/Plugin/views/sort/ViewsRandomSeedRandom.php, line 75

Class

ViewsRandomSeedRandom
Handle a random sort with seed.

Namespace

Drupal\views_random_seed\Plugin\views\sort

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $form['order']['#access'] = FALSE;
  $reuseSeedArray = [
    '' => $this
      ->t('None'),
  ];
  $viewList = Views::getAllViews();
  foreach ($viewList as $view) {
    foreach ($view
      ->get('display') as $display) {
      $reuseSeedArray[$view
        ->id() . '-' . $display['id']] = $view
        ->label() . ': ' . $display['display_title'];
    }
  }

  // Reuse seed from another view.
  $form['reuse_seed'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Reuse seed from another view display'),
    '#options' => $reuseSeedArray,
    '#description' => $this
      ->t('With this option enabled, you can sync results between views.'),
    '#default_value' => $this->options['reuse_seed'] ?? '',
  ];

  // User seed type.
  $form['user_seed_type'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('User seed type'),
    '#options' => [
      'same_per_user' => $this
        ->t('Use the same seed for every user'),
      'diff_per_user' => $this
        ->t('Use a different seed per user'),
    ],
    '#default_value' => $this->options['user_seed_type'] ?? 'same_per_user',
  ];

  // User seed anonymous.
  $form['anonymous_session'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Anonymous sessions'),
    '#default_value' => $this->options['anonymous_session'] ?? FALSE,
    '#description' => $this
      ->t('Start sessions for anonymous users, which impacts performance.<br />If left unchecked, the seed will be the same for all anonymous users.'),
    '#states' => [
      'visible' => [
        ':input[name="options[user_seed_type]"]' => [
          'value' => 'diff_per_user',
        ],
      ],
    ],
  ];

  // User seed type.
  $form['reset_seed_int'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Reset seed'),
    '#options' => [
      -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',
  ];

  // Custom time.
  $form['reset_seed_custom'] = [
    '#type' => 'number',
    '#min' => 0,
    '#size' => 10,
    '#title' => $this
      ->t('Custom reset seed'),
    '#required' => TRUE,
    '#default_value' => $this->options['reset_seed_custom'] ?? '300',
    '#description' => $this
      ->t('Define your own custom reset time in seconds.'),
    '#states' => [
      'visible' => [
        ':input[name="options[reset_seed_int]"]' => [
          'value' => '0',
        ],
      ],
    ],
  ];
}