You are here

function leaflet_views_plugin_style::options_form in Leaflet 7

Options form.

Overrides views_plugin_style::options_form

File

leaflet_views/leaflet_views_plugin_style.inc, line 118
Extension of the Views Plugin Style for Leaflet Map Adapted from the GeoField Map views module and the OpenLayers Views module.

Class

leaflet_views_plugin_style
@file Extension of the Views Plugin Style for Leaflet Map Adapted from the GeoField Map views module and the OpenLayers Views module.

Code

function options_form(&$form, &$form_state) {
  parent::options_form($form, $form_state);

  // Get list of fields in this view & flag available geodata fields:
  $handlers = $this->display->handler
    ->get_handlers('field');
  $fields = array();
  $fields_data = array();
  foreach ($handlers as $field_id => $handler) {
    $fields[$field_id] = $handler
      ->ui_name();
    if (!empty($handler->field_info['type']) && $handler->field_info['type'] == 'geofield') {
      $fields_data[$field_id] = $handler
        ->ui_name();
    }
  }

  // Check whether we have a geofield we can work with:
  if (!count($fields_data)) {
    $form['error'] = array(
      '#markup' => t('Please add at least one geofield to the view'),
    );
    return;
  }

  // Get available entity types for selection.
  $default_type = empty($this->options['entity_type']) ? '' : $this->options['entity_type'];
  $entity_types = array(
    '',
  );
  foreach (entity_get_info() as $key => $info) {
    $entity_types[$key] = $info['label'];
    if ($this->view->base_table == $info['base table']) {

      // If this entity type matches the view base table, use it.
      $default_type = $key;
    }
  }

  // Entity type selection.
  $form['entity_type'] = array(
    '#type' => 'select',
    '#title' => t('Entity Type'),
    '#description' => t('Which type of entity is this view based on?'),
    '#options' => $entity_types,
    '#default_value' => $default_type,
    '#required' => TRUE,
  );

  // Only allow users to choose the entity type if it cannot be detected.
  if (!empty($default_type)) {
    $form['entity_type']['#access'] = FALSE;
  }

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

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

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

  // Taken from openlayers_views_style_data::options_form().
  // Create view mode options:
  if (isset($this->entity_type)) {

    // Get the labels (human readable) of the view modes:
    $view_mode_options = array();
    foreach ($this->entity_info['view modes'] as $key => $view_mode) {
      $view_mode_options[$key] = $view_mode['label'];
    }

    // Output the form:
    $form['view_mode'] = array(
      '#type' => 'select',
      '#title' => t('View mode'),
      '#description' => 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' => array(
        'visible' => array(
          ':input[name="style_options[description_field]"]' => array(
            'value' => '#rendered_entity',
          ),
        ),
      ),
    );
  }

  // Choose a map preset:
  $map_options = array();
  foreach (leaflet_map_get_info() as $key => $map) {
    $map_options[$key] = t('@label', array(
      '@label' => $map['label'],
    ));
  }
  $form['map'] = array(
    '#title' => t('Map'),
    '#type' => 'select',
    '#options' => $map_options,
    '#default_value' => $this->options['map'] ? $this->options['map'] : '',
    '#required' => TRUE,
  );
  $form['height'] = array(
    '#title' => t('Map height'),
    '#type' => 'textfield',
    '#field_suffix' => t('px'),
    '#size' => 4,
    '#default_value' => $this->options['height'],
    '#required' => FALSE,
  );
  $form['hide_empty'] = array(
    '#title' => t('Hide empty'),
    '#type' => 'checkbox',
    '#description' => t('Hide the Leaflet map if there are no results to display.'),
    '#default_value' => isset($this->options['hide_empty']) ? $this->options['hide_empty'] : TRUE,
  );
  $form['zoom'] = leaflet_form_elements('zoom', $this->options);
  $form['icon'] = leaflet_form_elements('icon', $this->options, array(
    'path' => 'style_options',
    'fields' => $fields,
  ));
  $form['vector_display'] = leaflet_form_elements('vector_display', $this->options, array(
    'path' => 'style_options',
  ));
  $form['tokens'] = leaflet_form_elements('tokens', $this->options, array(
    'weight' => 998,
    'entity_type' => $this->entity_type,
  ));
}