You are here

public function GeoJson::buildOptionsForm in Views GeoJSON 8

Provide a form to edit options for this plugin.

Overrides StylePluginBase::buildOptionsForm

File

src/Plugin/views/style/GeoJson.php, line 122

Class

GeoJson
Style plugin to render view as GeoJSON code.

Namespace

Drupal\views_geojson\Plugin\views\style

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $fields = [];
  $fields_info = [];

  // Get list of fields in this view & flag available geodata fields.
  $handlers = $this->displayHandler
    ->getHandlers('field');
  $field_definition = $this->displayHandler
    ->getOption('fields');

  // Check for any fields, as the view needs them.
  if (empty($handlers)) {
    $form['error_markup'] = [
      '#value' => $this
        ->t('You need to enable at least one field before you can configure your field settings'),
      '#prefix' => '<div class="error form-item description">',
      '#suffix' => '</div>',
    ];
    return;
  }

  // Go through fields, fill $fields and $fields_info arrays.
  foreach ($this->displayHandler
    ->getHandlers('field') as $field_id => $handler) {
    $fields[$field_id] = $handler->definition['title'];
    $fields_info[$field_id]['type'] = $field_definition[$field_id]['type'];
  }

  // Default data source.
  $data_source_options = [
    'latlon' => $this
      ->t('Other: Lat/Lon Point'),
    'geofield' => $this
      ->t('Geofield'),
    'geolocation' => $this
      ->t('Geolocation'),
    'wkt' => $this
      ->t('WKT'),
  ];

  // Data Source options.
  $form['data_source'] = [
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#title' => $this
      ->t('Data Source'),
  ];
  $form['data_source']['value'] = [
    '#type' => 'select',
    '#multiple' => FALSE,
    '#title' => $this
      ->t('Map Data Sources'),
    '#description' => $this
      ->t('Choose which sources of data that the map will provide features for.'),
    '#options' => $data_source_options,
    '#default_value' => $this->options['data_source']['value'],
  ];

  // Other Lat and Lon data sources.
  if (count($fields) > 0) {
    $form['data_source']['latitude'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Latitude Field'),
      '#description' => $this
        ->t('Choose a field for Latitude.  This should be a field that is a decimal or float value.'),
      '#options' => $fields,
      '#default_value' => $this->options['data_source']['latitude'],
      '#states' => [
        'visible' => [
          ':input[name="style_options[data_source][value]"]' => [
            'value' => 'latlon',
          ],
        ],
      ],
    ];
    $form['data_source']['longitude'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Longitude Field'),
      '#description' => $this
        ->t('Choose a field for Longitude.  This should be a field that is a decimal or float value.'),
      '#options' => $fields,
      '#default_value' => $this->options['data_source']['longitude'],
      '#states' => [
        'visible' => [
          ':input[name="style_options[data_source][value]"]' => [
            'value' => 'latlon',
          ],
        ],
      ],
    ];

    // Get Geofield-type fields.
    $geofield_fields = [];
    foreach ($fields as $field_id => $field) {

      // @TODO We need to check if the field type is `geofield_default`. But
      // at the moment this information is missing from the array, due to a
      // bug with Geofield 8.x-1.x-dev. When the bug is fixed, we can add a
      // check here again.
      $geofield_fields[$field_id] = $field;
    }

    // Geofield.
    $form['data_source']['geofield'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Geofield'),
      '#description' => $this
        ->t("Choose a Geofield field. Any formatter will do; we'll access Geofield's underlying WKT format."),
      '#options' => $geofield_fields,
      '#default_value' => $this->options['data_source']['geofield'],
      '#states' => [
        'visible' => [
          ':input[name="style_options[data_source][value]"]' => [
            'value' => 'geofield',
          ],
        ],
      ],
    ];

    // Get Geofield-type fields.
    $geolocation_fields = [];
    foreach ($fields as $field_id => $field) {

      // @todo - need to limit to just geolocation fields.
      $geolocation_fields[$field_id] = $field;
    }

    // Geolocation.
    $form['data_source']['geolocation'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Geolocation'),
      '#description' => $this
        ->t("Choose a Geolocation field. Any formatter will do; we'll access Geolocation's underlying data storage."),
      '#options' => $geolocation_fields,
      '#default_value' => $this->options['data_source']['geolocation'],
      '#states' => [
        'visible' => [
          ':input[name="style_options[data_source][value]"]' => [
            'value' => 'geolocation',
          ],
        ],
      ],
    ];

    // WKT.
    $form['data_source']['wkt'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('WKT'),
      '#description' => $this
        ->t('Choose a WKT format field.'),
      '#options' => $fields,
      '#default_value' => $this->options['data_source']['wkt'],
      '#states' => [
        'visible' => [
          ':input[name="style_options[data_source][value]"]' => [
            'value' => 'wkt',
          ],
        ],
      ],
    ];
  }
  $form['data_source']['name_field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Title Field'),
    '#description' => $this
      ->t('Choose the field to appear as title on tooltips.'),
    '#options' => array_merge([
      '' => '',
    ], $fields),
    '#default_value' => $this->options['data_source']['name_field'],
  ];
  $form['data_source']['description_field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Description'),
    '#description' => $this
      ->t('Choose the field or rendering method to appear as
          description on tooltips.'),
    '#required' => FALSE,
    '#options' => array_merge([
      '' => '',
    ], $fields),
    '#default_value' => $this->options['data_source']['description_field'],
  ];

  // Attributes and variable styling description.
  $form['attributes'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Attributes and Styling'),
    '#description' => $this
      ->t('Attributes are field data attached to each feature.  This can be used with styling to create Variable styling.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  ];
  $form['jsonp_prefix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('JSONP prefix'),
    '#default_value' => $this->options['jsonp_prefix'],
    '#description' => $this
      ->t('If used the JSON output will be enclosed with parentheses and prefixed by this label, as in the JSONP format.'),
  ];

  // Make array of attributes.
  $variable_fields = [];

  // Add name and description.
  if (!empty($this->options['data_source']['name_field'])) {
    $variable_fields['name'] = '${name}';
  }
  if (!empty($this->options['data_source']['description_field'])) {
    $variable_fields['description'] = '${description}';
  }

  // Go through fields again to ID variable fields.
  // TODO: is it necessary to call getHandlers twice or can we reuse data from
  // $fields?
  foreach ($this->displayHandler
    ->getHandlers('field') as $field => $handler) {
    if ($field !== $this->options['data_source']['name_field'] && $field !== $this->options['data_source']['description_field']) {
      $variable_fields[$field] = '${' . $field . '}';
    }
  }
  $markup = $this
    ->t('Fields added to this view will be attached to their respective feature, (point, line, polygon,) as attributes.
      These attributes can then be used to add variable styling to your themes. This is accomplished by using the %syntax
      syntax in the values for a style.  The following is a list of formatted variables that are currently available;
      these can be placed right in the style interface.', [
    '%syntax' => '${field_name}',
  ]);
  $form['attributes']['styling'][''] = [
    '#type' => 'html_tag',
    '#tag' => 'p',
    '#attributes' => [
      'class' => [
        'description',
      ],
    ],
    '#markup' => $markup,
  ];
  $form['attributes']['styling'][] = [
    '#theme' => 'item_list',
    '#items' => $variable_fields,
    '#attributes' => [
      'class' => [
        'description',
      ],
    ],
  ];
  $form['attributes']['styling'][''] = [
    '#type' => 'html_tag',
    '#tag' => 'p',
    '#attributes' => [
      'class' => [
        'description',
      ],
    ],
    '#value' => $this
      ->t('Please note that this does not apply to Grouped Displays.'),
  ];
}