field.crud.inc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. Keys are columns from the
  292. * 'field_config' table, values are conditions to match. Additionally,
  293. * conditions on the 'entity_type' and 'bundle' columns from the
  294. * 'field_config_instance' table are supported (select fields having an
  295. * instance on a given bundle).
  296. * @param array $include_additional
  297. * The default behavior of this function is to not return fields that
  298. * are inactive or have been deleted. Setting
  299. * $include_additional['include_inactive'] or
  300. * $include_additional['include_deleted'] to TRUE will override this
  301. * behavior.
  302. * @return
  303. * An array of fields matching $params. If
  304. * $include_additional['include_deleted'] is TRUE, the array is keyed
  305. * by field id, otherwise it is keyed by field name.
  306. */
  307. function field_read_fields($params = array(), $include_additional = array()) {
  308. $query = db_select('field_config', 'fc', array('fetch' => PDO::FETCH_ASSOC));
  309. $query->fields('fc');
  310. // Turn the conditions into a query.
  311. foreach ($params as $key => $value) {
  312. // Allow filtering on the 'entity_type' and 'bundle' columns of the
  313. // field_config_instance table.
  314. if ($key == 'entity_type' || $key == 'bundle') {
  315. if (empty($fci_join)) {
  316. $fci_join = $query->join('field_config_instance', 'fci', 'fc.id = fci.field_id');
  317. }
  318. $key = 'fci.' . $key;
  319. }
  320. else {
  321. $key = 'fc.' . $key;
  322. }
  323. $query->condition($key, $value);
  324. }
  325. if (!isset($include_additional['include_inactive']) || !$include_additional['include_inactive']) {
  326. $query
  327. ->condition('fc.active', 1)
  328. ->condition('fc.storage_active', 1);
  329. }
  330. $include_deleted = (isset($include_additional['include_deleted']) && $include_additional['include_deleted']);
  331. if (!$include_deleted) {
  332. $query->condition('fc.deleted', 0);
  333. }
  334. $fields = array();
  335. $results = $query->execute();
  336. foreach ($results as $record) {
  337. $field = unserialize($record['data']);
  338. $field['id'] = $record['id'];
  339. $field['field_name'] = $record['field_name'];
  340. $field['type'] = $record['type'];
  341. $field['module'] = $record['module'];
  342. $field['active'] = $record['active'];
  343. $field['storage']['type'] = $record['storage_type'];
  344. $field['storage']['module'] = $record['storage_module'];
  345. $field['storage']['active'] = $record['storage_active'];
  346. $field['locked'] = $record['locked'];
  347. $field['cardinality'] = $record['cardinality'];
  348. $field['translatable'] = $record['translatable'];
  349. $field['deleted'] = $record['deleted'];
  350. module_invoke_all('field_read_field', $field);
  351. // Populate storage information.
  352. module_load_install($field['module']);
  353. $schema = (array) module_invoke($field['module'], 'field_schema', $field);
  354. $schema += array('columns' => array(), 'indexes' => array());
  355. $field['columns'] = $schema['columns'];
  356. $field_name = $field['field_name'];
  357. if ($include_deleted) {
  358. $field_name = $field['id'];
  359. }
  360. $fields[$field_name] = $field;
  361. }
  362. return $fields;
  363. }
  364. /**
  365. * Marks a field and its instances and data for deletion.
  366. *
  367. * @param $field_name
  368. * The field name to delete.
  369. */
  370. function field_delete_field($field_name) {
  371. // Delete all non-deleted instances.
  372. $field = field_info_field($field_name);
  373. if (isset($field['bundles'])) {
  374. foreach ($field['bundles'] as $entity_type => $bundles) {
  375. foreach ($bundles as $bundle) {
  376. $instance = field_info_instance($entity_type, $field_name, $bundle);
  377. field_delete_instance($instance, FALSE);
  378. }
  379. }
  380. }
  381. // Mark field data for deletion.
  382. module_invoke($field['storage']['module'], 'field_storage_delete_field', $field);
  383. // Mark the field for deletion.
  384. db_update('field_config')
  385. ->fields(array('deleted' => 1))
  386. ->condition('field_name', $field_name)
  387. ->execute();
  388. // Clear the cache.
  389. field_cache_clear(TRUE);
  390. module_invoke_all('field_delete_field', $field);
  391. }
  392. /**
  393. * Creates an instance of a field, binding it to a bundle.
  394. *
  395. * @param $instance
  396. * A field instance definition array. The field_name, entity_type and
  397. * bundle properties are required. Other properties, if omitted,
  398. * will be given the following default values:
  399. * - label: the field name
  400. * - description: empty string
  401. * - required: FALSE
  402. * - default_value_function: empty string
  403. * - settings: each omitted setting is given the default value specified in
  404. * hook_field_info().
  405. * - widget:
  406. * - type: the default widget specified in hook_field_info().
  407. * - settings: each omitted setting is given the default value specified in
  408. * hook_field_widget_info().
  409. * - display:
  410. * Settings for the 'default' view mode will be added if not present, and
  411. * each view mode in the definition will be completed with the following
  412. * default values:
  413. * - label: 'above'
  414. * - type: the default formatter specified in hook_field_info().
  415. * - settings: each omitted setting is given the default value specified in
  416. * hook_field_formatter_info().
  417. * View modes not present in the definition are left empty, and the field
  418. * will not be displayed in this mode.
  419. *
  420. * @return
  421. * The $instance array with the id property filled in.
  422. *
  423. * @throws FieldException
  424. *
  425. * See: @link field Field API data structures @endlink.
  426. */
  427. function field_create_instance($instance) {
  428. $field = field_read_field($instance['field_name']);
  429. if (empty($field)) {
  430. 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'])));
  431. }
  432. // Check that the required properties exists.
  433. if (empty($instance['entity_type'])) {
  434. throw new FieldException(t('Attempt to create an instance of field @field_name without an entity type.', array('@field_name' => $instance['field_name'])));
  435. }
  436. if (empty($instance['bundle'])) {
  437. throw new FieldException(t('Attempt to create an instance of field @field_name without a bundle.', array('@field_name' => $instance['field_name'])));
  438. }
  439. // Check that the field can be attached to this entity type.
  440. if (!empty($field['entity_types']) && !in_array($instance['entity_type'], $field['entity_types'])) {
  441. 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'])));
  442. }
  443. // Set the field id.
  444. $instance['field_id'] = $field['id'];
  445. // Note that we do *not* prevent creating a field on non-existing bundles,
  446. // because that would break the 'Body as field' upgrade for contrib
  447. // node types.
  448. // TODO: Check that the widget type is known and can handle the field type ?
  449. // TODO: Check that the formatters are known and can handle the field type ?
  450. // TODO: Check that the display view modes are known for the entity type ?
  451. // Those checks should probably happen in _field_write_instance() ?
  452. // Problem : this would mean that a UI module cannot update an instance with a disabled formatter.
  453. // Ensure the field instance is unique within the bundle.
  454. // We only check for instances of active fields, since adding an instance of
  455. // a disabled field is not supported.
  456. $prior_instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
  457. if (!empty($prior_instance)) {
  458. $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']));
  459. throw new FieldException($message);
  460. }
  461. _field_write_instance($instance);
  462. // Clear caches
  463. field_cache_clear();
  464. // Invoke external hooks after the cache is cleared for API consistency.
  465. module_invoke_all('field_create_instance', $instance);
  466. return $instance;
  467. }
  468. /**
  469. * Updates an instance of a field.
  470. *
  471. * @param $instance
  472. * An associative array representing an instance structure. The required
  473. * keys and values are:
  474. * - entity_type: The type of the entity the field is attached to.
  475. * - bundle: The bundle this field belongs to.
  476. * - field_name: The name of an existing field.
  477. * Read-only_id properties are assigned automatically. Any other
  478. * properties specified in $instance overwrite the existing values for
  479. * the instance.
  480. *
  481. * @throws FieldException
  482. *
  483. * @see field_create_instance()
  484. */
  485. function field_update_instance($instance) {
  486. // Check that the specified field exists.
  487. $field = field_read_field($instance['field_name']);
  488. if (empty($field)) {
  489. throw new FieldException(t('Attempt to update an instance of a nonexistent field @field.', array('@field' => $instance['field_name'])));
  490. }
  491. // Check that the field instance exists (even if it is inactive, since we
  492. // want to be able to replace inactive widgets with new ones).
  493. $prior_instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle'], array('include_inactive' => TRUE));
  494. if (empty($prior_instance)) {
  495. 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'])));
  496. }
  497. $instance['id'] = $prior_instance['id'];
  498. $instance['field_id'] = $prior_instance['field_id'];
  499. _field_write_instance($instance, TRUE);
  500. // Clear caches.
  501. field_cache_clear();
  502. module_invoke_all('field_update_instance', $instance, $prior_instance);
  503. }
  504. /**
  505. * Stores an instance record in the field configuration database.
  506. *
  507. * @param $instance
  508. * An instance structure.
  509. * @param $update
  510. * Whether this is a new or existing instance.
  511. */
  512. function _field_write_instance($instance, $update = FALSE) {
  513. $field = field_read_field($instance['field_name']);
  514. $field_type = field_info_field_types($field['type']);
  515. // Set defaults.
  516. $instance += array(
  517. 'settings' => array(),
  518. 'display' => array(),
  519. 'widget' => array(),
  520. 'required' => FALSE,
  521. 'label' => $instance['field_name'],
  522. 'description' => '',
  523. 'deleted' => 0,
  524. );
  525. // Set default instance settings.
  526. $instance['settings'] += field_info_instance_settings($field['type']);
  527. // Set default widget and settings.
  528. $instance['widget'] += array(
  529. // TODO: what if no 'default_widget' specified ?
  530. 'type' => $field_type['default_widget'],
  531. 'settings' => array(),
  532. );
  533. // If no weight specified, make sure the field sinks at the bottom.
  534. if (!isset($instance['widget']['weight'])) {
  535. $max_weight = field_info_max_weight($instance['entity_type'], $instance['bundle'], 'form');
  536. $instance['widget']['weight'] = isset($max_weight) ? $max_weight + 1 : 0;
  537. }
  538. // Check widget module.
  539. $widget_type = field_info_widget_types($instance['widget']['type']);
  540. $instance['widget']['module'] = $widget_type['module'];
  541. $instance['widget']['settings'] += field_info_widget_settings($instance['widget']['type']);
  542. // Make sure there are at least display settings for the 'default' view mode,
  543. // and fill in defaults for each view mode specified in the definition.
  544. $instance['display'] += array(
  545. 'default' => array(),
  546. );
  547. foreach ($instance['display'] as $view_mode => $display) {
  548. $display += array(
  549. 'label' => 'above',
  550. 'type' => isset($field_type['default_formatter']) ? $field_type['default_formatter'] : 'hidden',
  551. 'settings' => array(),
  552. );
  553. if ($display['type'] != 'hidden') {
  554. $formatter_type = field_info_formatter_types($display['type']);
  555. $display['module'] = $formatter_type['module'];
  556. $display['settings'] += field_info_formatter_settings($display['type']);
  557. }
  558. // If no weight specified, make sure the field sinks at the bottom.
  559. if (!isset($display['weight'])) {
  560. $max_weight = field_info_max_weight($instance['entity_type'], $instance['bundle'], $view_mode);
  561. $display['weight'] = isset($max_weight) ? $max_weight + 1 : 0;
  562. }
  563. $instance['display'][$view_mode] = $display;
  564. }
  565. // The serialized 'data' column contains everything from $instance that does
  566. // not have its own column and is not automatically populated when the
  567. // instance is read.
  568. $data = $instance;
  569. unset($data['id'], $data['field_id'], $data['field_name'], $data['entity_type'], $data['bundle'], $data['deleted']);
  570. $record = array(
  571. 'field_id' => $instance['field_id'],
  572. 'field_name' => $instance['field_name'],
  573. 'entity_type' => $instance['entity_type'],
  574. 'bundle' => $instance['bundle'],
  575. 'data' => $data,
  576. 'deleted' => $instance['deleted'],
  577. );
  578. // We need to tell drupal_update_record() the primary keys to trigger an
  579. // update.
  580. if ($update) {
  581. $record['id'] = $instance['id'];
  582. $primary_key = array('id');
  583. }
  584. else {
  585. $primary_key = array();
  586. }
  587. drupal_write_record('field_config_instance', $record, $primary_key);
  588. }
  589. /**
  590. * Reads a single instance record from the database.
  591. *
  592. * Generally, you should use field_info_instance() instead, as it
  593. * provides caching and allows other modules the opportunity to
  594. * append additional formatters, widgets, and other information.
  595. *
  596. * @param $entity_type
  597. * The type of entity to which the field is bound.
  598. * @param $field_name
  599. * The field name to read.
  600. * @param $bundle
  601. * The bundle to which the field is bound.
  602. * @param array $include_additional
  603. * The default behavior of this function is to not return an instance that
  604. * has been deleted, or whose field is inactive. Setting
  605. * $include_additional['include_inactive'] or
  606. * $include_additional['include_deleted'] to TRUE will override this
  607. * behavior.
  608. * @return
  609. * An instance structure, or FALSE.
  610. */
  611. function field_read_instance($entity_type, $field_name, $bundle, $include_additional = array()) {
  612. $instances = field_read_instances(array('entity_type' => $entity_type, 'field_name' => $field_name, 'bundle' => $bundle), $include_additional);
  613. return $instances ? current($instances) : FALSE;
  614. }
  615. /**
  616. * Reads in field instances that match an array of conditions.
  617. *
  618. * @param $param
  619. * An array of properties to use in selecting a field
  620. * instance. Valid keys include any column of the
  621. * field_config_instance table. If NULL, all instances will be returned.
  622. * @param $include_additional
  623. * The default behavior of this function is to not return field
  624. * instances that have been marked deleted, or whose field is inactive.
  625. * Setting $include_additional['include_inactive'] or
  626. * $include_additional['include_deleted'] to TRUE will override this
  627. * behavior.
  628. * @return
  629. * An array of instances matching the arguments.
  630. */
  631. function field_read_instances($params = array(), $include_additional = array()) {
  632. $include_inactive = isset($include_additional['include_inactive']) && $include_additional['include_inactive'];
  633. $include_deleted = isset($include_additional['include_deleted']) && $include_additional['include_deleted'];
  634. $query = db_select('field_config_instance', 'fci', array('fetch' => PDO::FETCH_ASSOC));
  635. $query->join('field_config', 'fc', 'fc.id = fci.field_id');
  636. $query->fields('fci');
  637. // Turn the conditions into a query.
  638. foreach ($params as $key => $value) {
  639. $query->condition('fci.' . $key, $value);
  640. }
  641. if (!$include_inactive) {
  642. $query
  643. ->condition('fc.active', 1)
  644. ->condition('fc.storage_active', 1);
  645. }
  646. if (!$include_deleted) {
  647. $query->condition('fc.deleted', 0);
  648. $query->condition('fci.deleted', 0);
  649. }
  650. $instances = array();
  651. $results = $query->execute();
  652. foreach ($results as $record) {
  653. // Filter out instances on unknown entity types (for instance because the
  654. // module exposing them was disabled).
  655. $entity_info = entity_get_info($record['entity_type']);
  656. if ($include_inactive || $entity_info) {
  657. $instance = unserialize($record['data']);
  658. $instance['id'] = $record['id'];
  659. $instance['field_id'] = $record['field_id'];
  660. $instance['field_name'] = $record['field_name'];
  661. $instance['entity_type'] = $record['entity_type'];
  662. $instance['bundle'] = $record['bundle'];
  663. $instance['deleted'] = $record['deleted'];
  664. module_invoke_all('field_read_instance', $instance);
  665. $instances[] = $instance;
  666. }
  667. }
  668. return $instances;
  669. }
  670. /**
  671. * Marks a field instance and its data for deletion.
  672. *
  673. * @param $instance
  674. * An instance structure.
  675. * @param $field_cleanup
  676. * If TRUE, the field will be deleted as well if its last instance is being
  677. * deleted. If FALSE, it is the caller's responsibility to handle the case of
  678. * fields left without instances. Defaults to TRUE.
  679. */
  680. function field_delete_instance($instance, $field_cleanup = TRUE) {
  681. // Mark the field instance for deletion.
  682. db_update('field_config_instance')
  683. ->fields(array('deleted' => 1))
  684. ->condition('field_name', $instance['field_name'])
  685. ->condition('entity_type', $instance['entity_type'])
  686. ->condition('bundle', $instance['bundle'])
  687. ->execute();
  688. // Clear the cache.
  689. field_cache_clear();
  690. // Mark instance data for deletion.
  691. $field = field_info_field($instance['field_name']);
  692. module_invoke($field['storage']['module'], 'field_storage_delete_instance', $instance);
  693. // Let modules react to the deletion of the instance.
  694. module_invoke_all('field_delete_instance', $instance);
  695. // Delete the field itself if we just deleted its last instance.
  696. if ($field_cleanup && count($field['bundles']) == 0) {
  697. field_delete_field($field['field_name']);
  698. }
  699. }
  700. /**
  701. * @} End of "defgroup field_crud".
  702. */
  703. /**
  704. * @defgroup field_purge Field API bulk data deletion
  705. * @{
  706. * Clean up after Field API bulk deletion operations.
  707. *
  708. * Field API provides functions for deleting data attached to individual
  709. * entities as well as deleting entire fields or field instances in a single
  710. * operation.
  711. *
  712. * Deleting field data items for an entity with field_attach_delete() involves
  713. * three separate operations:
  714. * - Invoking the Field Type API hook_field_delete() for each field on the
  715. * entity. The hook for each field type receives the entity and the specific
  716. * field being deleted. A file field module might use this hook to delete
  717. * uploaded files from the filesystem.
  718. * - Invoking the Field Storage API hook_field_storage_delete() to remove
  719. * data from the primary field storage. The hook implementation receives the
  720. * entity being deleted and deletes data for all of the entity's bundle's
  721. * fields.
  722. * - Invoking the global Field Attach API hook_field_attach_delete() for all
  723. * modules that implement it. Each hook implementation receives the entity
  724. * being deleted and can operate on whichever subset of the entity's bundle's
  725. * fields it chooses to.
  726. *
  727. * These hooks are invoked immediately when field_attach_delete() is
  728. * called. Similar operations are performed for field_attach_delete_revision().
  729. *
  730. * When a field, bundle, or field instance is deleted, it is not practical to
  731. * invoke these hooks immediately on every affected entity in a single page
  732. * request; there could be thousands or millions of them. Instead, the
  733. * appropriate field data items, instances, and/or fields are marked as deleted
  734. * so that subsequent load or query operations will not return them. Later, a
  735. * separate process cleans up, or "purges", the marked-as-deleted data by going
  736. * through the three-step process described above and, finally, removing
  737. * deleted field and instance records.
  738. *
  739. * Purging field data is made somewhat tricky by the fact that, while
  740. * field_attach_delete() has a complete entity to pass to the various deletion
  741. * hooks, the Field API purge process only has the field data it has previously
  742. * stored. It cannot reconstruct complete original entities to pass to the
  743. * deletion hooks. It is even possible that the original entity to which some
  744. * Field API data was attached has been itself deleted before the field purge
  745. * operation takes place.
  746. *
  747. * Field API resolves this problem by using "pseudo-entities" during purge
  748. * operations. A pseudo-entity contains only the information from the original
  749. * entity that Field API knows about: entity type, id, revision id, and
  750. * bundle. It also contains the field data for whichever field instance is
  751. * currently being purged. For example, suppose that the node type 'story' used
  752. * to contain a field called 'subtitle' but the field was deleted. If node 37
  753. * was a story with a subtitle, the pseudo-entity passed to the purge hooks
  754. * would look something like this:
  755. *
  756. * @code
  757. * $entity = stdClass Object(
  758. * [nid] => 37,
  759. * [vid] => 37,
  760. * [type] => 'story',
  761. * [subtitle] => array(
  762. * [0] => array(
  763. * 'value' => 'subtitle text',
  764. * ),
  765. * ),
  766. * );
  767. * @endcode
  768. *
  769. * See @link field Field API @endlink for information about the other parts of
  770. * the Field API.
  771. */
  772. /**
  773. * Purges a batch of deleted Field API data, instances, or fields.
  774. *
  775. * This function will purge deleted field data in batches. The batch size
  776. * is defined as an argument to the function, and once each batch is finished,
  777. * it continues with the next batch until all have completed. If a deleted field
  778. * instance with no remaining data records is found, the instance itself will
  779. * be purged. If a deleted field with no remaining field instances is found, the
  780. * field itself will be purged.
  781. *
  782. * @param $batch_size
  783. * The maximum number of field data records to purge before returning.
  784. */
  785. function field_purge_batch($batch_size) {
  786. // Retrieve all deleted field instances. We cannot use field_info_instances()
  787. // because that function does not return deleted instances.
  788. $instances = field_read_instances(array('deleted' => 1), array('include_deleted' => 1));
  789. foreach ($instances as $instance) {
  790. // field_purge_data() will need the field array.
  791. $field = field_info_field_by_id($instance['field_id']);
  792. // Retrieve some entities.
  793. $query = new EntityFieldQuery();
  794. $results = $query
  795. ->fieldCondition($field)
  796. ->entityCondition('bundle', $instance['bundle'])
  797. ->deleted(TRUE)
  798. ->range(0, $batch_size)
  799. ->execute();
  800. if ($results) {
  801. foreach ($results as $entity_type => $stub_entities) {
  802. field_attach_load($entity_type, $stub_entities, FIELD_LOAD_CURRENT, array('field_id' => $field['id'], 'deleted' => 1));
  803. foreach ($stub_entities as $stub_entity) {
  804. // Purge the data for the entity.
  805. field_purge_data($entity_type, $stub_entity, $field, $instance);
  806. }
  807. }
  808. }
  809. else {
  810. // No field data remains for the instance, so we can remove it.
  811. field_purge_instance($instance);
  812. }
  813. }
  814. // Retrieve all deleted fields. Any that have no instances can be purged.
  815. $fields = field_read_fields(array('deleted' => 1), array('include_deleted' => 1));
  816. foreach ($fields as $field) {
  817. $instances = field_read_instances(array('field_id' => $field['id']), array('include_deleted' => 1));
  818. if (empty($instances)) {
  819. field_purge_field($field);
  820. }
  821. }
  822. }
  823. /**
  824. * Purges the field data for a single field on a single pseudo-entity.
  825. *
  826. * This is basically the same as field_attach_delete() except it only applies
  827. * to a single field. The entity itself is not being deleted, and it is quite
  828. * possible that other field data will remain attached to it.
  829. *
  830. * @param $entity_type
  831. * The type of $entity; e.g. 'node' or 'user'.
  832. * @param $entity
  833. * The pseudo-entity whose field data is being purged.
  834. * @param $field
  835. * The (possibly deleted) field whose data is being purged.
  836. * @param $instance
  837. * The deleted field instance whose data is being purged.
  838. */
  839. function field_purge_data($entity_type, $entity, $field, $instance) {
  840. // Each field type's hook_field_delete() only expects to operate on a single
  841. // field at a time, so we can use it as-is for purging.
  842. $options = array('field_id' => $instance['field_id'], 'deleted' => TRUE);
  843. _field_invoke('delete', $entity_type, $entity, $dummy, $dummy, $options);
  844. // Tell the field storage system to purge the data.
  845. module_invoke($field['storage']['module'], 'field_storage_purge', $entity_type, $entity, $field, $instance);
  846. // Let other modules act on purging the data.
  847. foreach (module_implements('field_attach_purge') as $module) {
  848. $function = $module . '_field_attach_purge';
  849. $function($entity_type, $entity, $field, $instance);
  850. }
  851. }
  852. /**
  853. * Purges a field instance record from the database.
  854. *
  855. * This function assumes all data for the instance has already been purged, and
  856. * should only be called by field_purge_batch().
  857. *
  858. * @param $instance
  859. * The instance record to purge.
  860. */
  861. function field_purge_instance($instance) {
  862. db_delete('field_config_instance')
  863. ->condition('id', $instance['id'])
  864. ->execute();
  865. // Notify the storage engine.
  866. $field = field_info_field_by_id($instance['field_id']);
  867. module_invoke($field['storage']['module'], 'field_storage_purge_instance', $instance);
  868. // Clear the cache.
  869. field_info_cache_clear();
  870. // Invoke external hooks after the cache is cleared for API consistency.
  871. module_invoke_all('field_purge_instance', $instance);
  872. }
  873. /**
  874. * Purges a field record from the database.
  875. *
  876. * This function assumes all instances for the field has already been purged,
  877. * and should only be called by field_purge_batch().
  878. *
  879. * @param $field
  880. * The field record to purge.
  881. */
  882. function field_purge_field($field) {
  883. $instances = field_read_instances(array('field_id' => $field['id']), array('include_deleted' => 1));
  884. if (count($instances) > 0) {
  885. throw new FieldException(t('Attempt to purge a field @field_name that still has instances.', array('@field_name' => $field['field_name'])));
  886. }
  887. db_delete('field_config')
  888. ->condition('id', $field['id'])
  889. ->execute();
  890. // Notify the storage engine.
  891. module_invoke($field['storage']['module'], 'field_storage_purge_field', $field);
  892. // Clear the cache.
  893. field_info_cache_clear();
  894. // Invoke external hooks after the cache is cleared for API consistency.
  895. module_invoke_all('field_purge_field', $field);
  896. }
  897. /**
  898. * @} End of "defgroup field_purge".
  899. */