geofield.widgets.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * @file
  4. * Provides field widget hooks for geofield module.
  5. */
  6. /**
  7. * Geofield Input format - auto-discover.
  8. */
  9. define('GEOFIELD_INPUT_AUTO_DISCOVER', 'auto-discover');
  10. /**
  11. * Geofield Input format - WKT.
  12. */
  13. define('GEOFIELD_INPUT_WKT', 'wkt');
  14. /**
  15. * Geofield Input format - GeoJSON.
  16. */
  17. define('GEOFIELD_INPUT_GEOJSON', 'json');
  18. /**
  19. * Geofield Input format - lat/lon.
  20. */
  21. define('GEOFIELD_INPUT_LAT_LON', 'lat/lon');
  22. /**
  23. * Geofield Input format - bounds.
  24. */
  25. define('GEOFIELD_INPUT_BOUNDS', 'bounds');
  26. // If we have the Openlayers module installed, include their widget code.
  27. if (module_exists('openlayers')) {
  28. module_load_include('inc', 'geofield', 'geofield.widgets.openlayers');
  29. }
  30. /**
  31. * Implements hook_field_widget_info().
  32. */
  33. function geofield_field_widget_info() {
  34. $widgets = array();
  35. // OpenLayers dependant widget
  36. if (module_exists('openlayers')) {
  37. $widgets['geofield_openlayers'] = array(
  38. 'label' => t('Openlayers Map'),
  39. 'field types' => array('geofield'),
  40. );
  41. }
  42. $widgets['geofield_wkt'] = array(
  43. 'label' => t('Well Known Text (WKT)'),
  44. 'field types' => array('geofield'),
  45. );
  46. $widgets['geofield_geojson'] = array(
  47. 'label' => t('GeoJSON'),
  48. 'field types' => array('geofield'),
  49. );
  50. $widgets['geofield_latlon'] = array(
  51. 'label' => t('Latitude / Longitude'),
  52. 'field types' => array('geofield'),
  53. );
  54. $widgets['geofield_bounds'] = array(
  55. 'label' => t('Bounds'),
  56. 'field types' => array('geofield'),
  57. );
  58. return $widgets;
  59. }
  60. /**
  61. * Implements hook_field_widget_settings_form().
  62. */
  63. function geofield_field_widget_settings_form($field, $instance) {
  64. $widget = $instance['widget'];
  65. $settings = $widget['settings'];
  66. $form = array();
  67. if ($widget['type'] == 'geofield_latlon') {
  68. $form['html5_geolocation'] = array(
  69. '#type' => 'checkbox',
  70. '#title' => 'Use HTML5 Geolocation to set default values',
  71. '#default_value' => (!empty($settings['html5_geolocation'])) ? $settings['html5_geolocation'] : FALSE,
  72. );
  73. }
  74. return $form;
  75. }
  76. /**
  77. * Implements hook_field_widget_form().
  78. */
  79. function geofield_field_widget_form(&$form, &$form_state, $field, $instance,
  80. $langcode, $items, $delta, $base) {
  81. $element = $base;
  82. $widget = $instance['widget'];
  83. $settings = $widget['settings'];
  84. // $element['input_format'] is not a db field, but we use it determine how to parse/calculate values in our widget.
  85. $element['input_format'] = array(
  86. '#type' => 'value',
  87. '#attributes' => array('class' => array('geofield_input_format')),
  88. '#value' => GEOFIELD_INPUT_AUTO_DISCOVER,
  89. );
  90. // All of our widgets are keyed to 'geom,' we calculate proper field values in geofield_field_presave().
  91. switch ($widget['type']) {
  92. case 'geofield_wkt':
  93. $default_value = '';
  94. if (!empty($items[$delta]['geom'])) {
  95. geophp_load();
  96. $geometry = geoPHP::load($items[$delta]['geom']);
  97. $default_value = $geometry->out('wkt');
  98. }
  99. $element['geom'] = array(
  100. '#type' => 'textarea',
  101. '#title' => check_plain($instance['label']),
  102. '#description' => t($instance['description']),
  103. '#default_value' => $default_value,
  104. '#required' => $instance['required'],
  105. );
  106. $element['input_format']['#value'] = GEOFIELD_INPUT_WKT;
  107. break;
  108. case 'geofield_geojson':
  109. $default_value = '';
  110. if (!empty($items[$delta]['geom'])) {
  111. geophp_load();
  112. $geometry = geoPHP::load($items[$delta]['geom']);
  113. $default_value = $geometry->out('json');
  114. }
  115. $element['geom'] = array(
  116. '#type' => 'textarea',
  117. '#title' => check_plain($instance['label']),
  118. '#description' => t($instance['description']),
  119. '#default_value' => $default_value,
  120. '#required' => $instance['required'],
  121. );
  122. $element['input_format']['#value'] = GEOFIELD_INPUT_GEOJSON;
  123. break;
  124. case 'geofield_latlon':
  125. $latlon_value = array(
  126. 'lat' => '',
  127. 'lon' => '',
  128. );
  129. if (isset($items[$delta]['lat'])) {
  130. $latlon_value['lat'] = floatval($items[$delta]['lat']);
  131. }
  132. if (isset($items[$delta]['lon'])) {
  133. $latlon_value['lon'] = floatval($items[$delta]['lon']);
  134. }
  135. $element['geom'] = array(
  136. '#type' => 'geofield_latlon',
  137. '#title' => check_plain($instance['label']),
  138. '#description' => t($instance['description']),
  139. '#default_value' => $latlon_value,
  140. '#required' => $instance['required'],
  141. '#geolocation' => (!empty($instance['widget']['settings']['html5_geolocation'])) ? $instance['widget']['settings']['html5_geolocation'] : FALSE,
  142. );
  143. $element['input_format']['#value'] = GEOFIELD_INPUT_LAT_LON;
  144. break;
  145. case 'geofield_bounds':
  146. $bounds_value = array(
  147. 'top' => '',
  148. 'right' => '',
  149. 'bottom' => '',
  150. 'left' => '',
  151. );
  152. if (isset($items[$delta]['top'])) {
  153. $bounds_value['top'] = floatval($items[$delta]['top']);
  154. }
  155. if (isset($items[$delta]['right'])) {
  156. $bounds_value['right'] = floatval($items[$delta]['right']);
  157. }
  158. if (isset($items[$delta]['bottom'])) {
  159. $bounds_value['bottom'] = floatval($items[$delta]['bottom']);
  160. }
  161. if (isset($items[$delta]['left'])) {
  162. $bounds_value['left'] = floatval($items[$delta]['left']);
  163. }
  164. $element['geom'] = array(
  165. '#type' => 'geofield_bounds',
  166. '#title' => check_plain($instance['label']),
  167. '#description' => t($instance['description']),
  168. '#default_value' => $bounds_value,
  169. '#required' => $instance['required'],
  170. );
  171. $element['input_format']['#value'] = GEOFIELD_INPUT_BOUNDS;
  172. break;
  173. }
  174. return $element;
  175. }