You are here

protected function WizardPluginBase::build_sorts in Views (for Drupal 7) 8.3

Builds the form structure for selecting the view's sort order.

By default, this adds a "sorted by [date]" filter (when it is available).

1 call to WizardPluginBase::build_sorts()
WizardPluginBase::build_form in lib/Drupal/views/Plugin/views/wizard/WizardPluginBase.php
Implements Drupal\views\Plugin\views\wizard\WizardInterface::build_form().

File

lib/Drupal/views/Plugin/views/wizard/WizardPluginBase.php, line 510
Definition of Drupal\views\Plugin\views\wizard\WizardPluginBase.

Class

WizardPluginBase
Provides the interface and base class for Views Wizard plugins.

Namespace

Drupal\views\Plugin\views\wizard

Code

protected function build_sorts(&$form, &$form_state) {
  $sorts = array(
    'none' => t('Unsorted'),
  );

  // Check if we are allowed to sort by creation date.
  $created_column = $this
    ->getCreatedColumn();
  if ($created_column) {
    $sorts += array(
      $created_column . ':DESC' => t('Newest first'),
      $created_column . ':ASC' => t('Oldest first'),
    );
  }
  if ($available_sorts = $this
    ->getAvailableSorts()) {
    $sorts += $available_sorts;
  }
  foreach ($sorts as &$option) {
    if (is_object($option)) {
      $option = $option
        ->get();
    }
  }

  // If there is no sorts option available continue.
  if (!empty($sorts)) {
    $form['displays']['show']['sort'] = array(
      '#type' => 'select',
      '#title' => t('sorted by'),
      '#options' => $sorts,
      '#default_value' => isset($created_column) ? $created_column . ':DESC' : 'none',
    );
  }
}