You are here

public function ElasticsearchViewsEntity::buildOptionsForm in Elasticsearch Connector 8.2

Same name and namespace in other branches
  1. 8.7 modules/elasticsearch_connector_views/src/Plugin/views/field/ElasticsearchViewsEntity.php \Drupal\elasticsearch_connector_views\Plugin\views\field\ElasticsearchViewsEntity::buildOptionsForm()
  2. 8.5 modules/elasticsearch_connector_views/src/Plugin/views/field/ElasticsearchViewsEntity.php \Drupal\elasticsearch_connector_views\Plugin\views\field\ElasticsearchViewsEntity::buildOptionsForm()
  3. 8.6 modules/elasticsearch_connector_views/src/Plugin/views/field/ElasticsearchViewsEntity.php \Drupal\elasticsearch_connector_views\Plugin\views\field\ElasticsearchViewsEntity::buildOptionsForm()

Default options form that provides the label widget that all fields should have.

Overrides FieldPluginBase::buildOptionsForm

File

modules/elasticsearch_connector_views/src/Plugin/views/field/ElasticsearchViewsEntity.php, line 79

Class

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

Namespace

Drupal\elasticsearch_connector_views\Plugin\views\field

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $entity_type_id = $this
    ->getTargetEntityTypeId();
  $view_modes = array();
  $bundles = array();
  if ($entity_type_id) {
    $bundles = $this
      ->getEntityManager()
      ->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'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Display for "@bundle" bundle', $args),
      '#options' => array(
        '' => $this
          ->t('Hide'),
        'id' => $this
          ->t('Raw ID'),
        'label' => $this
          ->t('Only label'),
      ),
      '#default_value' => 'label',
    );
    if (isset($this->options['display_methods'][$bundle]['display_method'])) {
      $form['display_methods'][$bundle]['display_method']['#default_value'] = $this->options['display_methods'][$bundle]['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'] = array(
          '#type' => 'select',
          '#title' => $this
            ->t('View mode for "@bundle" bundle', $args),
          '#options' => $view_modes[$bundle],
          '#states' => array(
            'visible' => array(
              ':input[name="options[display_methods][' . $bundle . '][display_method]"]' => array(
                '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'] = array(
          '#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;
}