When using an Openlayers map as the input for a required WKT geofield the default value is "GEOMETRYCOLLECTION EMPTY". This allows the field to be saved with no errors. I think a required geofield should check that it is not empty and show an error if it is left empty.

When editing the field settings if the default value of "GEOMETRYCOLLECTION EMPTY" is removed, the settings page fails to save, throwing an exception:

Exception: geoPHP could not find an adapter of type in geoPHP::load()

In the meantime I have implemented a workaround in a custom module:

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */
function mymodule_field_widget_openlayers_geofield_form_alter(&$element, &$form_state, $context) {
  // make required geofield locations require a non-empty value
  if ($element[0]['#required']) {
    $element['#element_validate'][] = 'mymodule_validate_no_empty_geofield';
  }
}

function mymodule_validate_no_empty_geofield($element, &$form_state) {
  $container_delta = $element[0];
  $item = array();
  $item['input_format'] = $container_delta['input_format']['#value'];
  $item['geom'] = drupal_array_get_nested_value($form_state['values'], $container_delta['geom']['#parents']);

  if (geofield_field_is_empty($item, $element)) {
    form_set_error($container_delta['#field_name'], 'Location is missing');
  }
}

This has only been tested on WKT inputs, and I think the geofield module should be handing these checks.

Comments

scott.whittaker created an issue.