field.install 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the field module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function field_schema() {
  10. // Static (meta) tables.
  11. $schema['field_config'] = array(
  12. 'fields' => array(
  13. 'id' => array(
  14. 'type' => 'serial',
  15. 'not null' => TRUE,
  16. 'description' => 'The primary identifier for a field',
  17. ),
  18. 'field_name' => array(
  19. 'type' => 'varchar',
  20. 'length' => 32,
  21. 'not null' => TRUE,
  22. 'description' => 'The name of this field. Non-deleted field names are unique, but multiple deleted fields can have the same name.',
  23. ),
  24. 'type' => array(
  25. 'type' => 'varchar',
  26. 'length' => 128,
  27. 'not null' => TRUE,
  28. 'description' => 'The type of this field.',
  29. ),
  30. 'module' => array(
  31. 'type' => 'varchar',
  32. 'length' => 128,
  33. 'not null' => TRUE,
  34. 'default' => '',
  35. 'description' => 'The module that implements the field type.',
  36. ),
  37. 'active' => array(
  38. 'type' => 'int',
  39. 'size' => 'tiny',
  40. 'not null' => TRUE,
  41. 'default' => 0,
  42. 'description' => 'Boolean indicating whether the module that implements the field type is enabled.',
  43. ),
  44. 'storage_type' => array(
  45. 'type' => 'varchar',
  46. 'length' => 128,
  47. 'not null' => TRUE,
  48. 'description' => 'The storage backend for the field.',
  49. ),
  50. 'storage_module' => array(
  51. 'type' => 'varchar',
  52. 'length' => 128,
  53. 'not null' => TRUE,
  54. 'default' => '',
  55. 'description' => 'The module that implements the storage backend.',
  56. ),
  57. 'storage_active' => array(
  58. 'type' => 'int',
  59. 'size' => 'tiny',
  60. 'not null' => TRUE,
  61. 'default' => 0,
  62. 'description' => 'Boolean indicating whether the module that implements the storage backend is enabled.',
  63. ),
  64. 'locked' => array(
  65. 'type' => 'int',
  66. 'size' => 'tiny',
  67. 'not null' => TRUE,
  68. 'default' => 0,
  69. 'description' => '@TODO',
  70. ),
  71. 'data' => array(
  72. 'type' => 'blob',
  73. 'size' => 'big',
  74. 'not null' => TRUE,
  75. 'serialize' => TRUE,
  76. 'description' => 'Serialized data containing the field properties that do not warrant a dedicated column.',
  77. ),
  78. 'cardinality' => array(
  79. 'type' => 'int',
  80. 'size' => 'tiny',
  81. 'not null' => TRUE,
  82. 'default' => 0,
  83. ),
  84. 'translatable' => array(
  85. 'type' => 'int',
  86. 'size' => 'tiny',
  87. 'not null' => TRUE,
  88. 'default' => 0,
  89. ),
  90. 'deleted' => array(
  91. 'type' => 'int',
  92. 'size' => 'tiny',
  93. 'not null' => TRUE,
  94. 'default' => 0,
  95. ),
  96. ),
  97. 'primary key' => array('id'),
  98. 'indexes' => array(
  99. 'field_name' => array('field_name'),
  100. // Used by field_sync_field_status().
  101. 'active' => array('active'),
  102. 'storage_active' => array('storage_active'),
  103. 'deleted' => array('deleted'),
  104. // Used by field_modules_disabled().
  105. 'module' => array('module'),
  106. 'storage_module' => array('storage_module'),
  107. 'type' => array('type'),
  108. 'storage_type' => array('storage_type'),
  109. ),
  110. );
  111. $schema['field_config_instance'] = array(
  112. 'fields' => array(
  113. 'id' => array(
  114. 'type' => 'serial',
  115. 'not null' => TRUE,
  116. 'description' => 'The primary identifier for a field instance',
  117. ),
  118. 'field_id' => array(
  119. 'type' => 'int',
  120. 'not null' => TRUE,
  121. 'description' => 'The identifier of the field attached by this instance',
  122. ),
  123. 'field_name' => array(
  124. 'type' => 'varchar',
  125. 'length' => 32,
  126. 'not null' => TRUE,
  127. 'default' => ''
  128. ),
  129. 'entity_type' => array(
  130. 'type' => 'varchar',
  131. 'length' => 32,
  132. 'not null' => TRUE,
  133. 'default' => ''
  134. ),
  135. 'bundle' => array(
  136. 'type' => 'varchar',
  137. 'length' => 128,
  138. 'not null' => TRUE,
  139. 'default' => ''
  140. ),
  141. 'data' => array(
  142. 'type' => 'blob',
  143. 'size' => 'big',
  144. 'not null' => TRUE,
  145. 'serialize' => TRUE,
  146. ),
  147. 'deleted' => array(
  148. 'type' => 'int',
  149. 'size' => 'tiny',
  150. 'not null' => TRUE,
  151. 'default' => 0,
  152. ),
  153. ),
  154. 'primary key' => array('id'),
  155. 'indexes' => array(
  156. // Used by field_delete_instance().
  157. 'field_name_bundle' => array('field_name', 'entity_type', 'bundle'),
  158. // Used by field_read_instances().
  159. 'deleted' => array('deleted'),
  160. ),
  161. );
  162. $schema['cache_field'] = drupal_get_schema_unprocessed('system', 'cache');
  163. return $schema;
  164. }
  165. /**
  166. * Utility function: create a field by writing directly to the database.
  167. *
  168. * This function can be used for databases whose schema is at field module
  169. * version 7000 or higher.
  170. *
  171. * @ingroup update_api
  172. */
  173. function _update_7000_field_create_field(&$field) {
  174. // Merge in default values.`
  175. $field += array(
  176. 'entity_types' => array(),
  177. 'cardinality' => 1,
  178. 'translatable' => FALSE,
  179. 'locked' => FALSE,
  180. 'settings' => array(),
  181. 'indexes' => array(),
  182. 'deleted' => 0,
  183. 'active' => 1,
  184. );
  185. // Set storage.
  186. $field['storage'] = array(
  187. 'type' => 'field_sql_storage',
  188. 'settings' => array(),
  189. 'module' => 'field_sql_storage',
  190. 'active' => 1,
  191. );
  192. // Fetch the field schema to initialize columns and indexes. The field module
  193. // is not guaranteed to be loaded at this point.
  194. module_load_install($field['module']);
  195. $schema = (array) module_invoke($field['module'], 'field_schema', $field);
  196. $schema += array('columns' => array(), 'indexes' => array());
  197. // 'columns' are hardcoded in the field type.
  198. $field['columns'] = $schema['columns'];
  199. // 'indexes' can be both hardcoded in the field type, and specified in the
  200. // incoming $field definition.
  201. $field['indexes'] += $schema['indexes'];
  202. // The serialized 'data' column contains everything from $field that does not
  203. // have its own column and is not automatically populated when the field is
  204. // read.
  205. $data = $field;
  206. unset($data['columns'], $data['field_name'], $data['type'], $data['active'], $data['module'], $data['storage_type'], $data['storage_active'], $data['storage_module'], $data['locked'], $data['cardinality'], $data['deleted']);
  207. // Additionally, do not save the 'bundles' property populated by
  208. // field_info_field().
  209. unset($data['bundles']);
  210. // Write the field to the database.
  211. $record = array(
  212. 'field_name' => $field['field_name'],
  213. 'type' => $field['type'],
  214. 'module' => $field['module'],
  215. 'active' => (int) $field['active'],
  216. 'storage_type' => $field['storage']['type'],
  217. 'storage_module' => $field['storage']['module'],
  218. 'storage_active' => (int) $field['storage']['active'],
  219. 'locked' => (int) $field['locked'],
  220. 'data' => serialize($data),
  221. 'cardinality' => $field['cardinality'],
  222. 'translatable' => (int) $field['translatable'],
  223. 'deleted' => (int) $field['deleted'],
  224. );
  225. // We don't use drupal_write_record() here because it depends on the schema.
  226. $field['id'] = db_insert('field_config')
  227. ->fields($record)
  228. ->execute();
  229. // Create storage for the field.
  230. field_sql_storage_field_storage_create_field($field);
  231. }
  232. /**
  233. * Utility function: delete a field stored in SQL storage directly from the database.
  234. *
  235. * To protect user data, this function can only be used to delete fields once
  236. * all information it stored is gone. Delete all data from the
  237. * field_data_$field_name table before calling by either manually issuing
  238. * delete queries against it or using _update_7000_field_delete_instance().
  239. *
  240. * This function can be used for databases whose schema is at field module
  241. * version 7000 or higher.
  242. *
  243. * @param $field_name
  244. * The field name to delete.
  245. *
  246. * @ingroup update_api
  247. */
  248. function _update_7000_field_delete_field($field_name) {
  249. $table_name = 'field_data_' . $field_name;
  250. if (db_select($table_name)->range(0, 1)->countQuery()->execute()->fetchField()) {
  251. $t = get_t();
  252. throw new Exception($t('This function can only be used to delete fields without data'));
  253. }
  254. // Delete all instances.
  255. db_delete('field_config_instance')
  256. ->condition('field_name', $field_name)
  257. ->execute();
  258. // Nuke field data and revision tables.
  259. db_drop_table($table_name);
  260. db_drop_table('field_revision_' . $field_name);
  261. // Delete the field.
  262. db_delete('field_config')
  263. ->condition('field_name', $field_name)
  264. ->execute();
  265. }
  266. /**
  267. * Utility function: delete an instance and all its data of a field stored in SQL Storage.
  268. *
  269. * BEWARE: this function deletes user data from the field storage tables.
  270. *
  271. * This function is valid for a database schema version 7000.
  272. *
  273. * @ingroup update_api
  274. */
  275. function _update_7000_field_delete_instance($field_name, $entity_type, $bundle) {
  276. // Delete field instance configuration data.
  277. db_delete('field_config_instance')
  278. ->condition('field_name', $field_name)
  279. ->condition('entity_type', $entity_type)
  280. ->condition('bundle', $bundle)
  281. ->execute();
  282. // Nuke data.
  283. db_delete('field_data_' . $field_name)
  284. ->condition('entity_type', $entity_type)
  285. ->condition('bundle', $bundle)
  286. ->execute();
  287. db_delete('field_revision_' . $field_name)
  288. ->condition('entity_type', $entity_type)
  289. ->condition('bundle', $bundle)
  290. ->execute();
  291. }
  292. /**
  293. * Utility function: fetch all the field definitions from the database.
  294. *
  295. * Warning: unlike the field_read_fields() API function, this function returns
  296. * all fields by default, including deleted and inactive fields, unless
  297. * specified otherwise in the $conditions parameter.
  298. *
  299. * @param $conditions
  300. * An array of conditions to limit the select query to.
  301. * @param $key
  302. * The name of the field property the return array is indexed by. Using
  303. * anything else than 'id' might cause incomplete results if the $conditions
  304. * do not filter out deleted fields.
  305. *
  306. * @return
  307. * An array of fields matching $conditions, keyed by the property specified
  308. * by the $key parameter.
  309. *
  310. * @ingroup update_api
  311. */
  312. function _update_7000_field_read_fields(array $conditions = array(), $key = 'id') {
  313. $fields = array();
  314. $query = db_select('field_config', 'fc', array('fetch' => PDO::FETCH_ASSOC))
  315. ->fields('fc');
  316. foreach ($conditions as $column => $value) {
  317. $query->condition($column, $value);
  318. }
  319. foreach ($query->execute() as $record) {
  320. $field = unserialize($record['data']);
  321. $field['id'] = $record['id'];
  322. $field['field_name'] = $record['field_name'];
  323. $field['type'] = $record['type'];
  324. $field['module'] = $record['module'];
  325. $field['active'] = $record['active'];
  326. $field['storage']['type'] = $record['storage_type'];
  327. $field['storage']['module'] = $record['storage_module'];
  328. $field['storage']['active'] = $record['storage_active'];
  329. $field['locked'] = $record['locked'];
  330. $field['cardinality'] = $record['cardinality'];
  331. $field['translatable'] = $record['translatable'];
  332. $field['deleted'] = $record['deleted'];
  333. $fields[$field[$key]] = $field;
  334. }
  335. return $fields;
  336. }
  337. /**
  338. * Utility function: write a field instance directly to the database.
  339. *
  340. * This function can be used for databases whose schema is at field module
  341. * version 7000 or higher.
  342. *
  343. * @ingroup update_api
  344. */
  345. function _update_7000_field_create_instance($field, &$instance) {
  346. // Merge in defaults.
  347. $instance += array(
  348. 'field_id' => $field['id'],
  349. 'field_name' => $field['field_name'],
  350. 'deleted' => 0,
  351. );
  352. // The serialized 'data' column contains everything from $instance that does
  353. // not have its own column and is not automatically populated when the
  354. // instance is read.
  355. $data = $instance;
  356. unset($data['id'], $data['field_id'], $data['field_name'], $data['entity_type'], $data['bundle'], $data['deleted']);
  357. $record = array(
  358. 'field_id' => $instance['field_id'],
  359. 'field_name' => $instance['field_name'],
  360. 'entity_type' => $instance['entity_type'],
  361. 'bundle' => $instance['bundle'],
  362. 'data' => serialize($data),
  363. 'deleted' => (int) $instance['deleted'],
  364. );
  365. $instance['id'] = db_insert('field_config_instance')
  366. ->fields($record)
  367. ->execute();
  368. }
  369. /**
  370. * @addtogroup updates-6.x-to-7.x
  371. * @{
  372. */
  373. /**
  374. * Field update version placeholder.
  375. */
  376. function field_update_7000() {
  377. // Some update helper functions (such as _update_7000_field_create_field())
  378. // modify the database directly. They can be used safely only if the database
  379. // schema matches the field module schema established for Drupal 7.0 (i.e.
  380. // version 7000). This function exists solely to set the schema version to
  381. // 7000, so that update functions calling those helpers can do so safely
  382. // by declaring a dependency on field_update_7000().
  383. }
  384. /**
  385. * Fix fields definitions created during the d6 to d7 upgrade path.
  386. */
  387. function field_update_7001() {
  388. $fields = _update_7000_field_read_fields();
  389. foreach ($fields as $field) {
  390. // _update_7000_field_create_field() was broken in d7 RC2, and the fields
  391. // created during a d6 to d7 upgrade do not correcly store the 'index'
  392. // entry. See http://drupal.org/node/996160.
  393. module_load_install($field['module']);
  394. $schema = (array) module_invoke($field['module'], 'field_schema', $field);
  395. $schema += array('indexes' => array());
  396. // 'indexes' can be both hardcoded in the field type, and specified in the
  397. // incoming $field definition.
  398. $field['indexes'] += $schema['indexes'];
  399. // Place the updated entries in the existing serialized 'data' column.
  400. $data = db_query("SELECT data FROM {field_config} WHERE id = :id", array(':id' => $field['id']))->fetchField();
  401. $data = unserialize($data);
  402. $data['columns'] = $field['columns'];
  403. $data['indexes'] = $field['indexes'];
  404. // Save the new data.
  405. $query = db_update('field_config')
  406. ->condition('id', $field['id'])
  407. ->fields(array('data' => serialize($data)))
  408. ->execute();
  409. }
  410. }
  411. /**
  412. * @} End of "addtogroup updates-6.x-to-7.x".
  413. */
  414. /**
  415. * @addtogroup updates-7.x-extra
  416. * @{
  417. */
  418. /**
  419. * Split the all-inclusive field_bundle_settings variable per bundle.
  420. */
  421. function field_update_7002() {
  422. $settings = variable_get('field_bundle_settings', array());
  423. if ($settings) {
  424. foreach ($settings as $entity_type => $entity_type_settings) {
  425. foreach ($entity_type_settings as $bundle => $bundle_settings) {
  426. variable_set('field_bundle_settings_' . $entity_type . '__' . $bundle, $bundle_settings);
  427. }
  428. }
  429. variable_del('field_bundle_settings');
  430. }
  431. }
  432. /**
  433. * @} End of "addtogroup updates-7.x-extra".
  434. */