entity.info.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * @file
  4. * Provides basic entity property info for entities provided via the CRUD API,
  5. * as well as property info for all entity types defined by core. For that
  6. * the respective modules/MODULE.info.inc files are included.
  7. */
  8. /**
  9. * Implements hook_entity_property_info().
  10. */
  11. function entity_entity_property_info() {
  12. $items = array();
  13. // Add in info about entities provided by the CRUD API.
  14. foreach (entity_crud_get_info() as $type => $info) {
  15. // Automatically enable the controller only if the module does not implement
  16. // the hook itself.
  17. if (!isset($info['metadata controller class']) && !empty($info['base table']) && (!isset($info['module']) || !module_hook($info['module'], 'entity_property_info'))) {
  18. $info['metadata controller class'] = 'EntityDefaultMetadataController';
  19. }
  20. if (!empty($info['metadata controller class'])) {
  21. $controller = new $info['metadata controller class']($type);
  22. $items += $controller->entityPropertyInfo();
  23. }
  24. }
  25. // Add in info for all core entities.
  26. foreach (_entity_metadata_core_modules() as $module) {
  27. module_load_include('inc', 'entity', "modules/$module.info");
  28. if (function_exists($function = "entity_metadata_{$module}_entity_property_info")) {
  29. if ($return = $function()) {
  30. $items = array_merge_recursive($items, $return);
  31. }
  32. }
  33. }
  34. return $items;
  35. }
  36. /**
  37. * Implements hook_entity_property_info_alter().
  38. */
  39. function entity_entity_property_info_alter(&$entity_info) {
  40. // Add in info for all core entities.
  41. foreach (_entity_metadata_core_modules() as $module) {
  42. module_load_include('inc', 'entity', "modules/$module.info");
  43. if (function_exists($function = "entity_metadata_{$module}_entity_property_info_alter")) {
  44. $function($entity_info);
  45. }
  46. }
  47. }
  48. function _entity_metadata_core_modules() {
  49. return array_filter(array('book', 'comment', 'field', 'locale', 'node', 'taxonomy', 'user', 'system', 'statistics'), 'module_exists');
  50. }
  51. /**
  52. * Default controller for generating some basic metadata for CRUD entity types.
  53. */
  54. class EntityDefaultMetadataController {
  55. protected $type, $info;
  56. public function __construct($type) {
  57. $this->type = $type;
  58. $this->info = entity_get_info($type);
  59. }
  60. public function entityPropertyInfo() {
  61. $entity_label = drupal_strtolower($this->info['label']);
  62. // Provide defaults based on the schema.
  63. $info['properties'] = $this->convertSchema();
  64. foreach ($info['properties'] as $name => &$property) {
  65. // Add a description.
  66. $property['description'] = t('@entity "@property" property.', array('@entity' => drupal_ucfirst($entity_label), '@property' => $name));
  67. }
  68. // Set better metadata for known entity keys.
  69. $id_key = $this->info['entity keys']['id'];
  70. if (!empty($this->info['entity keys']['name']) && $key = $this->info['entity keys']['name']) {
  71. $info['properties'][$key]['type'] = 'token';
  72. $info['properties'][$key]['label'] = t('Machine-readable name');
  73. $info['properties'][$key]['description'] = t('The machine-readable name identifying this @entity.', array('@entity' => $entity_label));
  74. $info['properties'][$id_key]['label'] = t('Internal, numeric @entity ID', array('@entity' => $entity_label));
  75. $info['properties'][$id_key]['description'] = t('The ID used to identify this @entity internally.', array('@entity' => $entity_label));
  76. }
  77. else {
  78. $info['properties'][$id_key]['label'] = t('@entity ID', array('@entity' => drupal_ucfirst($entity_label)));
  79. $info['properties'][$id_key]['description'] = t('The unique ID of the @entity.', array('@entity' => $entity_label));
  80. }
  81. // Care for the bundle.
  82. if (!empty($this->info['entity keys']['bundle']) && $key = $this->info['entity keys']['bundle']) {
  83. $info['properties'][$key]['type'] = 'token';
  84. $info['properties'][$key]['options list'] = array(get_class($this), 'bundleOptionsList');
  85. }
  86. // Care for the label.
  87. if (!empty($this->info['entity keys']['label']) && $key = $this->info['entity keys']['label']) {
  88. $info['properties'][$key]['label'] = t('Label');
  89. $info['properties'][$key]['description'] = t('The human readable label.');
  90. }
  91. // Add a computed property for the entity URL and expose it to views.
  92. if (empty($info['properties']['url']) && !empty($this->info['uri callback'])) {
  93. $info['properties']['url'] = array(
  94. 'label' => t('URL'),
  95. 'description' => t('The URL of the entity.'),
  96. 'getter callback' => 'entity_metadata_entity_get_properties',
  97. 'type' => 'uri',
  98. 'computed' => TRUE,
  99. 'entity views field' => TRUE,
  100. );
  101. }
  102. return array($this->type => $info);
  103. }
  104. /**
  105. * A options list callback returning all bundles for an entity type.
  106. */
  107. public static function bundleOptionsList($name, $info) {
  108. if (!empty($info['parent']) && $type = $info['parent']) {
  109. $entity_info = $info['parent']->entityInfo();
  110. $options = array();
  111. foreach ($entity_info['bundles'] as $name => $bundle_info) {
  112. $options[$name] = $bundle_info['label'];
  113. }
  114. return $options;
  115. }
  116. }
  117. /**
  118. * Return a set of properties for an entity based on the schema definition
  119. */
  120. protected function convertSchema() {
  121. return entity_metadata_convert_schema($this->info['base table']);
  122. }
  123. }
  124. /**
  125. * Converts the schema information available for the given table to property info.
  126. *
  127. * @param $table
  128. * The name of the table as used in hook_schema().
  129. * @return
  130. * An array of property info as suiting for hook_entity_property_info().
  131. */
  132. function entity_metadata_convert_schema($table) {
  133. $schema = drupal_get_schema($table);
  134. $properties = array();
  135. foreach ($schema['fields'] as $name => $info) {
  136. if ($type = _entity_metadata_convert_schema_type($info['type'])) {
  137. $properties[$name] = array(
  138. 'type' => $type,
  139. 'label' => drupal_ucfirst($name),
  140. 'schema field' => $name,
  141. // As we cannot know about any setter access, leave out the setter
  142. // callback. For getting usually no further access callback is needed.
  143. );
  144. if ($info['type'] == 'serial') {
  145. $properties[$name]['validation callback'] = 'entity_metadata_validate_integer_positive';
  146. }
  147. }
  148. }
  149. return $properties;
  150. }
  151. function _entity_metadata_convert_schema_type($type) {
  152. switch ($type) {
  153. case 'int':
  154. case 'serial':
  155. return 'integer';
  156. case 'float':
  157. case 'numeric':
  158. return 'decimal';
  159. case 'char':
  160. case 'varchar':
  161. case 'text':
  162. return 'text';
  163. }
  164. }
  165. /**
  166. * Interface for extra fields controller.
  167. *
  168. * Note: Displays extra fields exposed by this controller are rendered by
  169. * default by the EntityAPIController.
  170. */
  171. interface EntityExtraFieldsControllerInterface {
  172. /**
  173. * Returns extra fields for this entity type.
  174. *
  175. * @see hook_field_extra_fields().
  176. */
  177. public function fieldExtraFields();
  178. }
  179. /**
  180. * Default controller for generating extra fields based on property metadata.
  181. *
  182. * By default a display extra field for each property not being a field, ID or
  183. * bundle is generated.
  184. */
  185. class EntityDefaultExtraFieldsController implements EntityExtraFieldsControllerInterface {
  186. /**
  187. * @var string
  188. */
  189. protected $entityType;
  190. /**
  191. * @var array
  192. */
  193. protected $entityInfo;
  194. /**
  195. * Constructor.
  196. */
  197. public function __construct($type) {
  198. $this->entityType = $type;
  199. $this->entityInfo = entity_get_info($type);
  200. $this->propertyInfo = entity_get_property_info($type);
  201. }
  202. /**
  203. * Implements EntityExtraFieldsControllerInterface::fieldExtraFields().
  204. */
  205. public function fieldExtraFields() {
  206. $extra = array();
  207. foreach ($this->propertyInfo['properties'] as $name => $property_info) {
  208. // Skip adding the ID or bundle.
  209. if ($this->entityInfo['entity keys']['id'] == $name || $this->entityInfo['entity keys']['bundle'] == $name) {
  210. continue;
  211. }
  212. $extra[$this->entityType][$this->entityType]['display'][$name] = $this->generateExtraFieldInfo($name, $property_info);
  213. }
  214. // Handle bundle properties.
  215. $this->propertyInfo += array('bundles' => array());
  216. foreach ($this->propertyInfo['bundles'] as $bundle_name => $info) {
  217. foreach ($info['properties'] as $name => $property_info) {
  218. if (empty($property_info['field'])) {
  219. $extra[$this->entityType][$bundle_name]['display'][$name] = $this->generateExtraFieldInfo($name, $property_info);
  220. }
  221. }
  222. }
  223. return $extra;
  224. }
  225. /**
  226. * Generates the display field info for a given property.
  227. */
  228. protected function generateExtraFieldInfo($name, $property_info) {
  229. $info = array(
  230. 'label' => $property_info['label'],
  231. 'weight' => 0,
  232. );
  233. if (!empty($property_info['description'])) {
  234. $info['description'] = $property_info['description'];
  235. }
  236. return $info;
  237. }
  238. }