Using geofield with Views and exposed filters to the user to search for things on the map within a certain radius of a particular location.

I only require miles and kilometres as units of measurement, not: Yards, Feet, Nautical Miles. Any way to omit these?

They are defined in the module geofield's file, geofield.elements.inc code:

 * Returns options for radius of the Earth.

function geofield_radius_options() {
  return array(
    GEOFIELD_KILOMETERS => t('Kilometers'),
    GEOFIELD_METERS => t('Meters'),
    GEOFIELD_MILES => t('Miles'),
    GEOFIELD_YARDS => t('Yards'),
    GEOFIELD_FEET => t('Feet'),
    GEOFIELD_NAUTICAL_MILES => t('Nautical Miles'),

But I don't want to hack this and remove them.

The approach I am expecting to use would be to implement hook_form_alter in my own module and remove the options from there.

But is there a better way?


Another approach is simply using CSS.

The select list is built like this:

  <option value="6371">Kilometres</option>
  <option value="6371000">Metres</option>
  <option value="3959">Miles</option>
  <option value="6975175">Yards</option>
  <option value="20925525">Feet</option>
  <option value="3444">Nautical Miles</option>

You could hide the undesired values with the following style (in this case only "kilometres" and "miles" remain):

  display: none;

If you're only using one value, it may look a bit silly to have a select box of it. You could disguise it as regular text though. Make sure the font is identical to the rest, remove the border, set a width to the container of the select list and hide everything seeping out of it, whilst making the select list a tad longer (thus the dropdown arrow gets hidden by the overflow):

    width: 90px;
    overflow: hidden;

    width: 120px;
    border: 0;
    cursor: text;

Alternatively (Bootstrap 3):

.form-item-field-geofield-distance-unit select{
    border: 0 !important;
    box-shadow: none;
    cursor: text;
    -moz-appearance: none;
    -webkit-appearance: none;
    appearance: none;

Adjust the margins or paddings if necessary…

  • +1 Neat answer - that hiding of options could be useful in other areas – therobyouknow Aug 10 '15 at 21:54

This isn't really in the form, via hook_form_alter(). It's an element of a form component, so you really need to implement hook_element_info_alter(), and add a "process" callback to the stack.

In that process callback you can then update the options:

 * Implements hook_element_info_alter().
function YOURTHEMEORMODULE_element_info_alter(&$type) {
  $type['geofield_proximity']['#process'][] = '_alter_proximity';

function _alter_proximity($element, &$form_state, $form) {
  //Create dropdown for units
  $reduced_options = array(
    GEOFIELD_MILES => 'Miles',
    GEOFIELD_KILOMETERS => 'Kilometers'
  $element['unit'] = array(
    '#type' => 'select',
    '#options' => $reduced_options,
    '#title' => t('Unit'),
    '#default_value' => !empty($element['#default_value']['unit']) ? $element['#default_value']['unit'] : GEOFIELD_KILOMETERS,
    '#title_display' => 'invisible',
  return $element;

My own answer (incomplete so far - will update when I've figured it out (feel free to chip in ;) ) but "on the right track" I think. Incomplete because I haven't figured out how to access the menu option elements from the hook module, Devel module dpm() is not helpful at directing me as to how to 'get at' the menu drop down form options in the $form array.)

I have started to write this hook override - which I have confirm is called in my custom module cgformutils in file cgformutils.module :-

 * Implements hook_form_views_exposed_form_alter().
function cgformutils_form_views_exposed_form_alter(&$form, &$form_state) {
  if($form['#id'] == 'views-exposed-form-locally-page') 

  // menu modifying code to go here:

  // I need to figure out what to put here, 
  // dpm() is not helpful at directing me as to how
  // to 'get at' the menu drop down form options in the $form array
  // in order to remove some - php unset them

  return $form;  

Here's an annotated screen shot for my explanation as to why Devel module dpm() function is not helping me work out how to access the menu drop down options from the $form array:

enter image description here

  • Other appoaches might be the better exposed filters module - though out of the box this doesn't provide what I need, though it does provide an API - but if I have to write code I might as well continue with my approach to write a hook. Another option is the views hacks module. – therobyouknow Jul 25 '13 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.