Skip to content

Instantly share code, notes, and snippets.

@balos1
Last active February 29, 2016 14:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balos1/cc61f11efe2ae4230cda to your computer and use it in GitHub Desktop.
Save balos1/cc61f11efe2ae4230cda to your computer and use it in GitHub Desktop.
Drupal Geofield Module Customizing
name = GeoField Customizer
description = A module for editing the Geofield module
core = 7.x
package = Other
dependencies[] = geofield
dependencies[] = devel
files[] = geofield_customizer.module
<?php
/*
* Set module weight lower then geofield module
*
*/
function geofield_customizer_search_install() {
$weight = db_select('system', 's')
->fields('s', array('weight'))
->condition('name', '[geofield]', '=')
->execute()
->fetchField();
db_update('system')
->fields(array('weight' => $weight -1))
->condition('name', '[geofield_customizer]', '=')
->execute();
};
?>
<?php
/*
*
* This module edits the geofield module
* It mostly edits the visual apsects rather then
* the actual functionality
*
*/
function geofield_customizer_form_alter(&$form, &$form_state, $form_id) {
dpm($form_state['view']->name);
dpm($form_state['view']->current_display);
if (($form_id == 'views_exposed_form') && ($form['#id'] == 'views-exposed-form-VIEW-NAME-DISPLAY-NAME')) {
}
};
function geofield_customizer_element_info_alter(&$type) {
if (isset($type['geofield_proximity']['#process'])) {
$type['geofield_proximity']['#process'][] = 'geofield_customizer_proximity_element_process';
}
};
/*
* Alter geofield views exposed form
* create distance select box
* hide unit dropdown
*/
function geofield_customizer_proximity_element_process($element, &$form_state, $form) {
if ($form_state['view']->name == 'VIEW-NAME' && $form_state['view']->current_display == 'DISPLAY-NAME'){
//dpm($element);
$element['distance']['#type'] = 'select';
// Add the dropdown
$element['distance']['#options'] = array(
5 => t('5 Miles'),
25 => t('25 Miles'),
50 => t('50 Miles'),
75 => t('75 Miles'),
100 => t('100 Miles'),
250 => t('250 Miles'),
100000 => t('Any Distance')
);
// Set distance default value
$element['distance']['#default_value'] = 10;
// Set unit deafult to miles
$element['unit']['#default_value'] = GEOFIELD_MILES;
//Hide unit dropdown
$element['unit']['#access'] = FALSE;
return $element;
}
return $element;
};
?>

Geofield Customizer Module

This is an exmaple of custom Drupal module that modifies portions of the Geofield module.

Current Features

  1. Alter Geofield exposed filter
    • Change distance textfield to select box
    • Default unit to miles and hide the unit selector

How to Use

  1. Download and place all files in your modules folder
  2. Enable the module
  3. Go to the page with your geofield exposed filter
  4. Take note of the view name and display name printed in the message area
  5. Open the geofield_customizer.module file
  6. Insert your view name and display name to views-exposed-form-VIEW-NAME-DISPLAY-NAME'
  7. Insert your view name and display name to if ($form_state['view']->name == 'VIEW-NAME' && $form_state['view']->current_display == 'DISPLAY-NAME')
  8. Save and reupload the geofield_customizer.module file
  9. Flush all caches on your drupal site
  10. Reload your page with the geofield exposed filter on it

Special thanks to cmonnow on drupal.org for the initial code.

@annstringer
Copy link

Thanks for this. Saved me tons of time and you just taught me how to write a custom module.

@dasginganinja
Copy link

Ahh this is just what I was looking for. Thanks!

@VangelisP
Copy link

Many thanks! It's really what I was looking for!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment