field.install 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. $schema['cache_field']['description'] = 'Cache table for the Field module to store already built field information.';
  164. return $schema;
  165. }
  166. /**
  167. * Utility function: create a field by writing directly to the database.
  168. *
  169. * This function can be used for databases whose schema is at field module
  170. * version 7000 or higher.
  171. *
  172. * @ingroup update_api
  173. */
  174. function _update_7000_field_create_field(&$field) {
  175. // Merge in default values.`
  176. $field += array(
  177. 'entity_types' => array(),
  178. 'cardinality' => 1,
  179. 'translatable' => FALSE,
  180. 'locked' => FALSE,
  181. 'settings' => array(),
  182. 'indexes' => array(),
  183. 'deleted' => 0,
  184. 'active' => 1,
  185. );
  186. // Set storage.
  187. $field['storage'] = array(
  188. 'type' => 'field_sql_storage',
  189. 'settings' => array(),
  190. 'module' => 'field_sql_storage',
  191. 'active' => 1,
  192. );
  193. // Fetch the field schema to initialize columns and indexes. The field module
  194. // is not guaranteed to be loaded at this point.
  195. module_load_install($field['module']);
  196. $schema = (array) module_invoke($field['module'], 'field_schema', $field);
  197. $schema += array('columns' => array(), 'indexes' => array());
  198. // 'columns' are hardcoded in the field type.
  199. $field['columns'] = $schema['columns'];
  200. // 'indexes' can be both hardcoded in the field type, and specified in the
  201. // incoming $field definition.
  202. $field['indexes'] += $schema['indexes'];
  203. // The serialized 'data' column contains everything from $field that does not
  204. // have its own column and is not automatically populated when the field is
  205. // read.
  206. $data = $field;
  207. 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']);
  208. // Additionally, do not save the 'bundles' property populated by
  209. // field_info_field().
  210. unset($data['bundles']);
  211. // Write the field to the database.
  212. $record = array(
  213. 'field_name' => $field['field_name'],
  214. 'type' => $field['type'],
  215. 'module' => $field['module'],
  216. 'active' => (int) $field['active'],
  217. 'storage_type' => $field['storage']['type'],
  218. 'storage_module' => $field['storage']['module'],
  219. 'storage_active' => (int) $field['storage']['active'],
  220. 'locked' => (int) $field['locked'],
  221. 'data' => serialize($data),
  222. 'cardinality' => $field['cardinality'],
  223. 'translatable' => (int) $field['translatable'],
  224. 'deleted' => (int) $field['deleted'],
  225. );
  226. // We don't use drupal_write_record() here because it depends on the schema.
  227. $field['id'] = db_insert('field_config')
  228. ->fields($record)
  229. ->execute();
  230. // Create storage for the field.
  231. field_sql_storage_field_storage_create_field($field);
  232. }
  233. /**
  234. * Utility function: delete a field stored in SQL storage directly from the database.
  235. *
  236. * To protect user data, this function can only be used to delete fields once
  237. * all information it stored is gone. Delete all data from the
  238. * field_data_$field_name table before calling by either manually issuing
  239. * delete queries against it or using _update_7000_field_delete_instance().
  240. *
  241. * This function can be used for databases whose schema is at field module
  242. * version 7000 or higher.
  243. *
  244. * @param $field_name
  245. * The field name to delete.
  246. *
  247. * @ingroup update_api
  248. */
  249. function _update_7000_field_delete_field($field_name) {
  250. $table_name = 'field_data_' . $field_name;
  251. if (db_select($table_name)->range(0, 1)->countQuery()->execute()->fetchField()) {
  252. $t = get_t();
  253. throw new Exception($t('This function can only be used to delete fields without data'));
  254. }
  255. // Delete all instances.
  256. db_delete('field_config_instance')
  257. ->condition('field_name', $field_name)
  258. ->execute();
  259. // Nuke field data and revision tables.
  260. db_drop_table($table_name);
  261. db_drop_table('field_revision_' . $field_name);
  262. // Delete the field.
  263. db_delete('field_config')
  264. ->condition('field_name', $field_name)
  265. ->execute();
  266. }
  267. /**
  268. * Utility function: delete an instance and all its data of a field stored in SQL Storage.
  269. *
  270. * BEWARE: this function deletes user data from the field storage tables.
  271. *
  272. * This function is valid for a database schema version 7000.
  273. *
  274. * @ingroup update_api
  275. */
  276. function _update_7000_field_delete_instance($field_name, $entity_type, $bundle) {
  277. // Delete field instance configuration data.
  278. db_delete('field_config_instance')
  279. ->condition('field_name', $field_name)
  280. ->condition('entity_type', $entity_type)
  281. ->condition('bundle', $bundle)
  282. ->execute();
  283. // Nuke data.
  284. db_delete('field_data_' . $field_name)
  285. ->condition('entity_type', $entity_type)
  286. ->condition('bundle', $bundle)
  287. ->execute();
  288. db_delete('field_revision_' . $field_name)
  289. ->condition('entity_type', $entity_type)
  290. ->condition('bundle', $bundle)
  291. ->execute();
  292. }
  293. /**
  294. * Utility function: fetch all the field definitions from the database.
  295. *
  296. * Warning: unlike the field_read_fields() API function, this function returns
  297. * all fields by default, including deleted and inactive fields, unless
  298. * specified otherwise in the $conditions parameter.
  299. *
  300. * @param $conditions
  301. * An array of conditions to limit the select query to.
  302. * @param $key
  303. * The name of the field property the return array is indexed by. Using
  304. * anything else than 'id' might cause incomplete results if the $conditions
  305. * do not filter out deleted fields.
  306. *
  307. * @return
  308. * An array of fields matching $conditions, keyed by the property specified
  309. * by the $key parameter.
  310. *
  311. * @ingroup update_api
  312. */
  313. function _update_7000_field_read_fields(array $conditions = array(), $key = 'id') {
  314. $fields = array();
  315. $query = db_select('field_config', 'fc', array('fetch' => PDO::FETCH_ASSOC))
  316. ->fields('fc');
  317. foreach ($conditions as $column => $value) {
  318. $query->condition($column, $value);
  319. }
  320. foreach ($query->execute() as $record) {
  321. $field = unserialize($record['data']);
  322. $field['id'] = $record['id'];
  323. $field['field_name'] = $record['field_name'];
  324. $field['type'] = $record['type'];
  325. $field['module'] = $record['module'];
  326. $field['active'] = $record['active'];
  327. $field['storage']['type'] = $record['storage_type'];
  328. $field['storage']['module'] = $record['storage_module'];
  329. $field['storage']['active'] = $record['storage_active'];
  330. $field['locked'] = $record['locked'];
  331. $field['cardinality'] = $record['cardinality'];
  332. $field['translatable'] = $record['translatable'];
  333. $field['deleted'] = $record['deleted'];
  334. $fields[$field[$key]] = $field;
  335. }
  336. return $fields;
  337. }
  338. /**
  339. * Utility function: write a field instance directly to the database.
  340. *
  341. * This function can be used for databases whose schema is at field module
  342. * version 7000 or higher.
  343. *
  344. * @ingroup update_api
  345. */
  346. function _update_7000_field_create_instance($field, &$instance) {
  347. // Merge in defaults.
  348. $instance += array(
  349. 'field_id' => $field['id'],
  350. 'field_name' => $field['field_name'],
  351. 'deleted' => 0,
  352. );
  353. // The serialized 'data' column contains everything from $instance that does
  354. // not have its own column and is not automatically populated when the
  355. // instance is read.
  356. $data = $instance;
  357. unset($data['id'], $data['field_id'], $data['field_name'], $data['entity_type'], $data['bundle'], $data['deleted']);
  358. $record = array(
  359. 'field_id' => $instance['field_id'],
  360. 'field_name' => $instance['field_name'],
  361. 'entity_type' => $instance['entity_type'],
  362. 'bundle' => $instance['bundle'],
  363. 'data' => serialize($data),
  364. 'deleted' => (int) $instance['deleted'],
  365. );
  366. $instance['id'] = db_insert('field_config_instance')
  367. ->fields($record)
  368. ->execute();
  369. }
  370. /**
  371. * @addtogroup updates-6.x-to-7.x
  372. * @{
  373. */
  374. /**
  375. * Field update version placeholder.
  376. */
  377. function field_update_7000() {
  378. // Some update helper functions (such as _update_7000_field_create_field())
  379. // modify the database directly. They can be used safely only if the database
  380. // schema matches the field module schema established for Drupal 7.0 (i.e.
  381. // version 7000). This function exists solely to set the schema version to
  382. // 7000, so that update functions calling those helpers can do so safely
  383. // by declaring a dependency on field_update_7000().
  384. }
  385. /**
  386. * Fix fields definitions created during the d6 to d7 upgrade path.
  387. */
  388. function field_update_7001() {
  389. $fields = _update_7000_field_read_fields();
  390. foreach ($fields as $field) {
  391. // _update_7000_field_create_field() was broken in d7 RC2, and the fields
  392. // created during a d6 to d7 upgrade do not correcly store the 'index'
  393. // entry. See http://drupal.org/node/996160.
  394. module_load_install($field['module']);
  395. $schema = (array) module_invoke($field['module'], 'field_schema', $field);
  396. $schema += array('indexes' => array());
  397. // 'indexes' can be both hardcoded in the field type, and specified in the
  398. // incoming $field definition.
  399. $field['indexes'] += $schema['indexes'];
  400. // Place the updated entries in the existing serialized 'data' column.
  401. $data = db_query("SELECT data FROM {field_config} WHERE id = :id", array(':id' => $field['id']))->fetchField();
  402. $data = unserialize($data);
  403. $data['columns'] = $field['columns'];
  404. $data['indexes'] = $field['indexes'];
  405. // Save the new data.
  406. $query = db_update('field_config')
  407. ->condition('id', $field['id'])
  408. ->fields(array('data' => serialize($data)))
  409. ->execute();
  410. }
  411. }
  412. /**
  413. * @} End of "addtogroup updates-6.x-to-7.x".
  414. */
  415. /**
  416. * @addtogroup updates-7.x-extra
  417. * @{
  418. */
  419. /**
  420. * Split the all-inclusive field_bundle_settings variable per bundle.
  421. */
  422. function field_update_7002() {
  423. $settings = variable_get('field_bundle_settings', array());
  424. if ($settings) {
  425. foreach ($settings as $entity_type => $entity_type_settings) {
  426. foreach ($entity_type_settings as $bundle => $bundle_settings) {
  427. variable_set('field_bundle_settings_' . $entity_type . '__' . $bundle, $bundle_settings);
  428. }
  429. }
  430. variable_del('field_bundle_settings');
  431. }
  432. }
  433. /**
  434. * Add the FieldInfo class to the class registry.
  435. */
  436. function field_update_7003() {
  437. // Empty update to force a rebuild of the registry.
  438. }
  439. /**
  440. * Grant the new "administer fields" permission to trusted users.
  441. */
  442. function field_update_7004() {
  443. // Assign the permission to anyone that already has a trusted core permission
  444. // that would have previously let them administer fields on an entity type.
  445. $rids = array();
  446. $permissions = array(
  447. 'administer site configuration',
  448. 'administer content types',
  449. 'administer users',
  450. );
  451. foreach ($permissions as $permission) {
  452. $rids = array_merge($rids, array_keys(user_roles(FALSE, $permission)));
  453. }
  454. $rids = array_unique($rids);
  455. foreach ($rids as $rid) {
  456. _update_7000_user_role_grant_permissions($rid, array('administer fields'), 'field');
  457. }
  458. }
  459. /**
  460. * @} End of "addtogroup updates-7.x-extra".
  461. */