synonyms_provider_property.module 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @file
  4. * Provides synonyms from entity properties.
  5. */
  6. /**
  7. * Implements hook_synonyms_behavior_implementation_info().
  8. */
  9. function synonyms_provider_property_synonyms_behavior_implementation_info($entity_type, $bundle, $behavior) {
  10. $providers = array();
  11. switch ($behavior) {
  12. case 'select':
  13. case 'autocomplete':
  14. case 'search':
  15. $entity_info = entity_get_info($entity_type);
  16. $class = $behavior == 'search' ? 'SearchPropertySynonymsBehavior' : 'PropertySynonymsBehavior';
  17. if ($entity_info['base table'] && isset($entity_info['entity keys']['id'])) {
  18. $entity_keys = array_filter($entity_info['entity keys']);
  19. $properties = entity_get_property_info($entity_type);
  20. $properties = $properties['properties'];
  21. // Label is the label of an entity, therefore it may not be a synonym.
  22. if (isset($entity_keys['label'])) {
  23. unset($properties[$entity_keys['label']]);
  24. unset($entity_keys['label']);
  25. }
  26. // Get all defined entity keys. We prefer them from entity keys rather
  27. // than from entity properties, because in the former case we know what
  28. // those keys represent (entity ID, bundle, etc), so we can give a bit
  29. // more unified UI by maintaining the same labels for the same entity
  30. // keys among different entity types.
  31. foreach ($entity_keys as $entity_key => $column) {
  32. if (in_array($column, $entity_info['schema_fields_sql']['base table'])) {
  33. $providers[] = array(
  34. 'provider' => synonyms_provider_property_provider_name($column),
  35. 'label' => $entity_key,
  36. 'class' => $class,
  37. );
  38. unset($properties[$column]);
  39. }
  40. }
  41. foreach ($properties as $property => $property_info) {
  42. if (isset($property_info['schema field']) && $property_info['schema field']) {
  43. $providers[] = array(
  44. 'provider' => synonyms_provider_property_provider_name($property_info['schema field']),
  45. 'label' => $property_info['label'],
  46. 'class' => $class,
  47. );
  48. }
  49. }
  50. }
  51. break;
  52. }
  53. return $providers;
  54. }
  55. /**
  56. * Construct synonyms provider name out of underlying property.
  57. *
  58. * This function is inverse of synonyms_provider_property_name().
  59. *
  60. * @param string $property
  61. * Entity property name whose provider name should be constructed
  62. *
  63. * @return string
  64. * Provider name that corresponds to the $property
  65. *
  66. * @see synonyms_provider_property_name()
  67. */
  68. function synonyms_provider_property_provider_name($property) {
  69. return 'synonyms_provider_property_' . $property;
  70. }
  71. /**
  72. * Reconstruct entity property name from the name of its provider.
  73. *
  74. * This function is inverse of synonyms_provider_property_provider_name().
  75. *
  76. * @param string $provider
  77. * Name of the provider whose underlying entity property should be
  78. * reconstructed
  79. *
  80. * @return string
  81. * Entity property name that corresponds to the $provider
  82. *
  83. * @see synonyms_provider_property_provider_name()
  84. */
  85. function synonyms_provider_property_name($provider) {
  86. return drupal_substr($provider, drupal_strlen('synonyms_provider_property_'));
  87. }