You are here

function openlayers_cck_widget in Openlayers 6

Same name and namespace in other branches
  1. 6.2 modules/openlayers_cck/openlayers_cck.module \openlayers_cck_widget()

Implementation of hook_widget().

File

modules/openlayers_cck/openlayers_cck.module, line 303
This file holds the main Drupal hook functions and private functions for the openlayers_cck module.

Code

function openlayers_cck_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  $element = array();
  if ($field['type'] == 'geo') {

    // If we are using this widget for a geo field...
    $info = module_invoke_all('gis_input_info');
    $info = $info[$field['widget']['type']];
    foreach ($items as $key => $item) {

      // Convert from the storage format (wkb) to the element's expected format (wkt).
      if ($info['gis input']) {
        $data = geo_wkb_get_data($item['wkb'], $info['gis input']);
        $items[$key] = $data['value'];
      }
    }
    $element['geo'] = array_merge($info['element'], array(
      '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
      '#gis_type' => $field['geo_type'],
      '#gis_input' => $info['gis input'],
      // The format expected by the element.
      '#gis_output' => 'wkt',
      // The format expected by geo_field's storage.
      '#delta' => $delta,
      '#title' => $field['widget']['label'],
      '#required' => $field['required'],
      '#description' => $field['widget']['description'],
    ));
    if (!isset($element['geo']['#after_build'])) {
      $element['geo']['#after_build'] = array();
    }
  }
  switch ($field['widget']['type']) {
    case 'openlayers_wkt_widget':
      $element['#type'] = 'openlayers_wkt_widget';
      $element['#default_value'] = $items;
      break;
  }
  return $element;
}