features.field.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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['foreign keys']);
  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_prefix = ' // Exported field_base: ';
  159. $field_identifier = features_var_export($identifier);
  160. if (features_field_export_needs_wrap($field_prefix, $field_identifier)) {
  161. $code[] = rtrim($field_prefix);
  162. $code[] = " // {$field_identifier}";
  163. }
  164. else {
  165. $code[] = $field_prefix . $field_identifier;
  166. }
  167. $code[] = " \$field_bases[{$field_identifier}] = {$field_export};";
  168. $code[] = "";
  169. }
  170. }
  171. $code[] = ' return $field_bases;';
  172. $code = implode("\n", $code);
  173. return array('field_default_field_bases' => $code);
  174. }
  175. /**
  176. * Implements hook_features_export_render().
  177. */
  178. function field_instance_features_export_render($module, $data, $export = NULL) {
  179. $translatables = $code = array();
  180. $code[] = ' $field_instances = array();';
  181. $code[] = '';
  182. foreach ($data as $identifier) {
  183. if ($instance = features_field_instance_load($identifier)) {
  184. _field_instance_features_export_sort($instance);
  185. $field_export = features_var_export($instance, ' ');
  186. $instance_prefix = ' // Exported field_instance: ';
  187. $instance_identifier = features_var_export($identifier);
  188. if (features_field_export_needs_wrap($instance_prefix, $instance_identifier)) {
  189. $code[] = rtrim($instance_prefix);
  190. $code[] = " // {$instance_identifier}";
  191. }
  192. else {
  193. $code[] = $instance_prefix . $instance_identifier;
  194. }
  195. $code[] = " \$field_instances[{$instance_identifier}] = {$field_export};";
  196. $code[] = "";
  197. if (!empty($instance['label'])) {
  198. $translatables[] = $instance['label'];
  199. }
  200. if (!empty($instance['description'])) {
  201. $translatables[] = $instance['description'];
  202. }
  203. }
  204. }
  205. if (!empty($translatables)) {
  206. $code[] = features_translatables_export($translatables, ' ');
  207. }
  208. $code[] = ' return $field_instances;';
  209. $code = implode("\n", $code);
  210. return array('field_default_field_instances' => $code);
  211. }
  212. // Helper to enforce consistency in field export arrays.
  213. function _field_instance_features_export_sort(&$field, $sort = TRUE) {
  214. // Some arrays are not sorted to preserve order (for example allowed_values).
  215. static $sort_blacklist = array(
  216. 'allowed_values',
  217. 'format_handlers',
  218. );
  219. if ($sort) {
  220. uksort($field, 'strnatcmp');
  221. }
  222. foreach ($field as $k => $v) {
  223. if (is_array($v)) {
  224. _field_instance_features_export_sort($field[$k], !in_array($k, $sort_blacklist));
  225. }
  226. }
  227. }
  228. /**
  229. * Implements hook_features_revert().
  230. */
  231. function field_base_features_revert($module) {
  232. field_base_features_rebuild($module);
  233. }
  234. /**
  235. * Implements hook_features_revert().
  236. */
  237. function field_instance_features_revert($module) {
  238. field_instance_features_rebuild($module);
  239. }
  240. /**
  241. * Implements of hook_features_rebuild().
  242. * Rebuilds fields from code defaults.
  243. */
  244. function field_base_features_rebuild($module) {
  245. if ($fields = features_get_default('field_base', $module)) {
  246. field_info_cache_clear();
  247. // Load all the existing field bases up-front so that we don't
  248. // have to rebuild the cache all the time.
  249. $existing_fields = field_info_fields();
  250. foreach ($fields as $field) {
  251. // Create or update field.
  252. if (isset($existing_fields[$field['field_name']])) {
  253. $existing_field = $existing_fields[$field['field_name']];
  254. if ($field + $existing_field !== $existing_field) {
  255. field_update_field($field);
  256. }
  257. }
  258. else {
  259. field_create_field($field);
  260. $existing_fields[$field['field_name']] = $field;
  261. }
  262. variable_set('menu_rebuild_needed', TRUE);
  263. }
  264. }
  265. }
  266. /**
  267. * Implements of hook_features_rebuild().
  268. * Rebuilds field instances from code defaults.
  269. */
  270. function field_instance_features_rebuild($module) {
  271. if ($instances = features_get_default('field_instance', $module)) {
  272. field_info_cache_clear();
  273. // Load all the existing instances up-front so that we don't
  274. // have to rebuild the cache all the time.
  275. $existing_instances = field_info_instances();
  276. foreach ($instances as $field_instance) {
  277. // If the field base information does not exist yet, cancel out.
  278. if (!field_info_field($field_instance['field_name'])) {
  279. continue;
  280. }
  281. // Create or update field instance.
  282. if (isset($existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']])) {
  283. $existing_instance = $existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']];
  284. if ($field_instance + $existing_instance !== $existing_instance) {
  285. try {
  286. field_update_instance($field_instance);
  287. }
  288. catch (FieldException $e) {
  289. 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);
  290. }
  291. }
  292. }
  293. else {
  294. try {
  295. field_create_instance($field_instance);
  296. }
  297. catch (FieldException $e) {
  298. 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);
  299. }
  300. $existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']] = $field_instance;
  301. }
  302. }
  303. if ($instances) {
  304. variable_set('menu_rebuild_needed', TRUE);
  305. }
  306. }
  307. }
  308. /**
  309. * Load a field base configuration by a field_name identifier.
  310. */
  311. function features_field_base_load($field_name) {
  312. if ($field_info = field_info_field($field_name)) {
  313. unset($field_info['id']);
  314. unset($field_info['bundles']);
  315. return $field_info;
  316. }
  317. return FALSE;
  318. }
  319. /**
  320. * Load a field's instance configuration by an entity_type-bundle-field_name
  321. * identifier.
  322. */
  323. function features_field_instance_load($identifier) {
  324. list($entity_type, $bundle, $field_name) = explode('-', $identifier);
  325. if ($instance_info = field_info_instance($entity_type, $field_name, $bundle)) {
  326. unset($instance_info['id']);
  327. unset($instance_info['field_id']);
  328. return $instance_info;
  329. }
  330. return FALSE;
  331. }
  332. /* ----- DEPRECATED FIELD EXPORT -----
  333. * keep this code for backward compatibility with older exports
  334. * until v3.x
  335. */
  336. /**
  337. * Implements hook_features_export_options().
  338. */
  339. function field_features_export_options() {
  340. $options = array();
  341. $instances = field_info_instances();
  342. foreach ($instances as $entity_type => $bundles) {
  343. foreach ($bundles as $bundle => $fields) {
  344. foreach ($fields as $field) {
  345. $identifier = "{$entity_type}-{$bundle}-{$field['field_name']}";
  346. $options[$identifier] = $identifier;
  347. }
  348. }
  349. }
  350. return $options;
  351. }
  352. /**
  353. * Implements hook_features_export().
  354. */
  355. function field_features_export($data, &$export, $module_name = '') {
  356. $pipe = array();
  357. // Convert 'field' to 'field_instance' on features-update.
  358. $pipe['field_instance'] = $data;
  359. return $pipe;
  360. }
  361. /**
  362. * Implements hook_features_export_render().
  363. */
  364. function field_features_export_render($module, $data, $export = NULL) {
  365. $translatables = $code = array();
  366. $code[] = ' $fields = array();';
  367. $code[] = '';
  368. foreach ($data as $identifier) {
  369. if ($field = features_field_load($identifier)) {
  370. unset($field['field_config']['columns']);
  371. // Only remove the 'storage' declaration if the field is using the default
  372. // storage type.
  373. if ($field['field_config']['storage']['type'] == variable_get('field_storage_default', 'field_sql_storage')) {
  374. unset($field['field_config']['storage']);
  375. }
  376. // If we still have a storage declaration here it means that a non-default
  377. // storage type was altered into to the field definition. And noone would
  378. // never need to change the 'details' key, so don't render it.
  379. if (isset($field['field_config']['storage']['details'])) {
  380. unset($field['field_config']['storage']['details']);
  381. }
  382. _field_features_export_sort($field);
  383. $field_export = features_var_export($field, ' ');
  384. $field_identifier = features_var_export($identifier);
  385. $code[] = " // Exported field: {$field_identifier}.";
  386. $code[] = " \$fields[{$field_identifier}] = {$field_export};";
  387. $code[] = "";
  388. // Add label and description to translatables array.
  389. if (!empty($field['field_instance']['label'])) {
  390. $translatables[] = $field['field_instance']['label'];
  391. }
  392. if (!empty($field['field_instance']['description'])) {
  393. $translatables[] = $field['field_instance']['description'];
  394. }
  395. }
  396. }
  397. if (!empty($translatables)) {
  398. $code[] = features_translatables_export($translatables, ' ');
  399. }
  400. $code[] = ' return $fields;';
  401. $code = implode("\n", $code);
  402. return array('field_default_fields' => $code);
  403. }
  404. // Helper to enforce consistency in field export arrays.
  405. function _field_features_export_sort(&$field, $sort = TRUE) {
  406. // Some arrays are not sorted to preserve order (for example allowed_values).
  407. static $sort_blacklist = array(
  408. 'allowed_values',
  409. 'format_handlers',
  410. );
  411. if ($sort) {
  412. ksort($field);
  413. }
  414. foreach ($field as $k => $v) {
  415. if (is_array($v)) {
  416. _field_features_export_sort($field[$k], !in_array($k, $sort_blacklist));
  417. }
  418. }
  419. }
  420. /**
  421. * Implements hook_features_revert().
  422. */
  423. function field_features_revert($module) {
  424. field_features_rebuild($module);
  425. }
  426. /**
  427. * Implements of hook_features_rebuild().
  428. * Rebuilds fields from code defaults.
  429. */
  430. function field_features_rebuild($module) {
  431. if ($fields = features_get_default('field', $module)) {
  432. field_info_cache_clear();
  433. // Load all the existing fields and instance up-front so that we don't
  434. // have to rebuild the cache all the time.
  435. $existing_fields = field_info_fields();
  436. $existing_instances = field_info_instances();
  437. foreach ($fields as $field) {
  438. // Create or update field.
  439. $field_config = $field['field_config'];
  440. if (isset($existing_fields[$field_config['field_name']])) {
  441. $existing_field = $existing_fields[$field_config['field_name']];
  442. if ($field_config + $existing_field !== $existing_field) {
  443. try {
  444. field_update_field($field_config);
  445. }
  446. catch (FieldException $e) {
  447. watchdog('features', 'Attempt to update field %label failed: %message', array('%label' => $field_config['field_name'], '%message' => $e->getMessage()), WATCHDOG_ERROR);
  448. }
  449. }
  450. }
  451. else {
  452. try {
  453. field_create_field($field_config);
  454. }
  455. catch (FieldException $e) {
  456. watchdog('features', 'Attempt to create field %label failed: %message', array('%label' => $field_config['field_name'], '%message' => $e->getMessage()), WATCHDOG_ERROR);
  457. }
  458. $existing_fields[$field_config['field_name']] = $field_config;
  459. }
  460. // Create or update field instance.
  461. $field_instance = $field['field_instance'];
  462. if (isset($existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']])) {
  463. $existing_instance = $existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']];
  464. if ($field_instance + $existing_instance !== $existing_instance) {
  465. field_update_instance($field_instance);
  466. }
  467. }
  468. else {
  469. field_create_instance($field_instance);
  470. $existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']] = $field_instance;
  471. }
  472. }
  473. if ($fields) {
  474. variable_set('menu_rebuild_needed', TRUE);
  475. }
  476. }
  477. }
  478. /**
  479. * Load a field's configuration and instance configuration by an
  480. * entity_type-bundle-field_name identifier.
  481. */
  482. function features_field_load($identifier) {
  483. list($entity_type, $bundle, $field_name) = explode('-', $identifier);
  484. $field_info = field_info_field($field_name);
  485. $instance_info = field_info_instance($entity_type, $field_name, $bundle);
  486. if ($field_info && $instance_info) {
  487. unset($field_info['id']);
  488. unset($field_info['bundles']);
  489. unset($instance_info['id']);
  490. unset($instance_info['field_id']);
  491. return array(
  492. 'field_config' => $field_info,
  493. 'field_instance' => $instance_info,
  494. );
  495. }
  496. return FALSE;
  497. }
  498. /**
  499. * Determine if a field export line needs to be wrapped.
  500. *
  501. * Drupal code standards specify that comments should wrap at 80 characters or
  502. * less.
  503. *
  504. * @param string $prefix
  505. * The prefix to be exported before the field identifier.
  506. * @param string $identifier
  507. * The field identifier.
  508. *
  509. * @return BOOL
  510. * TRUE if the line should be wrapped after the prefix, else FALSE.
  511. *
  512. * @see https://www.drupal.org/node/1354
  513. */
  514. function features_field_export_needs_wrap($prefix, $identifier) {
  515. return (strlen($prefix) + strlen($identifier) > 80);
  516. }