You are here

public function SearchApiRenderedItem::buildOptionsForm in Search API 8

Provide a form to edit options for this plugin.

Parameters

array|\ArrayAccess $form: The existing form structure, passed by reference.

\Drupal\Core\Form\FormStateInterface $form_state: The current form state.

Overrides SearchApiFieldTrait::buildOptionsForm

See also

\Drupal\views\Plugin\views\ViewsPluginInterface::buildOptionsForm()

File

src/Plugin/views/field/SearchApiRenderedItem.php, line 73

Class

SearchApiRenderedItem
Handles rendering an entity in a certain view mode in Search API Views.

Namespace

Drupal\search_api\Plugin\views\field

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $no_view_mode_option = [
    '' => $this
      ->t("Don't include the rendered item."),
  ];
  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 in this field."),
      ];
      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' => $no_view_mode_option + $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];
      }
    }
  }
}