You are here

public function LeafletMarker::buildOptionsForm in Leaflet 2.0.x

Same name and namespace in other branches
  1. 8 modules/leaflet_views/src/Plugin/views/row/LeafletMarker.php \Drupal\leaflet_views\Plugin\views\row\LeafletMarker::buildOptionsForm()
  2. 2.1.x modules/leaflet_views/src/Plugin/views/row/LeafletMarker.php \Drupal\leaflet_views\Plugin\views\row\LeafletMarker::buildOptionsForm()

Provide a form for setting options.

Overrides RowPluginBase::buildOptionsForm

File

modules/leaflet_views/src/Plugin/views/row/LeafletMarker.php, line 203

Class

LeafletMarker
Plugin which formats a row as a leaflet marker.

Namespace

Drupal\leaflet_views\Plugin\views\row

Code

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

  // Get a list of fields and a sublist of geo data fields in this view.
  // @TODO use $fields = $this->displayHandler->getFieldLabels();
  $fields = [];
  $fields_geo_data = [];

  /* @var \Drupal\views\Plugin\views\ViewsHandlerInterface $handler */
  foreach ($this->displayHandler
    ->getHandlers('field') as $field_id => $handler) {
    $label = $handler
      ->adminLabel() ?: $field_id;
    $fields[$field_id] = $label;
    if (is_a($handler, 'Drupal\\views\\Plugin\\views\\field\\EntityField')) {

      /* @var \Drupal\views\Plugin\views\field\EntityField $handler */
      $field_storage_definitions = $this->entityFieldManager
        ->getFieldStorageDefinitions($handler
        ->getEntityType());
      $field_storage_definition = $field_storage_definitions[$handler->definition['field_name']];
      $type = $field_storage_definition
        ->getType();
      $definition = $this->fieldTypeManager
        ->getDefinition($type);
      if (is_a($definition['class'], '\\Drupal\\geofield\\Plugin\\Field\\FieldType\\GeofieldItem', TRUE)) {
        $fields_geo_data[$field_id] = $label;
      }
    }
  }

  // Check whether we have a geo data field we can work with.
  if (!count($fields_geo_data)) {
    $form['error'] = [
      '#markup' => $this
        ->t('Please add at least one geofield to the view.'),
    ];
    return;
  }

  // Map preset.
  $form['data_source'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Data Source'),
    '#description' => $this
      ->t('Which field contains geodata?'),
    '#options' => $fields_geo_data,
    '#default_value' => $this->options['data_source'],
    '#required' => TRUE,
  ];

  // Name field.
  $form['name_field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Title Field'),
    '#description' => $this
      ->t('Choose the field which will appear as a title on tooltips.'),
    '#options' => $fields,
    '#default_value' => $this->options['name_field'],
    '#empty_value' => '',
  ];
  $desc_options = $fields;

  // Add an option to render the entire entity using a view mode.
  if ($this->entityTypeId) {
    $desc_options += [
      '#rendered_entity' => '<' . $this
        ->t('Rendered @entity entity', [
        '@entity' => $this->entityTypeId,
      ]) . '>',
    ];
  }
  $form['description_field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Description Field'),
    '#description' => $this
      ->t('Choose the field or rendering method which will appear as a description on tooltips or popups.'),
    '#options' => $desc_options,
    '#default_value' => $this->options['description_field'],
    '#empty_value' => '',
  ];
  if ($this->entityTypeId) {

    // Get the human readable labels for the entity view modes.
    $view_mode_options = [];
    foreach ($this->entityDisplay
      ->getViewModes($this->entityTypeId) as $key => $view_mode) {
      $view_mode_options[$key] = $view_mode['label'];
    }

    // The View Mode drop-down is visible conditional on "#rendered_entity"
    // being selected in the Description drop-down above.
    $form['view_mode'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('View mode'),
      '#description' => $this
        ->t('View modes are ways of displaying entities.'),
      '#options' => $view_mode_options,
      '#default_value' => !empty($this->options['view_mode']) ? $this->options['view_mode'] : 'full',
      '#states' => [
        'visible' => [
          ':input[name="row_options[description_field]"]' => [
            'value' => '#rendered_entity',
          ],
        ],
      ],
    ];
  }
}