features.field.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Implements hook_features_api().
  4. */
  5. function field_features_api() {
  6. return array(
  7. 'field' => array(
  8. 'name' => t('Fields'),
  9. 'default_hook' => 'field_default_fields',
  10. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  11. 'feature_source' => TRUE,
  12. )
  13. );
  14. }
  15. /**
  16. * Implements hook_features_export_options().
  17. */
  18. function field_features_export_options() {
  19. $options = array();
  20. $instances = field_info_instances();
  21. foreach ($instances as $entity_type => $bundles) {
  22. foreach ($bundles as $bundle => $fields) {
  23. foreach ($fields as $field) {
  24. $identifier = "{$entity_type}-{$bundle}-{$field['field_name']}";
  25. $options[$identifier] = $identifier;
  26. }
  27. }
  28. }
  29. return $options;
  30. }
  31. /**
  32. * Implements hook_features_export().
  33. */
  34. function field_features_export($data, &$export, $module_name = '') {
  35. $pipe = array();
  36. $map = features_get_default_map('field');
  37. // The field_default_fields() hook integration is provided by the
  38. // features module so we need to add it as a dependency.
  39. $export['dependencies']['features'] = 'features';
  40. foreach ($data as $identifier) {
  41. if ($field = features_field_load($identifier)) {
  42. // If this field is already provided by another module, remove the field
  43. // and add the other module as a dependency.
  44. if (isset($map[$identifier]) && $map[$identifier] != $module_name) {
  45. if (isset($export['features']['field'][$identifier])) {
  46. unset($export['features']['field'][$identifier]);
  47. }
  48. $module = $map[$identifier];
  49. $export['dependencies'][$module] = $module;
  50. }
  51. // If the field has not yet been exported, add it
  52. else {
  53. $export['features']['field'][$identifier] = $identifier;
  54. $export['dependencies'][$field['field_config']['module']] = $field['field_config']['module'];
  55. if ($field['field_config']['storage']['type'] != variable_get('field_storage_default', 'field_sql_storage')) {
  56. $export['dependencies'][$field['field_config']['storage']['module']] = $field['field_config']['storage']['module'];
  57. }
  58. $export['dependencies'][$field['field_instance']['widget']['module']] = $field['field_instance']['widget']['module'];
  59. foreach ($field['field_instance']['display'] as $key => $display) {
  60. if (isset($display['module'])) {
  61. $export['dependencies'][$display['module']] = $display['module'];
  62. // @TODO: handle the pipe to image styles
  63. }
  64. }
  65. // If taxonomy field, add in the vocabulary
  66. if ($field['field_config']['type'] == 'taxonomy_term_reference' && !empty($field['field_config']['settings']['allowed_values'])) {
  67. foreach ($field['field_config']['settings']['allowed_values'] as $allowed_values) {
  68. if (!empty($allowed_values['vocabulary'])) {
  69. $pipe['taxonomy'][] = $allowed_values['vocabulary'];
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. return $pipe;
  77. }
  78. /**
  79. * Implements hook_features_export_render().
  80. */
  81. function field_features_export_render($module, $data, $export = NULL) {
  82. $translatables = $code = array();
  83. $code[] = ' $fields = array();';
  84. $code[] = '';
  85. foreach ($data as $identifier) {
  86. if ($field = features_field_load($identifier)) {
  87. unset($field['field_config']['columns']);
  88. // Only remove the 'storage' declaration if the field is using the default
  89. // storage type.
  90. if ($field['field_config']['storage']['type'] == variable_get('field_storage_default', 'field_sql_storage')) {
  91. unset($field['field_config']['storage']);
  92. }
  93. // If we still have a storage declaration here it means that a non-default
  94. // storage type was altered into to the field definition. And noone would
  95. // never need to change the 'details' key, so don't render it.
  96. if (isset($field['field_config']['storage']['details'])) {
  97. unset($field['field_config']['storage']['details']);
  98. }
  99. _field_features_export_sort($field);
  100. $field_export = features_var_export($field, ' ');
  101. $field_identifier = features_var_export($identifier);
  102. $code[] = " // Exported field: {$field_identifier}.";
  103. $code[] = " \$fields[{$field_identifier}] = {$field_export};";
  104. $code[] = "";
  105. // Add label and description to translatables array.
  106. if (!empty($field['field_instance']['label'])) {
  107. $translatables[] = $field['field_instance']['label'];
  108. }
  109. if (!empty($field['field_instance']['description'])) {
  110. $translatables[] = $field['field_instance']['description'];
  111. }
  112. }
  113. }
  114. if (!empty($translatables)) {
  115. $code[] = features_translatables_export($translatables, ' ');
  116. }
  117. $code[] = ' return $fields;';
  118. $code = implode("\n", $code);
  119. return array('field_default_fields' => $code);
  120. }
  121. // Helper to enforce consistency in field export arrays.
  122. function _field_features_export_sort(&$field, $sort = TRUE) {
  123. // Some arrays are not sorted to preserve order (for example allowed_values).
  124. static $sort_blacklist = array(
  125. 'allowed_values',
  126. 'format_handlers',
  127. );
  128. if ($sort) {
  129. ksort($field);
  130. }
  131. foreach ($field as $k => $v) {
  132. if (is_array($v)) {
  133. _field_features_export_sort($field[$k], !in_array($k, $sort_blacklist));
  134. }
  135. }
  136. }
  137. /**
  138. * Implements hook_features_revert().
  139. */
  140. function field_features_revert($module) {
  141. field_features_rebuild($module);
  142. }
  143. /**
  144. * Implements of hook_features_rebuild().
  145. * Rebuilds fields from code defaults.
  146. */
  147. function field_features_rebuild($module) {
  148. if ($fields = features_get_default('field', $module)) {
  149. field_info_cache_clear();
  150. // Load all the existing fields and instance up-front so that we don't
  151. // have to rebuild the cache all the time.
  152. $existing_fields = field_info_fields();
  153. $existing_instances = field_info_instances();
  154. foreach ($fields as $field) {
  155. // Create or update field.
  156. $field_config = $field['field_config'];
  157. if (isset($existing_fields[$field_config['field_name']])) {
  158. $existing_field = $existing_fields[$field_config['field_name']];
  159. if ($field_config + $existing_field != $existing_field) {
  160. field_update_field($field_config);
  161. }
  162. }
  163. else {
  164. field_create_field($field_config);
  165. $existing_fields[$field_config['field_name']] = $field_config;
  166. }
  167. // Create or update field instance.
  168. $field_instance = $field['field_instance'];
  169. if (isset($existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']])) {
  170. $existing_instance = $existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']];
  171. if ($field_instance + $existing_instance != $existing_instance) {
  172. field_update_instance($field_instance);
  173. }
  174. }
  175. else {
  176. field_create_instance($field_instance);
  177. $existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']] = $field_instance;
  178. }
  179. }
  180. if ($fields) {
  181. variable_set('menu_rebuild_needed', TRUE);
  182. }
  183. }
  184. }
  185. /**
  186. * Load a field's configuration and instance configuration by an
  187. * entity_type-bundle-field_name identifier.
  188. */
  189. function features_field_load($identifier) {
  190. list($entity_type, $bundle, $field_name) = explode('-', $identifier);
  191. $field_info = field_info_field($field_name);
  192. $instance_info = field_info_instance($entity_type, $field_name, $bundle);
  193. if ($field_info && $instance_info) {
  194. unset($field_info['id']);
  195. unset($field_info['bundles']);
  196. unset($instance_info['id']);
  197. unset($instance_info['field_id']);
  198. return array(
  199. 'field_config' => $field_info,
  200. 'field_instance' => $instance_info,
  201. );
  202. }
  203. return FALSE;
  204. }