You are here

public function RateWidgetField::buildOptionsForm in Rate 8.2

Provide the options form.

Overrides FieldPluginBase::buildOptionsForm

File

src/Plugin/views/field/RateWidgetField.php, line 44

Class

RateWidgetField
Views field handler for the rate widget.

Namespace

Drupal\rate\Plugin\views\field

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {

  // Let the user select the ID to vote on and avoid relationship nightmare.
  $field_list = [];
  $field_handlers = $this->view
    ->getHandlers('field');
  foreach ($field_handlers as $field_name) {
    if ($field_name['entity_type'] && $field_name['plugin_id'] != 'rate_widget_field') {
      $field_list[$field_name['id']] = $field_name['label'];
    }
  }
  $form['id_column'] = [
    '#title' => $this
      ->t('Which field column holds the entity ID?'),
    '#type' => 'select',
    '#default_value' => $this->options['id_column'],
    '#options' => $field_list,
    '#description' => $this
      ->t('Enable and hide the ID field of the entity, which has a Rate widget attached to it.'),
  ];

  // Handle multiple widgets per entity.
  $widgets = \Drupal::service('entity_type.manager')
    ->getStorage('rate_widget')
    ->loadMultiple();
  $entity_types = [];
  $widget_count = 0;
  foreach ($widgets as $id => $widget) {
    $widget_entities = $widget
      ->get('entity_types');
    if (count($widget_entities > 0)) {
      foreach ($widget_entities as $entity) {
        $entity = str_replace('.', ':', $entity);
        $entity_types[$entity][$id] = $widget
          ->label();
        if (count($entity_types[$entity]) > $widget_count) {
          $widget_count = count($entity_types[$entity]);
        }
      }
    }
  }

  // Let the user select the widget to use in this view field.
  if ($widget_count > 1) {
    $form['widgets'] = [
      '#type' => 'table',
      '#caption' => $this
        ->t('<strong>Some entities have multiple widgets attached - select the ones to be shown in this field.<strong>'),
      '#header' => [
        'Entity',
        'Widget',
      ],
    ];
    foreach ($entity_types as $entity => $widgets) {
      if (count($widgets) > 1) {
        $form['widgets'][$entity]['entity'] = [
          '#type' => 'item',
          '#markup' => $entity,
        ];
        $form['widgets'][$entity]['widget'] = [
          '#type' => 'select',
          '#options' => $widgets,
          '#default_value' => $this->options['widgets'][$entity]['widget'],
        ];
      }
    }
  }
  else {
    $form['widgets'] = [];
  }

  // Select how to display the widget.
  $widget_display_options = [
    'full' => $this
      ->t('Full'),
    'readonly' => $this
      ->t('Read only'),
    'summary' => $this
      ->t('Result summary'),
  ];
  $form['widget_display'] = [
    '#title' => $this
      ->t('Show widget'),
    '#type' => 'select',
    '#default_value' => $this->options['widget_display'],
    '#options' => $widget_display_options,
    '#default_value' => $this->options['widget_display'],
  ];

  // Override rate widget display settings.
  $form['display_overrides'] = [
    '#title' => $this
      ->t('Override rate widget display options'),
    '#type' => 'checkboxes',
    '#options' => [
      'hide_label' => $this
        ->t('Hide label'),
      'hide_description' => $this
        ->t('Hide description'),
      'hide_summary' => $this
        ->t('Hide summary'),
    ],
    '#default_value' => $this->options['display_overrides'],
    '#description' => $this
      ->t('Unchecking all options will show the rate widget as configured.'),
  ];
  parent::buildOptionsForm($form, $form_state);
}