features.field.inc 20 KB

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