You are here

public function SearchApiEntity::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/SearchApiEntity.php, line 112

Class

SearchApiEntity
Handles the display of entity reference fields in Search API Views.

Namespace

Drupal\search_api\Plugin\views\field

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $entity_type_id = $this
    ->getTargetEntityTypeId();
  $view_modes = [];
  $bundles = [];
  if ($entity_type_id) {
    $bundles = $this
      ->getEntityTypeBundleInfo()
      ->getBundleInfo($entity_type_id);

    // In case the field definition specifies the bundles to expect, restrict
    // the displayed bundles to those.
    $settings = $this
      ->getFieldDefinition()
      ->getSettings();
    if (!empty($settings['handler_settings']['target_bundles'])) {
      $bundles = array_intersect_key($bundles, $settings['handler_settings']['target_bundles']);
    }
    foreach ($bundles as $bundle => $info) {
      $view_modes[$bundle] = $this
        ->getEntityDisplayRepository()
        ->getViewModeOptionsByBundle($entity_type_id, $bundle);
    }
  }
  foreach ($bundles as $bundle => $info) {
    $args['@bundle'] = $info['label'];
    $form['display_methods'][$bundle]['display_method'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Display for "@bundle" bundle', $args),
      '#options' => [
        '' => $this
          ->t('Hide'),
        'id' => $this
          ->t('Raw ID'),
        'label' => $this
          ->t('Only label'),
      ],
      '#default_value' => 'label',
    ];
    $display_method = $this
      ->getDisplayMethod($bundle);
    if ($display_method !== NULL) {
      $form['display_methods'][$bundle]['display_method']['#default_value'] = $display_method;
    }
    if (!empty($view_modes[$bundle])) {
      $form['display_methods'][$bundle]['display_method']['#options']['view_mode'] = $this
        ->t('Entity view');
      if (count($view_modes[$bundle]) > 1) {
        $form['display_methods'][$bundle]['view_mode'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('View mode for "@bundle" bundle', $args),
          '#options' => $view_modes[$bundle],
          '#states' => [
            'visible' => [
              ':input[name="options[display_methods][' . $bundle . '][display_method]"]' => [
                'value' => 'view_mode',
              ],
            ],
          ],
        ];
        if (isset($this->options['display_methods'][$bundle]['view_mode'])) {
          $form['display_methods'][$bundle]['view_mode']['#default_value'] = $this->options['display_methods'][$bundle]['view_mode'];
        }
      }
      else {
        reset($view_modes[$bundle]);
        $form['display_methods'][$bundle]['view_mode'] = [
          '#type' => 'value',
          '#value' => key($view_modes[$bundle]),
        ];
      }
    }
    if (count($bundles) == 1) {
      $form['display_methods'][$bundle]['display_method']['#title'] = $this
        ->t('Display method');
      if (!empty($form['display_methods'][$bundle]['view_mode'])) {
        $form['display_methods'][$bundle]['view_mode']['#title'] = $this
          ->t('View mode');
      }
    }
  }
  $form['link_to_item']['#description'] .= ' ' . $this
    ->t('This will only take effect for entities for which only the entity label is displayed.');
  $form['link_to_item']['#weight'] = 5;
  unset($form['use_highlighting']);
}