field.crud.inc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. <?php
  2. /**
  3. * @file
  4. * Field CRUD API, handling field and field instance creation and deletion.
  5. */
  6. /**
  7. * @defgroup field_crud Field CRUD API
  8. * @{
  9. * Create, update, and delete Field API fields, bundles, and instances.
  10. *
  11. * Modules use this API, often in hook_install(), to create custom
  12. * data structures. UI modules will use it to create a user interface.
  13. *
  14. * The Field CRUD API uses
  15. * @link field Field API data structures @endlink.
  16. *
  17. * See @link field Field API @endlink for information about the other parts of
  18. * the Field API.
  19. */
  20. /**
  21. * Creates a field.
  22. *
  23. * This function does not bind the field to any bundle; use
  24. * field_create_instance() for that.
  25. *
  26. * @param $field
  27. * A field definition array. The field_name and type properties are required.
  28. * Other properties, if omitted, will be given the following default values:
  29. * - cardinality: 1
  30. * - locked: FALSE
  31. * - indexes: the field-type indexes, specified by the field type's
  32. * hook_field_schema(). The indexes specified in $field are added
  33. * to those default indexes. It is possible to override the
  34. * definition of a field-type index by providing an index with the
  35. * same name, or to remove it by redefining it as an empty array
  36. * of columns. Overriding field-type indexes should be done
  37. * carefully, for it might seriously affect the site's performance.
  38. * - settings: each omitted setting is given the default value defined in
  39. * hook_field_info().
  40. * - storage:
  41. * - type: the storage backend specified in the 'field_storage_default'
  42. * system variable.
  43. * - settings: each omitted setting is given the default value specified in
  44. * hook_field_storage_info().
  45. *
  46. * @return
  47. * The $field array with the id property filled in.
  48. *
  49. * @throws FieldException
  50. *
  51. * See: @link field Field API data structures @endlink.
  52. */
  53. function field_create_field($field) {
  54. // Field name is required.
  55. if (empty($field['field_name'])) {
  56. throw new FieldException('Attempt to create an unnamed field.');
  57. }
  58. // Field type is required.
  59. if (empty($field['type'])) {
  60. throw new FieldException('Attempt to create a field with no type.');
  61. }
  62. // Field name cannot contain invalid characters.
  63. if (!preg_match('/^[_a-z]+[_a-z0-9]*$/', $field['field_name'])) {
  64. throw new FieldException('Attempt to create a field with invalid characters. Only lowercase alphanumeric characters and underscores are allowed, and only lowercase letters and underscore are allowed as the first character');
  65. }
  66. // Field name cannot be longer than 32 characters. We use drupal_strlen()
  67. // because the DB layer assumes that column widths are given in characters,
  68. // not bytes.
  69. if (drupal_strlen($field['field_name']) > 32) {
  70. throw new FieldException(t('Attempt to create a field with a name longer than 32 characters: %name',
  71. array('%name' => $field['field_name'])));
  72. }
  73. // Ensure the field name is unique over active and disabled fields.
  74. // We do not care about deleted fields.
  75. $prior_field = field_read_field($field['field_name'], array('include_inactive' => TRUE));
  76. if (!empty($prior_field)) {
  77. $message = $prior_field['active']?
  78. t('Attempt to create field name %name which already exists and is active.', array('%name' => $field['field_name'])):
  79. t('Attempt to create field name %name which already exists, although it is inactive.', array('%name' => $field['field_name']));
  80. throw new FieldException($message);
  81. }
  82. // Disallow reserved field names. This can't prevent all field name
  83. // collisions with existing entity properties, but some is better
  84. // than none.
  85. foreach (entity_get_info() as $type => $info) {
  86. if (in_array($field['field_name'], $info['entity keys'])) {
  87. throw new FieldException(t('Attempt to create field name %name which is reserved by entity type %type.', array('%name' => $field['field_name'], '%type' => $type)));
  88. }
  89. }
  90. $field += array(
  91. 'entity_types' => array(),
  92. 'cardinality' => 1,
  93. 'translatable' => FALSE,
  94. 'locked' => FALSE,
  95. 'settings' => array(),
  96. 'storage' => array(),
  97. 'deleted' => 0,
  98. );
  99. // Check that the field type is known.
  100. $field_type = field_info_field_types($field['type']);
  101. if (!$field_type) {
  102. throw new FieldException(t('Attempt to create a field of unknown type %type.', array('%type' => $field['type'])));
  103. }
  104. // Create all per-field-type properties (needed here as long as we have
  105. // settings that impact column definitions).
  106. $field['settings'] += field_info_field_settings($field['type']);
  107. $field['module'] = $field_type['module'];
  108. $field['active'] = 1;
  109. // Provide default storage.
  110. $field['storage'] += array(
  111. 'type' => variable_get('field_storage_default', 'field_sql_storage'),
  112. 'settings' => array(),
  113. );
  114. // Check that the storage type is known.
  115. $storage_type = field_info_storage_types($field['storage']['type']);
  116. if (!$storage_type) {
  117. throw new FieldException(t('Attempt to create a field with unknown storage type %type.', array('%type' => $field['storage']['type'])));
  118. }
  119. // Provide default storage settings.
  120. $field['storage']['settings'] += field_info_storage_settings($field['storage']['type']);
  121. $field['storage']['module'] = $storage_type['module'];
  122. $field['storage']['active'] = 1;
  123. // Collect storage information.
  124. module_load_install($field['module']);
  125. $schema = (array) module_invoke($field['module'], 'field_schema', $field);
  126. $schema += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array());
  127. // 'columns' are hardcoded in the field type.
  128. $field['columns'] = $schema['columns'];
  129. // 'foreign keys' are hardcoded in the field type.
  130. $field['foreign keys'] = $schema['foreign keys'];
  131. // 'indexes' can be both hardcoded in the field type, and specified in the
  132. // incoming $field definition.
  133. $field += array(
  134. 'indexes' => array(),
  135. );
  136. $field['indexes'] += $schema['indexes'];
  137. // The serialized 'data' column contains everything from $field that does not
  138. // have its own column and is not automatically populated when the field is
  139. // read.
  140. $data = $field;
  141. 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']);
  142. // Additionally, do not save the 'bundles' property populated by
  143. // field_info_field().
  144. unset($data['bundles']);
  145. $record = array(
  146. 'field_name' => $field['field_name'],
  147. 'type' => $field['type'],
  148. 'module' => $field['module'],
  149. 'active' => $field['active'],
  150. 'storage_type' => $field['storage']['type'],
  151. 'storage_module' => $field['storage']['module'],
  152. 'storage_active' => $field['storage']['active'],
  153. 'locked' => $field['locked'],
  154. 'data' => $data,
  155. 'cardinality' => $field['cardinality'],
  156. 'translatable' => $field['translatable'],
  157. 'deleted' => $field['deleted'],
  158. );
  159. // Store the field and get the id back.
  160. drupal_write_record('field_config', $record);
  161. $field['id'] = $record['id'];
  162. // Invoke hook_field_storage_create_field after the field is
  163. // complete (e.g. it has its id).
  164. try {
  165. // Invoke hook_field_storage_create_field after
  166. // drupal_write_record() sets the field id.
  167. module_invoke($storage_type['module'], 'field_storage_create_field', $field);
  168. }
  169. catch (Exception $e) {
  170. // If storage creation failed, remove the field_config record before
  171. // rethrowing the exception.
  172. db_delete('field_config')
  173. ->condition('id', $field['id'])
  174. ->execute();
  175. throw $e;
  176. }
  177. // Clear caches
  178. field_cache_clear(TRUE);
  179. // Invoke external hooks after the cache is cleared for API consistency.
  180. module_invoke_all('field_create_field', $field);
  181. return $field;
  182. }
  183. /**
  184. * Updates a field.
  185. *
  186. * Any module may forbid any update for any reason. For example, the
  187. * field's storage module might forbid an update if it would change
  188. * the storage schema while data for the field exists. A field type
  189. * module might forbid an update if it would change existing data's
  190. * semantics, or if there are external dependencies on field settings
  191. * that cannot be updated.
  192. *
  193. * @param $field
  194. * A field structure. $field['field_name'] must provided; it
  195. * identifies the field that will be updated to match this
  196. * structure. Any other properties of the field that are not
  197. * specified in $field will be left unchanged, so it is not
  198. * necessary to pass in a fully populated $field structure.
  199. * @return
  200. * Throws a FieldException if the update cannot be performed.
  201. * @see field_create_field()
  202. */
  203. function field_update_field($field) {
  204. // Check that the specified field exists.
  205. $prior_field = field_read_field($field['field_name']);
  206. if (empty($prior_field)) {
  207. throw new FieldException('Attempt to update a non-existent field.');
  208. }
  209. // Use the prior field values for anything not specifically set by the new
  210. // field to be sure that all values are set.
  211. $field += $prior_field;
  212. $field['settings'] += $prior_field['settings'];
  213. // Some updates are always disallowed.
  214. if ($field['type'] != $prior_field['type']) {
  215. throw new FieldException("Cannot change an existing field's type.");
  216. }
  217. if ($field['entity_types'] != $prior_field['entity_types']) {
  218. throw new FieldException("Cannot change an existing field's entity_types property.");
  219. }
  220. if ($field['storage']['type'] != $prior_field['storage']['type']) {
  221. throw new FieldException("Cannot change an existing field's storage type.");
  222. }
  223. // Collect the new storage information, since what is in
  224. // $prior_field may no longer be right.
  225. module_load_install($field['module']);
  226. $schema = (array) module_invoke($field['module'], 'field_schema', $field);
  227. $schema += array('columns' => array(), 'indexes' => array());
  228. // 'columns' are hardcoded in the field type.
  229. $field['columns'] = $schema['columns'];
  230. // 'indexes' can be both hardcoded in the field type, and specified in the
  231. // incoming $field definition.
  232. $field += array(
  233. 'indexes' => array(),
  234. );
  235. $field['indexes'] += $schema['indexes'];
  236. $has_data = field_has_data($field);
  237. // See if any module forbids the update by throwing an exception.
  238. foreach (module_implements('field_update_forbid') as $module) {
  239. $function = $module . '_field_update_forbid';
  240. $function($field, $prior_field, $has_data);
  241. }
  242. // Tell the storage engine to update the field. Do this before
  243. // saving the new definition since it still might fail.
  244. $storage_type = field_info_storage_types($field['storage']['type']);
  245. module_invoke($storage_type['module'], 'field_storage_update_field', $field, $prior_field, $has_data);
  246. // Save the new field definition. @todo: refactor with
  247. // field_create_field.
  248. // The serialized 'data' column contains everything from $field that does not
  249. // have its own column and is not automatically populated when the field is
  250. // read.
  251. $data = $field;
  252. unset($data['columns'], $data['field_name'], $data['type'], $data['locked'], $data['module'], $data['cardinality'], $data['active'], $data['deleted']);
  253. // Additionally, do not save the 'bundles' property populated by
  254. // field_info_field().
  255. unset($data['bundles']);
  256. $field['data'] = $data;
  257. // Store the field and create the id.
  258. $primary_key = array('id');
  259. drupal_write_record('field_config', $field, $primary_key);
  260. // Clear caches
  261. field_cache_clear(TRUE);
  262. // Invoke external hooks after the cache is cleared for API consistency.
  263. module_invoke_all('field_update_field', $field, $prior_field, $has_data);
  264. }
  265. /**
  266. * Reads a single field record directly from the database.
  267. *
  268. * Generally, you should use the field_info_field() instead.
  269. *
  270. * This function will not return deleted fields. Use
  271. * field_read_fields() instead for this purpose.
  272. *
  273. * @param $field_name
  274. * The field name to read.
  275. * @param array $include_additional
  276. * The default behavior of this function is to not return a field that
  277. * is inactive. Setting
  278. * $include_additional['include_inactive'] to TRUE will override this
  279. * behavior.
  280. * @return
  281. * A field definition array, or FALSE.
  282. */
  283. function field_read_field($field_name, $include_additional = array()) {
  284. $fields = field_read_fields(array('field_name' => $field_name), $include_additional);
  285. return $fields ? current($fields) : FALSE;
  286. }
  287. /**
  288. * Reads in fields that match an array of conditions.
  289. *
  290. * @param array $params
  291. * An array of conditions to match against.
  292. * @param array $include_additional
  293. * The default behavior of this function is to not return fields that
  294. * are inactive or have been deleted. Setting
  295. * $include_additional['include_inactive'] or
  296. * $include_additional['include_deleted'] to TRUE will override this
  297. * behavior.
  298. * @return
  299. * An array of fields matching $params. If
  300. * $include_additional['include_deleted'] is TRUE, the array is keyed
  301. * by field id, otherwise it is keyed by field name.
  302. */
  303. function field_read_fields($params = array(), $include_additional = array()) {
  304. $query = db_select('field_config', 'fc', array('fetch' => PDO::FETCH_ASSOC));
  305. $query->fields('fc');
  306. // Turn the conditions into a query.
  307. foreach ($params as $key => $value) {
  308. $query->condition($key, $value);
  309. }
  310. if (!isset($include_additional['include_inactive']) || !$include_additional['include_inactive']) {
  311. $query
  312. ->condition('fc.active', 1)
  313. ->condition('fc.storage_active', 1);
  314. }
  315. $include_deleted = (isset($include_additional['include_deleted']) && $include_additional['include_deleted']);
  316. if (!$include_deleted) {
  317. $query->condition('fc.deleted', 0);
  318. }
  319. $fields = array();
  320. $results = $query->execute();
  321. foreach ($results as $record) {
  322. $field = unserialize($record['data']);
  323. $field['id'] = $record['id'];
  324. $field['field_name'] = $record['field_name'];
  325. $field['type'] = $record['type'];
  326. $field['module'] = $record['module'];
  327. $field['active'] = $record['active'];
  328. $field['storage']['type'] = $record['storage_type'];
  329. $field['storage']['module'] = $record['storage_module'];
  330. $field['storage']['active'] = $record['storage_active'];
  331. $field['locked'] = $record['locked'];
  332. $field['cardinality'] = $record['cardinality'];
  333. $field['translatable'] = $record['translatable'];
  334. $field['deleted'] = $record['deleted'];
  335. module_invoke_all('field_read_field', $field);
  336. // Populate storage information.
  337. module_load_install($field['module']);
  338. $schema = (array) module_invoke($field['module'], 'field_schema', $field);
  339. $schema += array('columns' => array(), 'indexes' => array());
  340. $field['columns'] = $schema['columns'];
  341. $field_name = $field['field_name'];
  342. if ($include_deleted) {
  343. $field_name = $field['id'];
  344. }
  345. $fields[$field_name] = $field;
  346. }
  347. return $fields;
  348. }
  349. /**
  350. * Marks a field and its instances and data for deletion.
  351. *
  352. * @param $field_name
  353. * The field name to delete.
  354. */
  355. function field_delete_field($field_name) {
  356. // Delete all non-deleted instances.
  357. $field = field_info_field($field_name);
  358. if (isset($field['bundles'])) {
  359. foreach ($field['bundles'] as $entity_type => $bundles) {
  360. foreach ($bundles as $bundle) {
  361. $instance = field_info_instance($entity_type, $field_name, $bundle);
  362. field_delete_instance($instance, FALSE);
  363. }
  364. }
  365. }
  366. // Mark field data for deletion.
  367. module_invoke($field['storage']['module'], 'field_storage_delete_field', $field);
  368. // Mark the field for deletion.
  369. db_update('field_config')
  370. ->fields(array('deleted' => 1))
  371. ->condition('field_name', $field_name)
  372. ->execute();
  373. // Clear the cache.
  374. field_cache_clear(TRUE);
  375. module_invoke_all('field_delete_field', $field);
  376. }
  377. /**
  378. * Creates an instance of a field, binding it to a bundle.
  379. *
  380. * @param $instance
  381. * A field instance definition array. The field_name, entity_type and
  382. * bundle properties are required. Other properties, if omitted,
  383. * will be given the following default values:
  384. * - label: the field name
  385. * - description: empty string
  386. * - required: FALSE
  387. * - default_value_function: empty string
  388. * - settings: each omitted setting is given the default value specified in
  389. * hook_field_info().
  390. * - widget:
  391. * - type: the default widget specified in hook_field_info().
  392. * - settings: each omitted setting is given the default value specified in
  393. * hook_field_widget_info().
  394. * - display:
  395. * Settings for the 'default' view mode will be added if not present, and
  396. * each view mode in the definition will be completed with the following
  397. * default values:
  398. * - label: 'above'
  399. * - type: the default formatter specified in hook_field_info().
  400. * - settings: each omitted setting is given the default value specified in
  401. * hook_field_formatter_info().
  402. * View modes not present in the definition are left empty, and the field
  403. * will not be displayed in this mode.
  404. *
  405. * @return
  406. * The $instance array with the id property filled in.
  407. *
  408. * @throws FieldException
  409. *
  410. * See: @link field Field API data structures @endlink.
  411. */
  412. function field_create_instance($instance) {
  413. $field = field_read_field($instance['field_name']);
  414. if (empty($field)) {
  415. throw new FieldException(t("Attempt to create an instance of a field @field_name that doesn't exist or is currently inactive.", array('@field_name' => $instance['field_name'])));
  416. }
  417. // Check that the required properties exists.
  418. if (empty($instance['entity_type'])) {
  419. throw new FieldException(t('Attempt to create an instance of field @field_name without an entity type.', array('@field_name' => $instance['field_name'])));
  420. }
  421. if (empty($instance['bundle'])) {
  422. throw new FieldException(t('Attempt to create an instance of field @field_name without a bundle.', array('@field_name' => $instance['field_name'])));
  423. }
  424. // Check that the field can be attached to this entity type.
  425. if (!empty($field['entity_types']) && !in_array($instance['entity_type'], $field['entity_types'])) {
  426. throw new FieldException(t('Attempt to create an instance of field @field_name on forbidden entity type @entity_type.', array('@field_name' => $instance['field_name'], '@entity_type' => $instance['entity_type'])));
  427. }
  428. // Set the field id.
  429. $instance['field_id'] = $field['id'];
  430. // Note that we do *not* prevent creating a field on non-existing bundles,
  431. // because that would break the 'Body as field' upgrade for contrib
  432. // node types.
  433. // TODO: Check that the widget type is known and can handle the field type ?
  434. // TODO: Check that the formatters are known and can handle the field type ?
  435. // TODO: Check that the display view modes are known for the entity type ?
  436. // Those checks should probably happen in _field_write_instance() ?
  437. // Problem : this would mean that a UI module cannot update an instance with a disabled formatter.
  438. // Ensure the field instance is unique within the bundle.
  439. // We only check for instances of active fields, since adding an instance of
  440. // a disabled field is not supported.
  441. $prior_instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
  442. if (!empty($prior_instance)) {
  443. $message = t('Attempt to create an instance of field @field_name on bundle @bundle that already has an instance of that field.', array('@field_name' => $instance['field_name'], '@bundle' => $instance['bundle']));
  444. throw new FieldException($message);
  445. }
  446. _field_write_instance($instance);
  447. // Clear caches
  448. field_cache_clear();
  449. // Invoke external hooks after the cache is cleared for API consistency.
  450. module_invoke_all('field_create_instance', $instance);
  451. return $instance;
  452. }
  453. /**
  454. * Updates an instance of a field.
  455. *
  456. * @param $instance
  457. * An associative array representing an instance structure. The required
  458. * keys and values are:
  459. * - entity_type: The type of the entity the field is attached to.
  460. * - bundle: The bundle this field belongs to.
  461. * - field_name: The name of an existing field.
  462. * Read-only_id properties are assigned automatically. Any other
  463. * properties specified in $instance overwrite the existing values for
  464. * the instance.
  465. *
  466. * @throws FieldException
  467. *
  468. * @see field_create_instance()
  469. */
  470. function field_update_instance($instance) {
  471. // Check that the specified field exists.
  472. $field = field_read_field($instance['field_name']);
  473. if (empty($field)) {
  474. throw new FieldException(t('Attempt to update an instance of a nonexistent field @field.', array('@field' => $instance['field_name'])));
  475. }
  476. // Check that the field instance exists (even if it is inactive, since we
  477. // want to be able to replace inactive widgets with new ones).
  478. $prior_instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle'], array('include_inactive' => TRUE));
  479. if (empty($prior_instance)) {
  480. throw new FieldException(t("Attempt to update an instance of field @field on bundle @bundle that doesn't exist.", array('@field' => $instance['field_name'], '@bundle' => $instance['bundle'])));
  481. }
  482. $instance['id'] = $prior_instance['id'];
  483. $instance['field_id'] = $prior_instance['field_id'];
  484. _field_write_instance($instance, TRUE);
  485. // Clear caches.
  486. field_cache_clear();
  487. module_invoke_all('field_update_instance', $instance, $prior_instance);
  488. }
  489. /**
  490. * Stores an instance record in the field configuration database.
  491. *
  492. * @param $instance
  493. * An instance structure.
  494. * @param $update
  495. * Whether this is a new or existing instance.
  496. */
  497. function _field_write_instance($instance, $update = FALSE) {
  498. $field = field_read_field($instance['field_name']);
  499. $field_type = field_info_field_types($field['type']);
  500. // Set defaults.
  501. $instance += array(
  502. 'settings' => array(),
  503. 'display' => array(),
  504. 'widget' => array(),
  505. 'required' => FALSE,
  506. 'label' => $instance['field_name'],
  507. 'description' => '',
  508. 'deleted' => 0,
  509. );
  510. // Set default instance settings.
  511. $instance['settings'] += field_info_instance_settings($field['type']);
  512. // Set default widget and settings.
  513. $instance['widget'] += array(
  514. // TODO: what if no 'default_widget' specified ?
  515. 'type' => $field_type['default_widget'],
  516. 'settings' => array(),
  517. );
  518. // If no weight specified, make sure the field sinks at the bottom.
  519. if (!isset($instance['widget']['weight'])) {
  520. $max_weight = field_info_max_weight($instance['entity_type'], $instance['bundle'], 'form');
  521. $instance['widget']['weight'] = isset($max_weight) ? $max_weight + 1 : 0;
  522. }
  523. // Check widget module.
  524. $widget_type = field_info_widget_types($instance['widget']['type']);
  525. $instance['widget']['module'] = $widget_type['module'];
  526. $instance['widget']['settings'] += field_info_widget_settings($instance['widget']['type']);
  527. // Make sure there are at least display settings for the 'default' view mode,
  528. // and fill in defaults for each view mode specified in the definition.
  529. $instance['display'] += array(
  530. 'default' => array(),
  531. );
  532. foreach ($instance['display'] as $view_mode => $display) {
  533. $display += array(
  534. 'label' => 'above',
  535. 'type' => isset($field_type['default_formatter']) ? $field_type['default_formatter'] : 'hidden',
  536. 'settings' => array(),
  537. );
  538. if ($display['type'] != 'hidden') {
  539. $formatter_type = field_info_formatter_types($display['type']);
  540. $display['module'] = $formatter_type['module'];
  541. $display['settings'] += field_info_formatter_settings($display['type']);
  542. }
  543. // If no weight specified, make sure the field sinks at the bottom.
  544. if (!isset($display['weight'])) {
  545. $max_weight = field_info_max_weight($instance['entity_type'], $instance['bundle'], $view_mode);
  546. $display['weight'] = isset($max_weight) ? $max_weight + 1 : 0;
  547. }
  548. $instance['display'][$view_mode] = $display;
  549. }
  550. // The serialized 'data' column contains everything from $instance that does
  551. // not have its own column and is not automatically populated when the
  552. // instance is read.
  553. $data = $instance;
  554. unset($data['id'], $data['field_id'], $data['field_name'], $data['entity_type'], $data['bundle'], $data['deleted']);
  555. $record = array(
  556. 'field_id' => $instance['field_id'],
  557. 'field_name' => $instance['field_name'],
  558. 'entity_type' => $instance['entity_type'],
  559. 'bundle' => $instance['bundle'],
  560. 'data' => $data,
  561. 'deleted' => $instance['deleted'],
  562. );
  563. // We need to tell drupal_update_record() the primary keys to trigger an
  564. // update.
  565. if ($update) {
  566. $record['id'] = $instance['id'];
  567. $primary_key = array('id');
  568. }
  569. else {
  570. $primary_key = array();
  571. }
  572. drupal_write_record('field_config_instance', $record, $primary_key);
  573. }
  574. /**
  575. * Reads a single instance record from the database.
  576. *
  577. * Generally, you should use field_info_instance() instead, as it
  578. * provides caching and allows other modules the opportunity to
  579. * append additional formatters, widgets, and other information.
  580. *
  581. * @param $entity_type
  582. * The type of entity to which the field is bound.
  583. * @param $field_name
  584. * The field name to read.
  585. * @param $bundle
  586. * The bundle to which the field is bound.
  587. * @param array $include_additional
  588. * The default behavior of this function is to not return an instance that
  589. * has been deleted, or whose field is inactive. Setting
  590. * $include_additional['include_inactive'] or
  591. * $include_additional['include_deleted'] to TRUE will override this
  592. * behavior.
  593. * @return
  594. * An instance structure, or FALSE.
  595. */
  596. function field_read_instance($entity_type, $field_name, $bundle, $include_additional = array()) {
  597. $instances = field_read_instances(array('entity_type' => $entity_type, 'field_name' => $field_name, 'bundle' => $bundle), $include_additional);
  598. return $instances ? current($instances) : FALSE;
  599. }
  600. /**
  601. * Reads in field instances that match an array of conditions.
  602. *
  603. * @param $param
  604. * An array of properties to use in selecting a field
  605. * instance. Valid keys include any column of the
  606. * field_config_instance table. If NULL, all instances will be returned.
  607. * @param $include_additional
  608. * The default behavior of this function is to not return field
  609. * instances that have been marked deleted, or whose field is inactive.
  610. * Setting $include_additional['include_inactive'] or
  611. * $include_additional['include_deleted'] to TRUE will override this
  612. * behavior.
  613. * @return
  614. * An array of instances matching the arguments.
  615. */
  616. function field_read_instances($params = array(), $include_additional = array()) {
  617. $include_inactive = isset($include_additional['include_inactive']) && $include_additional['include_inactive'];
  618. $include_deleted = isset($include_additional['include_deleted']) && $include_additional['include_deleted'];
  619. $query = db_select('field_config_instance', 'fci', array('fetch' => PDO::FETCH_ASSOC));
  620. $query->join('field_config', 'fc', 'fc.id = fci.field_id');
  621. $query->fields('fci');
  622. // Turn the conditions into a query.
  623. foreach ($params as $key => $value) {
  624. $query->condition('fci.' . $key, $value);
  625. }
  626. if (!$include_inactive) {
  627. $query
  628. ->condition('fc.active', 1)
  629. ->condition('fc.storage_active', 1);
  630. }
  631. if (!$include_deleted) {
  632. $query->condition('fc.deleted', 0);
  633. $query->condition('fci.deleted', 0);
  634. }
  635. $instances = array();
  636. $results = $query->execute();
  637. foreach ($results as $record) {
  638. // Filter out instances on unknown entity types (for instance because the
  639. // module exposing them was disabled).
  640. $entity_info = entity_get_info($record['entity_type']);
  641. if ($include_inactive || $entity_info) {
  642. $instance = unserialize($record['data']);
  643. $instance['id'] = $record['id'];
  644. $instance['field_id'] = $record['field_id'];
  645. $instance['field_name'] = $record['field_name'];
  646. $instance['entity_type'] = $record['entity_type'];
  647. $instance['bundle'] = $record['bundle'];
  648. $instance['deleted'] = $record['deleted'];
  649. module_invoke_all('field_read_instance', $instance);
  650. $instances[] = $instance;
  651. }
  652. }
  653. return $instances;
  654. }
  655. /**
  656. * Marks a field instance and its data for deletion.
  657. *
  658. * @param $instance
  659. * An instance structure.
  660. * @param $field_cleanup
  661. * If TRUE, the field will be deleted as well if its last instance is being
  662. * deleted. If FALSE, it is the caller's responsibility to handle the case of
  663. * fields left without instances. Defaults to TRUE.
  664. */
  665. function field_delete_instance($instance, $field_cleanup = TRUE) {
  666. // Mark the field instance for deletion.
  667. db_update('field_config_instance')
  668. ->fields(array('deleted' => 1))
  669. ->condition('field_name', $instance['field_name'])
  670. ->condition('entity_type', $instance['entity_type'])
  671. ->condition('bundle', $instance['bundle'])
  672. ->execute();
  673. // Clear the cache.
  674. field_cache_clear();
  675. // Mark instance data for deletion.
  676. $field = field_info_field($instance['field_name']);
  677. module_invoke($field['storage']['module'], 'field_storage_delete_instance', $instance);
  678. // Let modules react to the deletion of the instance.
  679. module_invoke_all('field_delete_instance', $instance);
  680. // Delete the field itself if we just deleted its last instance.
  681. if ($field_cleanup && count($field['bundles']) == 0) {
  682. field_delete_field($field['field_name']);
  683. }
  684. }
  685. /**
  686. * @} End of "defgroup field_crud".
  687. */
  688. /**
  689. * @defgroup field_purge Field API bulk data deletion
  690. * @{
  691. * Clean up after Field API bulk deletion operations.
  692. *
  693. * Field API provides functions for deleting data attached to individual
  694. * entities as well as deleting entire fields or field instances in a single
  695. * operation.
  696. *
  697. * Deleting field data items for an entity with field_attach_delete() involves
  698. * three separate operations:
  699. * - Invoking the Field Type API hook_field_delete() for each field on the
  700. * entity. The hook for each field type receives the entity and the specific
  701. * field being deleted. A file field module might use this hook to delete
  702. * uploaded files from the filesystem.
  703. * - Invoking the Field Storage API hook_field_storage_delete() to remove
  704. * data from the primary field storage. The hook implementation receives the
  705. * entity being deleted and deletes data for all of the entity's bundle's
  706. * fields.
  707. * - Invoking the global Field Attach API hook_field_attach_delete() for all
  708. * modules that implement it. Each hook implementation receives the entity
  709. * being deleted and can operate on whichever subset of the entity's bundle's
  710. * fields it chooses to.
  711. *
  712. * These hooks are invoked immediately when field_attach_delete() is
  713. * called. Similar operations are performed for field_attach_delete_revision().
  714. *
  715. * When a field, bundle, or field instance is deleted, it is not practical to
  716. * invoke these hooks immediately on every affected entity in a single page
  717. * request; there could be thousands or millions of them. Instead, the
  718. * appropriate field data items, instances, and/or fields are marked as deleted
  719. * so that subsequent load or query operations will not return them. Later, a
  720. * separate process cleans up, or "purges", the marked-as-deleted data by going
  721. * through the three-step process described above and, finally, removing
  722. * deleted field and instance records.
  723. *
  724. * Purging field data is made somewhat tricky by the fact that, while
  725. * field_attach_delete() has a complete entity to pass to the various deletion
  726. * hooks, the Field API purge process only has the field data it has previously
  727. * stored. It cannot reconstruct complete original entities to pass to the
  728. * deletion hooks. It is even possible that the original entity to which some
  729. * Field API data was attached has been itself deleted before the field purge
  730. * operation takes place.
  731. *
  732. * Field API resolves this problem by using "pseudo-entities" during purge
  733. * operations. A pseudo-entity contains only the information from the original
  734. * entity that Field API knows about: entity type, id, revision id, and
  735. * bundle. It also contains the field data for whichever field instance is
  736. * currently being purged. For example, suppose that the node type 'story' used
  737. * to contain a field called 'subtitle' but the field was deleted. If node 37
  738. * was a story with a subtitle, the pseudo-entity passed to the purge hooks
  739. * would look something like this:
  740. *
  741. * @code
  742. * $entity = stdClass Object(
  743. * [nid] => 37,
  744. * [vid] => 37,
  745. * [type] => 'story',
  746. * [subtitle] => array(
  747. * [0] => array(
  748. * 'value' => 'subtitle text',
  749. * ),
  750. * ),
  751. * );
  752. * @endcode
  753. *
  754. * See @link field Field API @endlink for information about the other parts of
  755. * the Field API.
  756. */
  757. /**
  758. * Purges a batch of deleted Field API data, instances, or fields.
  759. *
  760. * This function will purge deleted field data in batches. The batch size
  761. * is defined as an argument to the function, and once each batch is finished,
  762. * it continues with the next batch until all have completed. If a deleted field
  763. * instance with no remaining data records is found, the instance itself will
  764. * be purged. If a deleted field with no remaining field instances is found, the
  765. * field itself will be purged.
  766. *
  767. * @param $batch_size
  768. * The maximum number of field data records to purge before returning.
  769. */
  770. function field_purge_batch($batch_size) {
  771. // Retrieve all deleted field instances. We cannot use field_info_instances()
  772. // because that function does not return deleted instances.
  773. $instances = field_read_instances(array('deleted' => 1), array('include_deleted' => 1));
  774. foreach ($instances as $instance) {
  775. // field_purge_data() will need the field array.
  776. $field = field_info_field_by_id($instance['field_id']);
  777. // Retrieve some entities.
  778. $query = new EntityFieldQuery();
  779. $results = $query
  780. ->fieldCondition($field)
  781. ->entityCondition('bundle', $instance['bundle'])
  782. ->deleted(TRUE)
  783. ->range(0, $batch_size)
  784. ->execute();
  785. if ($results) {
  786. foreach ($results as $entity_type => $stub_entities) {
  787. field_attach_load($entity_type, $stub_entities, FIELD_LOAD_CURRENT, array('field_id' => $field['id'], 'deleted' => 1));
  788. foreach ($stub_entities as $stub_entity) {
  789. // Purge the data for the entity.
  790. field_purge_data($entity_type, $stub_entity, $field, $instance);
  791. }
  792. }
  793. }
  794. else {
  795. // No field data remains for the instance, so we can remove it.
  796. field_purge_instance($instance);
  797. }
  798. }
  799. // Retrieve all deleted fields. Any that have no instances can be purged.
  800. $fields = field_read_fields(array('deleted' => 1), array('include_deleted' => 1));
  801. foreach ($fields as $field) {
  802. $instances = field_read_instances(array('field_id' => $field['id']), array('include_deleted' => 1));
  803. if (empty($instances)) {
  804. field_purge_field($field);
  805. }
  806. }
  807. }
  808. /**
  809. * Purges the field data for a single field on a single pseudo-entity.
  810. *
  811. * This is basically the same as field_attach_delete() except it only applies
  812. * to a single field. The entity itself is not being deleted, and it is quite
  813. * possible that other field data will remain attached to it.
  814. *
  815. * @param $entity_type
  816. * The type of $entity; e.g. 'node' or 'user'.
  817. * @param $entity
  818. * The pseudo-entity whose field data is being purged.
  819. * @param $field
  820. * The (possibly deleted) field whose data is being purged.
  821. * @param $instance
  822. * The deleted field instance whose data is being purged.
  823. */
  824. function field_purge_data($entity_type, $entity, $field, $instance) {
  825. // Each field type's hook_field_delete() only expects to operate on a single
  826. // field at a time, so we can use it as-is for purging.
  827. $options = array('field_id' => $instance['field_id'], 'deleted' => TRUE);
  828. _field_invoke('delete', $entity_type, $entity, $dummy, $dummy, $options);
  829. // Tell the field storage system to purge the data.
  830. module_invoke($field['storage']['module'], 'field_storage_purge', $entity_type, $entity, $field, $instance);
  831. // Let other modules act on purging the data.
  832. foreach (module_implements('field_attach_purge') as $module) {
  833. $function = $module . '_field_attach_purge';
  834. $function($entity_type, $entity, $field, $instance);
  835. }
  836. }
  837. /**
  838. * Purges a field instance record from the database.
  839. *
  840. * This function assumes all data for the instance has already been purged, and
  841. * should only be called by field_purge_batch().
  842. *
  843. * @param $instance
  844. * The instance record to purge.
  845. */
  846. function field_purge_instance($instance) {
  847. db_delete('field_config_instance')
  848. ->condition('id', $instance['id'])
  849. ->execute();
  850. // Notify the storage engine.
  851. $field = field_info_field_by_id($instance['field_id']);
  852. module_invoke($field['storage']['module'], 'field_storage_purge_instance', $instance);
  853. // Clear the cache.
  854. field_info_cache_clear();
  855. // Invoke external hooks after the cache is cleared for API consistency.
  856. module_invoke_all('field_purge_instance', $instance);
  857. }
  858. /**
  859. * Purges a field record from the database.
  860. *
  861. * This function assumes all instances for the field has already been purged,
  862. * and should only be called by field_purge_batch().
  863. *
  864. * @param $field
  865. * The field record to purge.
  866. */
  867. function field_purge_field($field) {
  868. $instances = field_read_instances(array('field_id' => $field['id']), array('include_deleted' => 1));
  869. if (count($instances) > 0) {
  870. throw new FieldException(t('Attempt to purge a field @field_name that still has instances.', array('@field_name' => $field['field_name'])));
  871. }
  872. db_delete('field_config')
  873. ->condition('id', $field['id'])
  874. ->execute();
  875. // Notify the storage engine.
  876. module_invoke($field['storage']['module'], 'field_storage_purge_field', $field);
  877. // Clear the cache.
  878. field_info_cache_clear();
  879. // Invoke external hooks after the cache is cleared for API consistency.
  880. module_invoke_all('field_purge_field', $field);
  881. }
  882. /**
  883. * @} End of "defgroup field_purge".
  884. */