synonyms.views.inc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * @file
  4. * Views integration of Synonyms module.
  5. */
  6. /**
  7. * Implements hook_views_data().
  8. */
  9. function synonyms_views_data() {
  10. $data = array();
  11. foreach (entity_get_info() as $entity_type => $entity_info) {
  12. $wrapper = entity_metadata_wrapper($entity_type);
  13. $property_info = $wrapper->getPropertyInfo();
  14. if (isset($property_info['synonyms']) && isset($entity_info['base table']) && $entity_info['base table']) {
  15. $data[$entity_info['base table']]['synonyms'] = array(
  16. 'title' => t('All synonyms'),
  17. 'help' => t('All synonyms of @entity_type', array(
  18. '@entity_type' => $entity_info['label'],
  19. )),
  20. 'field' => array(
  21. 'handler' => 'synonyms_views_handler_field_synonyms',
  22. 'real field' => $entity_info['entity keys']['id'],
  23. 'click sortable' => FALSE,
  24. 'synonyms entity type' => $entity_type,
  25. ),
  26. );
  27. }
  28. }
  29. return $data;
  30. }
  31. /**
  32. * Implements hook_views_plugins_alter().
  33. */
  34. function synonyms_views_plugins_alter(&$plugins) {
  35. if (module_exists('taxonomy')) {
  36. // Replace default taxonomy term argument validator with our extended
  37. // version, which can also handle a term synonym as an argument.
  38. $plugins['argument validator']['taxonomy_term']['handler'] = 'synonyms_views_plugin_argument_validate_taxonomy_term';
  39. }
  40. }
  41. /**
  42. * Implements hook_field_views_data_alter().
  43. */
  44. function synonyms_field_views_data_alter(&$result, $field, $module) {
  45. switch ($field['type']) {
  46. case 'taxonomy_term_reference':
  47. // Add synonyms friendly filters.
  48. foreach ($field['storage']['details']['sql'] as $table) {
  49. $tid_column = reset($table);
  50. $tid_column = $tid_column['tid'];
  51. $table = array_keys($table);
  52. $table = $table[0];
  53. if (isset($result[$table][$tid_column]['filter'])) {
  54. $result[$table][$tid_column]['filter']['handler'] = 'synonyms_views_handler_filter_term_tid';
  55. }
  56. }
  57. break;
  58. case 'entityreference':
  59. foreach ($field['storage']['details']['sql'] as $table) {
  60. $target_id_column = reset($table);
  61. $target_id_column = $target_id_column['target_id'];
  62. $table = array_keys($table);
  63. $table = $table[0];
  64. if (isset($result[$table][$target_id_column]['filter'])) {
  65. $result[$table][$target_id_column]['filter']['handler'] = 'synonyms_views_handler_filter_entityreference_synonyms';
  66. }
  67. }
  68. break;
  69. }
  70. }