You are here

public function SearchApiRow::buildOptionsForm in Search API 8

Provide a form for setting options.

Overrides RowPluginBase::buildOptionsForm

File

src/Plugin/views/row/SearchApiRow.php, line 109

Class

SearchApiRow
Provides a row plugin for displaying a result as a rendered item.

Namespace

Drupal\search_api\Plugin\views\row

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  foreach ($this->index
    ->getDatasources() as $datasource_id => $datasource) {
    $datasource_label = $datasource
      ->label();
    $bundles = $datasource
      ->getBundles();
    if (!$datasource
      ->getViewModes()) {
      $form['view_modes'][$datasource_id] = [
        '#type' => 'item',
        '#title' => $this
          ->t('View mode for datasource %name', [
          '%name' => $datasource_label,
        ]),
        '#description' => $this
          ->t("This datasource doesn't have any view modes available. It is therefore not possible to display results of this datasource using this row plugin."),
      ];
      continue;
    }
    foreach ($bundles as $bundle_id => $bundle_label) {
      $title = $this
        ->t('View mode for datasource %datasource, bundle %bundle', [
        '%datasource' => $datasource_label,
        '%bundle' => $bundle_label,
      ]);
      $view_modes = $datasource
        ->getViewModes($bundle_id);
      if (!$view_modes) {
        $form['view_modes'][$datasource_id][$bundle_id] = [
          '#type' => 'item',
          '#title' => $title,
          '#description' => $this
            ->t("This bundle doesn't have any view modes available. It is therefore not possible to display results of this bundle using this row plugin."),
        ];
        continue;
      }
      $form['view_modes'][$datasource_id][$bundle_id] = [
        '#type' => 'select',
        '#options' => $view_modes,
        '#title' => $title,
        '#default_value' => key($view_modes),
      ];
      if (isset($this->options['view_modes'][$datasource_id][$bundle_id])) {
        $form['view_modes'][$datasource_id][$bundle_id]['#default_value'] = $this->options['view_modes'][$datasource_id][$bundle_id];
      }
    }
  }
}