You are here

public function GroupwiseMax::buildOptionsForm in Views (for Drupal 7) 8.3

Extends the relationship's basic options, allowing the user to pick a sort and an order for it.

Overrides RelationshipPluginBase::buildOptionsForm

File

lib/Drupal/views/Plugin/views/relationship/GroupwiseMax.php, line 88
Definition of Drupal\views\Plugin\views\relationship\GroupwiseMax.

Class

GroupwiseMax
Relationship handler that allows a groupwise maximum of the linked in table. For a definition, see: http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.... In lay terms, instead of joining to get all matching records in the…

Namespace

Drupal\views\Plugin\views\relationship

Code

public function buildOptionsForm(&$form, &$form_state) {
  parent::buildOptionsForm($form, $form_state);

  // Get the sorts that apply to our base.
  $sorts = views_fetch_fields($this->definition['base'], 'sort');
  foreach ($sorts as $sort_id => $sort) {
    $sort_options[$sort_id] = "{$sort['group']}: {$sort['title']}";
  }
  $base_table_data = views_fetch_data($this->definition['base']);
  $form['subquery_sort'] = array(
    '#type' => 'select',
    '#title' => t('Representative sort criteria'),
    // Provide the base field as sane default sort option.
    '#default_value' => !empty($this->options['subquery_sort']) ? $this->options['subquery_sort'] : $this->definition['base'] . '.' . $base_table_data['table']['base']['field'],
    '#options' => $sort_options,
    '#description' => t("The sort criteria is applied to the data brought in by the relationship to determine how a representative item is obtained for each row. For example, to show the most recent node for each user, pick 'Content: Updated date'."),
  );
  $form['subquery_order'] = array(
    '#type' => 'radios',
    '#title' => t('Representative sort order'),
    '#description' => t("The ordering to use for the sort criteria selected above."),
    '#options' => array(
      'ASC' => t('Ascending'),
      'DESC' => t('Descending'),
    ),
    '#default_value' => $this->options['subquery_order'],
  );
  $form['subquery_namespace'] = array(
    '#type' => 'textfield',
    '#title' => t('Subquery namespace'),
    '#description' => t('Advanced. Enter a namespace for the subquery used by this relationship.'),
    '#default_value' => $this->options['subquery_namespace'],
  );

  // WIP: This stuff doens't work yet: namespacing issues.
  // A list of suitable views to pick one as the subview.
  $views = array(
    '' => '<none>',
  );
  $all_views = views_get_all_views();
  foreach ($all_views as $view) {

    // Only get views that are suitable:
    // - base must the base that our relationship joins towards
    // - must have fields.
    if ($view->base_table == $this->definition['base'] && !empty($view->display['default']['display_options']['fields'])) {

      // TODO: check the field is the correct sort?
      // or let users hang themselves at this stage and check later?
      if ($view->type == 'Default') {
        $views[t('Default Views')][$view->storage->name] = $view->storage->name;
      }
      else {
        $views[t('Existing Views')][$view->storage->name] = $view->storage->name;
      }
    }
  }
  $form['subquery_view'] = array(
    '#type' => 'select',
    '#title' => t('Representative view'),
    '#default_value' => $this->options['subquery_view'],
    '#options' => $views,
    '#description' => t('Advanced. Use another view to generate the relationship subquery. This allows you to use filtering and more than one sort. If you pick a view here, the sort options above are ignored. Your view must have the ID of its base as its only field, and should have some kind of sorting.'),
  );
  $form['subquery_regenerate'] = array(
    '#type' => 'checkbox',
    '#title' => t('Generate subquery each time view is run.'),
    '#default_value' => $this->options['subquery_regenerate'],
    '#description' => t('Will re-generate the subquery for this relationship every time the view is run, instead of only when these options are saved. Use for testing if you are making changes elsewhere. WARNING: seriously impairs performance.'),
  );
}