geocoder.drush.inc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. );
  12. return $items;
  13. }
  14. function geocoder_drush_backfill() {
  15. $all_entity_info = entity_get_info();
  16. foreach ($all_entity_info as $entity_type => $entity_info) {
  17. if ($entity_type == 'node') { //TODO: FIX THE LOGIC BELOW and implement for all entities
  18. if ($entity_info['fieldable']) {
  19. foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
  20. foreach (field_info_instances($entity_type, $bundle_name) as $field_name => $field_instance) {
  21. $field_info = field_info_field($field_name);
  22. if ($field_instance['widget']['type'] === 'geocoder') {
  23. $entity_load = $entity_info['load hook'];
  24. $query = db_select($entity_info['base table'])
  25. ->fields($entity_info['base table'], array($entity_info['entity keys']['id']))
  26. ->condition($entity_info['entity keys']['bundle'], $bundle_name);
  27. $results = $query->execute();
  28. while ($id = $results->fetchField()) {
  29. $entity = $entity_load($id);
  30. $langcode = field_language($entity_type, $entity, $field_name);
  31. $items = field_get_items($entity_type, $entity, $field_name, $langcode);
  32. // Check for values and if there are no values, reload the entity
  33. if ($field_info['type'] == 'geofield') {
  34. if (empty($items['wkt'])) node_save($entity); //TODO: fix for all entities
  35. }
  36. if ($field_info['type'] == 'location') {
  37. if (empty($items['latitude'])) node_save($entity); //TODO: fix for all entities
  38. }
  39. if ($field_info['type'] == 'geolocation') {
  40. if (empty($items['lat'])) node_save($entity); //TODO: fix for all entities
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }