field.info.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // Since the label will be exposed via hook_token_info() and it is not
  50. // clearly defined if that should be sanitized already we prevent XSS
  51. // right here (field labels are user provided text).
  52. 'label' => filter_xss_admin($instance['label']),
  53. 'type' => $field_type['property_type'],
  54. 'description' => t('Field "@name".', array('@name' => $name)),
  55. 'getter callback' => 'entity_metadata_field_property_get',
  56. 'setter callback' => 'entity_metadata_field_property_set',
  57. 'access callback' => 'entity_metadata_field_access_callback',
  58. 'query callback' => 'entity_metadata_field_query',
  59. 'translatable' => !empty($field['translatable']),
  60. // Specify that this property stems from a field.
  61. 'field' => TRUE,
  62. 'required' => !empty($instance['required']),
  63. );
  64. // For field types of the list module add in the options list callback.
  65. if (strpos($field['type'], 'list') === 0) {
  66. $property['options list'] = 'entity_metadata_field_options_list';
  67. }
  68. }
  69. }
  70. /**
  71. * Additional callback to adapt the property info for text fields. If a text
  72. * field is processed we make use of a separate data structure so that format
  73. * filters are available too. For the text value the sanitized, thus processed
  74. * value is returned by default.
  75. *
  76. * @see entity_metadata_field_entity_property_info()
  77. * @see entity_field_info_alter()
  78. * @see entity_property_text_formatted_info()
  79. */
  80. function entity_metadata_field_text_property_callback(&$info, $entity_type, $field, $instance, $field_type) {
  81. if (!empty($instance['settings']['text_processing']) || $field['type'] == 'text_with_summary') {
  82. // Define a data structure for dealing with text that is formatted or has
  83. // a summary.
  84. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  85. $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  86. $property['setter callback'] = 'entity_metadata_field_verbatim_set';
  87. unset($property['query callback']);
  88. if (empty($instance['settings']['text_processing'])) {
  89. $property['property info'] = entity_property_field_item_textsummary_info();
  90. }
  91. else {
  92. // For formatted text we use the type name 'text_formatted'.
  93. $property['type'] = ($field['cardinality'] != 1) ? 'list<text_formatted>' : 'text_formatted';
  94. $property['property info'] = entity_property_text_formatted_info();
  95. }
  96. // Enable auto-creation of the item, so that it is possible to just set
  97. // the textual or summary value.
  98. $property['auto creation'] = 'entity_property_create_array';
  99. if ($field['type'] != 'text_with_summary') {
  100. unset($property['property info']['summary']);
  101. }
  102. }
  103. }
  104. /**
  105. * Additional callback to adapt the property info for term reference fields.
  106. * @see entity_metadata_field_entity_property_info().
  107. */
  108. function entity_metadata_field_term_reference_callback(&$info, $entity_type, $field, $instance, $field_type) {
  109. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  110. if (count($field['settings']['allowed_values']) == 1) {
  111. $settings = reset($field['settings']['allowed_values']);
  112. $property['bundle'] = $settings['vocabulary'];
  113. }
  114. // Only add the options list callback for controlled vocabularies, thus
  115. // vocabularies not using the autocomplete widget.
  116. if ($instance['widget']['type'] != 'taxonomy_autocomplete') {
  117. $property['options list'] = 'entity_metadata_field_options_list';
  118. }
  119. }
  120. /**
  121. * Additional callback to adapt the property info for file fields.
  122. * @see entity_metadata_field_entity_property_info().
  123. */
  124. function entity_metadata_field_file_callback(&$info, $entity_type, $field, $instance, $field_type) {
  125. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  126. // Define a data structure so it's possible to deal with files and their
  127. // descriptions.
  128. $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  129. $property['setter callback'] = 'entity_metadata_field_verbatim_set';
  130. // Auto-create the field $items as soon as a property is set.
  131. $property['auto creation'] = 'entity_metadata_field_file_create_item';
  132. $property['validation callback'] = 'entity_metadata_field_file_validate_item';
  133. $property['property info'] = entity_property_field_item_file_info();
  134. if (empty($instance['settings']['description_field'])) {
  135. unset($property['property info']['description']);
  136. }
  137. if (empty($field['settings']['display_field'])) {
  138. unset($property['property info']['display']);
  139. }
  140. unset($property['query callback']);
  141. }
  142. /**
  143. * Additional callback to adapt the property info for image fields.
  144. * This callback gets invoked after entity_metadata_field_file_callback().
  145. * @see entity_metadata_field_entity_property_info().
  146. */
  147. function entity_metadata_field_image_callback(&$info, $entity_type, $field, $instance, $field_type) {
  148. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  149. // Update the property info with the info for image fields.
  150. $property['property info'] = entity_property_field_item_image_info();
  151. if (empty($instance['settings']['alt_field'])) {
  152. unset($property['property info']['alt']);
  153. }
  154. if (empty($instance['settings']['title_field'])) {
  155. unset($property['property info']['title']);
  156. }
  157. }