field.info.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @file
  4. * Provides info for fields.
  5. */
  6. /**
  7. * Implements hook_entity_property_info() on top of field module.
  8. *
  9. * @see entity_field_info_alter()
  10. * @see entity_entity_property_info()
  11. */
  12. function entity_metadata_field_entity_property_info() {
  13. $info = array();
  14. // Loop over all field instances and add them as property.
  15. foreach (field_info_fields() as $field_name => $field) {
  16. $field += array('bundles' => array());
  17. if ($field_type = field_info_field_types($field['type'])) {
  18. // Add in our default callback as the first one.
  19. $field_type += array('property_callbacks' => array());
  20. array_unshift($field_type['property_callbacks'], 'entity_metadata_field_default_property_callback');
  21. foreach ($field['bundles'] as $entity_type => $bundles) {
  22. foreach ($bundles as $bundle) {
  23. $instance = field_info_instance($entity_type, $field_name, $bundle);
  24. if ($instance && empty($instance['deleted'])) {
  25. foreach ($field_type['property_callbacks'] as $callback) {
  26. $callback($info, $entity_type, $field, $instance, $field_type);
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }
  33. return $info;
  34. }
  35. /**
  36. * Callback to add in property info defaults per field instance.
  37. * @see entity_metadata_field_entity_property_info().
  38. */
  39. function entity_metadata_field_default_property_callback(&$info, $entity_type, $field, $instance, $field_type) {
  40. if (!empty($field_type['property_type'])) {
  41. if ($field['cardinality'] != 1) {
  42. $field_type['property_type'] = 'list<' . $field_type['property_type'] . '>';
  43. }
  44. // Add in instance specific property info, if given and apply defaults.
  45. $name = $field['field_name'];
  46. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
  47. $instance += array('property info' => array());
  48. $property = $instance['property info'] + array(
  49. 'label' => $instance['label'],
  50. 'type' => $field_type['property_type'],
  51. 'description' => t('Field "@name".', array('@name' => $name)),
  52. 'getter callback' => 'entity_metadata_field_property_get',
  53. 'setter callback' => 'entity_metadata_field_property_set',
  54. 'access callback' => 'entity_metadata_field_access_callback',
  55. 'query callback' => 'entity_metadata_field_query',
  56. 'translatable' => !empty($field['translatable']),
  57. // Specify that this property stems from a field.
  58. 'field' => TRUE,
  59. 'required' => !empty($instance['required']),
  60. );
  61. // For field types of the list module add in the options list callback.
  62. if (strpos($field['type'], 'list') === 0) {
  63. $property['options list'] = 'entity_metadata_field_options_list';
  64. }
  65. }
  66. }
  67. /**
  68. * Additional callback to adapt the property info for text fields. If a text
  69. * field is processed we make use of a separate data structure so that format
  70. * filters are available too. For the text value the sanitized, thus processed
  71. * value is returned by default.
  72. *
  73. * @see entity_metadata_field_entity_property_info()
  74. * @see entity_field_info_alter()
  75. * @see entity_property_text_formatted_info()
  76. */
  77. function entity_metadata_field_text_property_callback(&$info, $entity_type, $field, $instance, $field_type) {
  78. if (!empty($instance['settings']['text_processing']) || $field['type'] == 'text_with_summary') {
  79. // Define a data structure for dealing with text that is formatted or has
  80. // a summary.
  81. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  82. $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  83. $property['setter callback'] = 'entity_metadata_field_verbatim_set';
  84. unset($property['query callback']);
  85. if (empty($instance['settings']['text_processing'])) {
  86. $property['property info'] = entity_property_field_item_textsummary_info();
  87. }
  88. else {
  89. // For formatted text we use the type name 'text_formatted'.
  90. $property['type'] = ($field['cardinality'] != 1) ? 'list<text_formatted>' : 'text_formatted';
  91. $property['property info'] = entity_property_text_formatted_info();
  92. }
  93. // Enable auto-creation of the item, so that it is possible to just set
  94. // the textual or summary value.
  95. $property['auto creation'] = 'entity_property_create_array';
  96. if ($field['type'] != 'text_with_summary') {
  97. unset($property['property info']['summary']);
  98. }
  99. }
  100. }
  101. /**
  102. * Additional callback to adapt the property info for term reference fields.
  103. * @see entity_metadata_field_entity_property_info().
  104. */
  105. function entity_metadata_field_term_reference_callback(&$info, $entity_type, $field, $instance, $field_type) {
  106. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  107. if (count($field['settings']['allowed_values']) == 1) {
  108. $settings = reset($field['settings']['allowed_values']);
  109. $property['bundle'] = $settings['vocabulary'];
  110. }
  111. // Only add the options list callback for controlled vocabularies, thus
  112. // vocabularies not using the autocomplete widget.
  113. if ($instance['widget']['type'] != 'taxonomy_autocomplete') {
  114. $property['options list'] = 'entity_metadata_field_options_list';
  115. }
  116. }
  117. /**
  118. * Additional callback to adapt the property info for file fields.
  119. * @see entity_metadata_field_entity_property_info().
  120. */
  121. function entity_metadata_field_file_callback(&$info, $entity_type, $field, $instance, $field_type) {
  122. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  123. // Define a data structure so it's possible to deal with files and their
  124. // descriptions.
  125. $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  126. $property['setter callback'] = 'entity_metadata_field_verbatim_set';
  127. // Auto-create the field $items as soon as a property is set.
  128. $property['auto creation'] = 'entity_metadata_field_file_create_item';
  129. $property['validation callback'] = 'entity_metadata_field_file_validate_item';
  130. $property['property info'] = entity_property_field_item_file_info();
  131. if (empty($instance['settings']['description_field'])) {
  132. unset($property['property info']['description']);
  133. }
  134. if (empty($field['settings']['display_field'])) {
  135. unset($property['property info']['display']);
  136. }
  137. unset($property['query callback']);
  138. }
  139. /**
  140. * Additional callback to adapt the property info for image fields.
  141. * This callback gets invoked after entity_metadata_field_file_callback().
  142. * @see entity_metadata_field_entity_property_info().
  143. */
  144. function entity_metadata_field_image_callback(&$info, $entity_type, $field, $instance, $field_type) {
  145. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  146. // Update the property info with the info for image fields.
  147. $property['property info'] = entity_property_field_item_image_info();
  148. if (empty($instance['settings']['alt_field'])) {
  149. unset($property['property info']['alt']);
  150. }
  151. if (empty($field['settings']['title_field'])) {
  152. unset($property['property info']['title']);
  153. }
  154. }