You are here

public function GroupwiseMax::buildOptionsForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/src/Plugin/views/relationship/GroupwiseMax.php \Drupal\views\Plugin\views\relationship\GroupwiseMax::buildOptionsForm()

Provide a form to edit options for this plugin.

Overrides RelationshipPluginBase::buildOptionsForm

File

core/modules/views/src/Plugin/views/relationship/GroupwiseMax.php, line 81

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, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);

  // Get the sorts that apply to our base.
  $sorts = Views::viewsDataHelper()
    ->fetchFields($this->definition['base'], 'sort');
  $sort_options = [];
  foreach ($sorts as $sort_id => $sort) {
    $sort_options[$sort_id] = "{$sort['group']}: {$sort['title']}";
  }
  $base_table_data = Views::viewsData()
    ->get($this->definition['base']);

  // Extends the relationship's basic options, allowing the user to pick a
  // sort and an order for it.
  $form['subquery_sort'] = [
    '#type' => 'select',
    '#title' => $this
      ->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' => $this
      ->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'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Representative sort order'),
    '#description' => $this
      ->t("The ordering to use for the sort criteria selected above."),
    '#options' => [
      'ASC' => $this
        ->t('Ascending'),
      'DESC' => $this
        ->t('Descending'),
    ],
    '#default_value' => $this->options['subquery_order'],
  ];
  $form['subquery_namespace'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Subquery namespace'),
    '#description' => $this
      ->t('Advanced. Enter a namespace for the subquery used by this relationship.'),
    '#default_value' => $this->options['subquery_namespace'],
  ];

  // WIP: This stuff doesn't work yet: namespacing issues.
  // A list of suitable views to pick one as the subview.
  $views = [
    '' => '- None -',
  ];
  foreach (Views::getAllViews() as $view) {

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

      // TODO: check the field is the correct sort?
      // or let users hang themselves at this stage and check later?
      $views[$view
        ->id()] = $view
        ->id();
    }
  }
  $form['subquery_view'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Representative view'),
    '#default_value' => $this->options['subquery_view'],
    '#options' => $views,
    '#description' => $this
      ->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'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Generate subquery each time view is run'),
    '#default_value' => $this->options['subquery_regenerate'],
    '#description' => $this
      ->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.'),
  ];
}