features.field.inc 19 KB

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