features.field.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <?php
  2. /**
  3. * Implements hook_features_api().
  4. */
  5. function field_features_api() {
  6. return array(
  7. 'field' => array(
  8. // this is deprecated by field_base and field_instance
  9. // but retained for compatibility with older exports
  10. 'name' => t('Fields'),
  11. 'default_hook' => 'field_default_fields',
  12. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  13. 'feature_source' => FALSE,
  14. ),
  15. 'field_base' => array(
  16. 'name' => t('Field Bases'),
  17. 'default_hook' => 'field_default_field_bases',
  18. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  19. 'feature_source' => TRUE,
  20. 'supersedes' => 'field',
  21. ),
  22. 'field_instance' => array(
  23. 'name' => t('Field Instances'),
  24. 'default_hook' => 'field_default_field_instances',
  25. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  26. 'feature_source' => TRUE,
  27. 'supersedes' => 'field',
  28. )
  29. );
  30. }
  31. /**
  32. * Implements hook_features_export_options().
  33. */
  34. function field_base_features_export_options() {
  35. $options = array();
  36. $fields = field_info_fields();
  37. foreach ($fields as $field_name => $field) {
  38. $options[$field_name] = $field_name;
  39. }
  40. return $options;
  41. }
  42. /**
  43. * Implements hook_features_export_options().
  44. */
  45. function field_instance_features_export_options() {
  46. $options = array();
  47. foreach (field_info_fields() as $field_name => $field) {
  48. foreach ($field['bundles'] as $entity_type => $bundles) {
  49. foreach ($bundles as $bundle) {
  50. $identifier = "{$entity_type}-{$bundle}-{$field_name}";
  51. $options[$identifier] = $identifier;
  52. }
  53. }
  54. }
  55. ksort($options);
  56. return $options;
  57. }
  58. /**
  59. * Implements hook_features_export().
  60. */
  61. function field_base_features_export($data, &$export, $module_name = '') {
  62. $pipe = array();
  63. $map = features_get_default_map('field_base');
  64. // The field_default_field_bases() hook integration is provided by the
  65. // features module so we need to add it as a dependency.
  66. $export['dependencies']['features'] = 'features';
  67. foreach ($data as $identifier) {
  68. if ($base = features_field_base_load($identifier)) {
  69. // If this field is already provided by another module, remove the field
  70. // and add the other module as a dependency.
  71. if (isset($map[$identifier]) && $map[$identifier] != $module_name) {
  72. if (isset($export['features']['field_base'][$identifier])) {
  73. unset($export['features']['field_base'][$identifier]);
  74. }
  75. $module = $map[$identifier];
  76. $export['dependencies'][$module] = $module;
  77. }
  78. // If the field has not yet been exported, add it
  79. else {
  80. $export['features']['field_base'][$identifier] = $identifier;
  81. $export['dependencies'][$base['module']] = $base['module'];
  82. if ($base['storage']['type'] != variable_get('field_storage_default', 'field_sql_storage')) {
  83. $export['dependencies'][$base['storage']['module']] = $base['storage']['module'];
  84. }
  85. // If taxonomy field, add in the vocabulary
  86. if ($base['type'] == 'taxonomy_term_reference' && !empty($base['settings']['allowed_values'])) {
  87. foreach ($base['settings']['allowed_values'] as $allowed_values) {
  88. if (!empty($allowed_values['vocabulary'])) {
  89. $pipe['taxonomy'][] = $allowed_values['vocabulary'];
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. return $pipe;
  97. }
  98. /**
  99. * Implements hook_features_export().
  100. */
  101. function field_instance_features_export($data, &$export, $module_name = '') {
  102. $pipe = array('field_base' => array());
  103. $map = features_get_default_map('field_instance');
  104. // The field_default_field_instances() hook integration is provided by the
  105. // features module so we need to add it as a dependency.
  106. $export['dependencies']['features'] = 'features';
  107. foreach ($data as $identifier) {
  108. if ($instance = features_field_instance_load($identifier)) {
  109. // If this field is already provided by another module, remove the field
  110. // and add the other module as a dependency.
  111. if (isset($map[$identifier]) && $map[$identifier] != $module_name) {
  112. if (isset($export['features']['field_instance'][$identifier])) {
  113. unset($export['features']['field_instance'][$identifier]);
  114. }
  115. $module = $map[$identifier];
  116. $export['dependencies'][$module] = $module;
  117. }
  118. // If the field has not yet been exported, add it
  119. else {
  120. $export['features']['field_instance'][$identifier] = $identifier;
  121. $export['dependencies'][$instance['widget']['module']] = $instance['widget']['module'];
  122. foreach ($instance['display'] as $key => $display) {
  123. if (isset($display['module'])) {
  124. $export['dependencies'][$display['module']] = $display['module'];
  125. // @TODO: handle the pipe to image styles
  126. }
  127. }
  128. $pipe['field_base'][] = $instance['field_name'];
  129. }
  130. }
  131. }
  132. return $pipe;
  133. }
  134. /**
  135. * Implements hook_features_export_render().
  136. */
  137. function field_base_features_export_render($module, $data, $export = NULL) {
  138. $translatables = $code = array();
  139. $code[] = ' $field_bases = array();';
  140. $code[] = '';
  141. foreach ($data as $identifier) {
  142. if ($field = features_field_base_load($identifier)) {
  143. unset($field['columns']);
  144. // unset($field['locked']);
  145. // Only remove the 'storage' declaration if the field is using the default
  146. // storage type.
  147. if ($field['storage']['type'] == variable_get('field_storage_default', 'field_sql_storage')) {
  148. unset($field['storage']);
  149. }
  150. // If we still have a storage declaration here it means that a non-default
  151. // storage type was altered into to the field definition. And noone would
  152. // never need to change the 'details' key, so don't render it.
  153. if (isset($field['storage']['details'])) {
  154. unset($field['storage']['details']);
  155. }
  156. _field_instance_features_export_sort($field);
  157. $field_export = features_var_export($field, ' ');
  158. $field_identifier = features_var_export($identifier);
  159. $code[] = " // Exported field_base: {$field_identifier}";
  160. $code[] = " \$field_bases[{$field_identifier}] = {$field_export};";
  161. $code[] = "";
  162. }
  163. }
  164. $code[] = ' return $field_bases;';
  165. $code = implode("\n", $code);
  166. return array('field_default_field_bases' => $code);
  167. }
  168. /**
  169. * Implements hook_features_export_render().
  170. */
  171. function field_instance_features_export_render($module, $data, $export = NULL) {
  172. $translatables = $code = array();
  173. $code[] = ' $field_instances = array();';
  174. $code[] = '';
  175. foreach ($data as $identifier) {
  176. if ($instance = features_field_instance_load($identifier)) {
  177. _field_instance_features_export_sort($instance);
  178. $field_export = features_var_export($instance, ' ');
  179. $instance_identifier = features_var_export($identifier);
  180. $code[] = " // Exported field_instance: {$instance_identifier}";
  181. $code[] = " \$field_instances[{$instance_identifier}] = {$field_export};";
  182. $code[] = "";
  183. if (!empty($instance['label'])) {
  184. $translatables[] = $instance['label'];
  185. }
  186. if (!empty($instance['description'])) {
  187. $translatables[] = $instance['description'];
  188. }
  189. }
  190. }
  191. if (!empty($translatables)) {
  192. $code[] = features_translatables_export($translatables, ' ');
  193. }
  194. $code[] = ' return $field_instances;';
  195. $code = implode("\n", $code);
  196. return array('field_default_field_instances' => $code);
  197. }
  198. // Helper to enforce consistency in field export arrays.
  199. function _field_instance_features_export_sort(&$field, $sort = TRUE) {
  200. // Some arrays are not sorted to preserve order (for example allowed_values).
  201. static $sort_blacklist = array(
  202. 'allowed_values',
  203. 'format_handlers',
  204. );
  205. if ($sort) {
  206. uksort($field, 'strnatcmp');
  207. }
  208. foreach ($field as $k => $v) {
  209. if (is_array($v)) {
  210. _field_instance_features_export_sort($field[$k], !in_array($k, $sort_blacklist));
  211. }
  212. }
  213. }
  214. /**
  215. * Implements hook_features_revert().
  216. */
  217. function field_base_features_revert($module) {
  218. field_base_features_rebuild($module);
  219. }
  220. /**
  221. * Implements hook_features_revert().
  222. */
  223. function field_instance_features_revert($module) {
  224. field_instance_features_rebuild($module);
  225. }
  226. /**
  227. * Implements of hook_features_rebuild().
  228. * Rebuilds fields from code defaults.
  229. */
  230. function field_base_features_rebuild($module) {
  231. if ($fields = features_get_default('field_base', $module)) {
  232. field_info_cache_clear();
  233. // Load all the existing field bases up-front so that we don't
  234. // have to rebuild the cache all the time.
  235. $existing_fields = field_info_fields();
  236. foreach ($fields as $field) {
  237. // Create or update field.
  238. if (isset($existing_fields[$field['field_name']])) {
  239. $existing_field = $existing_fields[$field['field_name']];
  240. if ($field + $existing_field !== $existing_field) {
  241. field_update_field($field);
  242. }
  243. }
  244. else {
  245. field_create_field($field);
  246. $existing_fields[$field['field_name']] = $field;
  247. }
  248. variable_set('menu_rebuild_needed', TRUE);
  249. }
  250. }
  251. }
  252. /**
  253. * Implements of hook_features_rebuild().
  254. * Rebuilds field instances from code defaults.
  255. */
  256. function field_instance_features_rebuild($module) {
  257. if ($instances = features_get_default('field_instance', $module)) {
  258. field_info_cache_clear();
  259. // Load all the existing instances up-front so that we don't
  260. // have to rebuild the cache all the time.
  261. $existing_instances = field_info_instances();
  262. foreach ($instances as $field_instance) {
  263. // If the field base information does not exist yet, cancel out.
  264. if (!field_info_field($field_instance['field_name'])) {
  265. continue;
  266. }
  267. // Create or update field instance.
  268. if (isset($existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']])) {
  269. $existing_instance = $existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']];
  270. if ($field_instance + $existing_instance !== $existing_instance) {
  271. try {
  272. field_update_instance($field_instance);
  273. }
  274. catch (FieldException $e) {
  275. watchdog('features', 'Attempt to update field instance %label (in %entity entity type %bundle bundle) failed: %message', array('%label' => $field_instance['field_name'], '%entity' => $field_instance['entity_type'], '%bundle' => $field_instance['bundle'], '%message' => $e->getMessage()), WATCHDOG_ERROR);
  276. }
  277. }
  278. }
  279. else {
  280. try {
  281. field_create_instance($field_instance);
  282. }
  283. catch (FieldException $e) {
  284. watchdog('features', 'Attempt to create field instance %label (in %entity entity type %bundle bundle) failed: %message', array('%label' => $field_instance['field_name'], '%entity' => $field_instance['entity_type'], '%bundle' => $field_instance['bundle'], '%message' => $e->getMessage()), WATCHDOG_ERROR);
  285. }
  286. $existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']] = $field_instance;
  287. }
  288. }
  289. if ($instances) {
  290. variable_set('menu_rebuild_needed', TRUE);
  291. }
  292. }
  293. }
  294. /**
  295. * Load a field base configuration by a field_name identifier.
  296. */
  297. function features_field_base_load($field_name) {
  298. if ($field_info = field_info_field($field_name)) {
  299. unset($field_info['id']);
  300. unset($field_info['bundles']);
  301. return $field_info;
  302. }
  303. return FALSE;
  304. }
  305. /**
  306. * Load a field's instance configuration by an entity_type-bundle-field_name
  307. * identifier.
  308. */
  309. function features_field_instance_load($identifier) {
  310. list($entity_type, $bundle, $field_name) = explode('-', $identifier);
  311. if ($instance_info = field_info_instance($entity_type, $field_name, $bundle)) {
  312. unset($instance_info['id']);
  313. unset($instance_info['field_id']);
  314. return $instance_info;
  315. }
  316. return FALSE;
  317. }
  318. /* ----- DEPRECATED FIELD EXPORT -----
  319. * keep this code for backward compatibility with older exports
  320. * until v3.x
  321. */
  322. /**
  323. * Implements hook_features_export_options().
  324. */
  325. function field_features_export_options() {
  326. $options = array();
  327. $instances = field_info_instances();
  328. foreach ($instances as $entity_type => $bundles) {
  329. foreach ($bundles as $bundle => $fields) {
  330. foreach ($fields as $field) {
  331. $identifier = "{$entity_type}-{$bundle}-{$field['field_name']}";
  332. $options[$identifier] = $identifier;
  333. }
  334. }
  335. }
  336. return $options;
  337. }
  338. /**
  339. * Implements hook_features_export().
  340. */
  341. function field_features_export($data, &$export, $module_name = '') {
  342. $pipe = array();
  343. // Convert 'field' to 'field_instance' on features-update.
  344. $pipe['field_instance'] = $data;
  345. return $pipe;
  346. }
  347. /**
  348. * Implements hook_features_export_render().
  349. */
  350. function field_features_export_render($module, $data, $export = NULL) {
  351. $translatables = $code = array();
  352. $code[] = ' $fields = array();';
  353. $code[] = '';
  354. foreach ($data as $identifier) {
  355. if ($field = features_field_load($identifier)) {
  356. unset($field['field_config']['columns']);
  357. // Only remove the 'storage' declaration if the field is using the default
  358. // storage type.
  359. if ($field['field_config']['storage']['type'] == variable_get('field_storage_default', 'field_sql_storage')) {
  360. unset($field['field_config']['storage']);
  361. }
  362. // If we still have a storage declaration here it means that a non-default
  363. // storage type was altered into to the field definition. And noone would
  364. // never need to change the 'details' key, so don't render it.
  365. if (isset($field['field_config']['storage']['details'])) {
  366. unset($field['field_config']['storage']['details']);
  367. }
  368. _field_features_export_sort($field);
  369. $field_export = features_var_export($field, ' ');
  370. $field_identifier = features_var_export($identifier);
  371. $code[] = " // Exported field: {$field_identifier}.";
  372. $code[] = " \$fields[{$field_identifier}] = {$field_export};";
  373. $code[] = "";
  374. // Add label and description to translatables array.
  375. if (!empty($field['field_instance']['label'])) {
  376. $translatables[] = $field['field_instance']['label'];
  377. }
  378. if (!empty($field['field_instance']['description'])) {
  379. $translatables[] = $field['field_instance']['description'];
  380. }
  381. }
  382. }
  383. if (!empty($translatables)) {
  384. $code[] = features_translatables_export($translatables, ' ');
  385. }
  386. $code[] = ' return $fields;';
  387. $code = implode("\n", $code);
  388. return array('field_default_fields' => $code);
  389. }
  390. // Helper to enforce consistency in field export arrays.
  391. function _field_features_export_sort(&$field, $sort = TRUE) {
  392. // Some arrays are not sorted to preserve order (for example allowed_values).
  393. static $sort_blacklist = array(
  394. 'allowed_values',
  395. 'format_handlers',
  396. );
  397. if ($sort) {
  398. ksort($field);
  399. }
  400. foreach ($field as $k => $v) {
  401. if (is_array($v)) {
  402. _field_features_export_sort($field[$k], !in_array($k, $sort_blacklist));
  403. }
  404. }
  405. }
  406. /**
  407. * Implements hook_features_revert().
  408. */
  409. function field_features_revert($module) {
  410. field_features_rebuild($module);
  411. }
  412. /**
  413. * Implements of hook_features_rebuild().
  414. * Rebuilds fields from code defaults.
  415. */
  416. function field_features_rebuild($module) {
  417. if ($fields = features_get_default('field', $module)) {
  418. field_info_cache_clear();
  419. // Load all the existing fields and instance up-front so that we don't
  420. // have to rebuild the cache all the time.
  421. $existing_fields = field_info_fields();
  422. $existing_instances = field_info_instances();
  423. foreach ($fields as $field) {
  424. // Create or update field.
  425. $field_config = $field['field_config'];
  426. if (isset($existing_fields[$field_config['field_name']])) {
  427. $existing_field = $existing_fields[$field_config['field_name']];
  428. if ($field_config + $existing_field !== $existing_field) {
  429. try {
  430. field_update_field($field_config);
  431. }
  432. catch (FieldException $e) {
  433. watchdog('features', 'Attempt to update field %label failed: %message', array('%label' => $field_config['field_name'], '%message' => $e->getMessage()), WATCHDOG_ERROR);
  434. }
  435. }
  436. }
  437. else {
  438. try {
  439. field_create_field($field_config);
  440. }
  441. catch (FieldException $e) {
  442. watchdog('features', 'Attempt to create field %label failed: %message', array('%label' => $field_config['field_name'], '%message' => $e->getMessage()), WATCHDOG_ERROR);
  443. }
  444. $existing_fields[$field_config['field_name']] = $field_config;
  445. }
  446. // Create or update field instance.
  447. $field_instance = $field['field_instance'];
  448. if (isset($existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']])) {
  449. $existing_instance = $existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']];
  450. if ($field_instance + $existing_instance !== $existing_instance) {
  451. field_update_instance($field_instance);
  452. }
  453. }
  454. else {
  455. field_create_instance($field_instance);
  456. $existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']] = $field_instance;
  457. }
  458. }
  459. if ($fields) {
  460. variable_set('menu_rebuild_needed', TRUE);
  461. }
  462. }
  463. }
  464. /**
  465. * Load a field's configuration and instance configuration by an
  466. * entity_type-bundle-field_name identifier.
  467. */
  468. function features_field_load($identifier) {
  469. list($entity_type, $bundle, $field_name) = explode('-', $identifier);
  470. $field_info = field_info_field($field_name);
  471. $instance_info = field_info_instance($entity_type, $field_name, $bundle);
  472. if ($field_info && $instance_info) {
  473. unset($field_info['id']);
  474. unset($field_info['bundles']);
  475. unset($instance_info['id']);
  476. unset($instance_info['field_id']);
  477. return array(
  478. 'field_config' => $field_info,
  479. 'field_instance' => $instance_info,
  480. );
  481. }
  482. return FALSE;
  483. }