geocoder.drush.inc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Implementation of hook_drush_command().
  4. */
  5. function geocoder_drush_command() {
  6. $items = array();
  7. // the key in the $items array is the name of the command.
  8. $items['geocoder-backfill'] = array(
  9. 'callback' => 'geocoder_drush_backfill',
  10. 'description' => "Geocodes all nodes that have a geocoder widget but no geodata.",
  11. 'options' => array(
  12. 'force' => 'Force the geocode to run, even if there is already geodata',
  13. ),
  14. );
  15. return $items;
  16. }
  17. function geocoder_drush_backfill() {
  18. $force_reload = drush_get_option('force');
  19. $all_entity_info = entity_get_info();
  20. foreach ($all_entity_info as $entity_type => $entity_info) {
  21. if ($entity_type == 'node') { //TODO: FIX THE LOGIC BELOW and implement for all entities
  22. if ($entity_info['fieldable']) {
  23. foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
  24. foreach (field_info_instances($entity_type, $bundle_name) as $field_name => $field_instance) {
  25. $field_info = field_info_field($field_name);
  26. if ($field_instance['widget']['type'] === 'geocoder') {
  27. $entity_load = $entity_info['load hook'];
  28. $query = db_select($entity_info['base table'])
  29. ->fields($entity_info['base table'], array($entity_info['entity keys']['id']))
  30. ->condition($entity_info['entity keys']['bundle'], $bundle_name);
  31. $results = $query->execute();
  32. while ($id = $results->fetchField()) {
  33. $entity = $entity_load($id);
  34. $langcode = field_language($entity_type, $entity, $field_name);
  35. $items = field_get_items($entity_type, $entity, $field_name, $langcode);
  36. if ($force_reload) {
  37. $entity->original = array();
  38. }
  39. // Check for values and if there are no values, reload the entity
  40. if ($field_info['type'] == 'geofield') {
  41. if ($force_reload || (empty($items['wkt']) && empty($items['geom']))) node_save($entity); //TODO: fix for all entities
  42. }
  43. if ($field_info['type'] == 'location') {
  44. if ($force_reload || empty($items['latitude'])) node_save($entity); //TODO: fix for all entities
  45. }
  46. if ($field_info['type'] == 'geolocation') {
  47. if ($force_reload || empty($items['lat'])) node_save($entity); //TODO: fix for all entities
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }