| Drupal | PHP Cross Reference | Content Management Systems |
1 <?php 2 3 /** 4 * @file 5 * Field attach API, allowing entities (nodes, users, ...) to be 'fieldable'. 6 */ 7 8 /** 9 * Exception thrown by field_attach_validate() on field validation errors. 10 */ 11 class FieldValidationException extends FieldException { 12 var $errors; 13 14 /** 15 * Constructor for FieldValidationException. 16 * 17 * @param $errors 18 * An array of field validation errors, keyed by field name and 19 * delta that contains two keys: 20 * - 'error': A machine-readable error code string, prefixed by 21 * the field module name. A field widget may use this code to decide 22 * how to report the error. 23 * - 'message': A human-readable error message such as to be 24 * passed to form_error() for the appropriate form element. 25 */ 26 function __construct($errors) { 27 $this->errors = $errors; 28 parent::__construct(t('Field validation errors')); 29 } 30 } 31 32 /** 33 * @defgroup field_storage Field Storage API 34 * @{ 35 * Implement a storage engine for Field API data. 36 * 37 * The Field Attach API uses the Field Storage API to perform all "database 38 * access". Each Field Storage API hook function defines a primitive database 39 * operation such as read, write, or delete. The default field storage module, 40 * field_sql_storage.module, uses the local SQL database to implement these 41 * operations, but alternative field storage backends can choose to represent 42 * the data in SQL differently or use a completely different storage mechanism 43 * such as a cloud-based database. 44 * 45 * Each field defines which storage backend it uses. The Drupal system variable 46 * 'field_storage_default' identifies the storage backend used by default. 47 * 48 * See @link field Field API @endlink for information about the other parts of 49 * the Field API. 50 */ 51 52 /** 53 * Argument for an update operation. 54 * 55 * This is used in hook_field_storage_write when updating an 56 * existing entity. 57 */ 58 define('FIELD_STORAGE_UPDATE', 'update'); 59 60 /** 61 * Argument for an insert operation. 62 * 63 * This is used in hook_field_storage_write when inserting a new entity. 64 */ 65 define('FIELD_STORAGE_INSERT', 'insert'); 66 67 /** 68 * @} End of "defgroup field_storage". 69 */ 70 71 /** 72 * @defgroup field_attach Field Attach API 73 * @{ 74 * Operate on Field API data attached to Drupal entities. 75 * 76 * Field Attach API functions load, store, display, generate Field API 77 * structures, and perform a variety of other functions for field data attached 78 * to individual entities. 79 * 80 * Field Attach API functions generally take $entity_type and $entity arguments 81 * along with additional function-specific arguments. $entity_type is the type 82 * of the fieldable entity, such as 'node' or 'user', and $entity is the entity 83 * itself. 84 * 85 * hook_entity_info() is the central place for entity types to define if and 86 * how Field API should operate on their entity objects. Notably, the 87 * 'fieldable' property needs to be set to TRUE. 88 * 89 * The Field Attach API uses the concept of bundles: the set of fields for a 90 * given entity is defined on a per-bundle basis. The collection of bundles for 91 * an entity type is defined its hook_entity_info() implementation. For 92 * instance, node_entity_info() exposes each node type as its own bundle. This 93 * means that the set of fields of a node is determined by the node type. The 94 * Field API reads the bundle name for a given entity from a particular 95 * property of the entity object, and hook_entity_info() defines which property 96 * to use. For instance, node_entity_info() specifies: 97 * @code $info['entity keys']['bundle'] = 'type'@endcode 98 * This indicates that for a particular node object, the bundle name can be 99 * found in $node->type. This property can be omitted if the entity type only 100 * exposes a single bundle (all entities of this type have the same collection 101 * of fields). This is the case for the 'user' entity type. 102 * 103 * Most Field Attach API functions define a corresponding hook function that 104 * allows any module to act on Field Attach operations for any entity after the 105 * operation is complete, and access or modify all the field, form, or display 106 * data for that entity and operation. For example, field_attach_view() invokes 107 * hook_field_attach_view_alter(). These all-module hooks are distinct from 108 * those of the Field Types API, such as hook_field_load(), that are only 109 * invoked for the module that defines a specific field type. 110 * 111 * field_attach_load(), field_attach_insert(), and field_attach_update() also 112 * define pre-operation hooks, e.g. hook_field_attach_pre_load(). These hooks 113 * run before the corresponding Field Storage API and Field Type API 114 * operations. They allow modules to define additional storage locations (e.g. 115 * denormalizing, mirroring) for field data on a per-field basis. They also 116 * allow modules to take over field storage completely by instructing other 117 * implementations of the same hook and the Field Storage API itself not to 118 * operate on specified fields. 119 * 120 * The pre-operation hooks do not make the Field Storage API irrelevant. The 121 * Field Storage API is essentially the "fallback mechanism" for any fields 122 * that aren't being intercepted explicitly by pre-operation hooks. 123 * 124 * @link field_language Field Language API @endlink provides information about 125 * the structure of field objects. 126 * 127 * See @link field Field API @endlink for information about the other parts of 128 * the Field API. 129 */ 130 131 /** 132 * Invoke a field hook. 133 * 134 * @param $op 135 * Possible operations include: 136 * - form 137 * - validate 138 * - presave 139 * - insert 140 * - update 141 * - delete 142 * - delete revision 143 * - view 144 * - prepare translation 145 * @param $entity_type 146 * The type of $entity; e.g. 'node' or 'user'. 147 * @param $entity 148 * The fully formed $entity_type entity. 149 * @param $a 150 * - The $form in the 'form' operation. 151 * - The value of $view_mode in the 'view' operation. 152 * - Otherwise NULL. 153 * @param $b 154 * - The $form_state in the 'submit' operation. 155 * - Otherwise NULL. 156 * @param $options 157 * An associative array of additional options, with the following keys: 158 * - 'field_name': The name of the field whose operation should be 159 * invoked. By default, the operation is invoked on all the fields 160 * in the entity's bundle. NOTE: This option is not compatible with 161 * the 'deleted' option; the 'field_id' option should be used 162 * instead. 163 * - 'field_id': The id of the field whose operation should be 164 * invoked. By default, the operation is invoked on all the fields 165 * in the entity's' bundles. 166 * - 'default': A boolean value, specifying which implementation of 167 * the operation should be invoked. 168 * - if FALSE (default), the field types implementation of the operation 169 * will be invoked (hook_field_[op]) 170 * - If TRUE, the default field implementation of the field operation 171 * will be invoked (field_default_[op]) 172 * Internal use only. Do not explicitely set to TRUE, but use 173 * _field_invoke_default() instead. 174 * - 'deleted': If TRUE, the function will operate on deleted fields 175 * as well as non-deleted fields. If unset or FALSE, only 176 * non-deleted fields are operated on. 177 * - 'language': A language code or an array of language codes keyed by field 178 * name. It will be used to narrow down to a single value the available 179 * languages to act on. 180 */ 181 function _field_invoke($op, $entity_type, $entity, &$a = NULL, &$b = NULL, $options = array()) { 182 // Merge default options. 183 $default_options = array( 184 'default' => FALSE, 185 'deleted' => FALSE, 186 'language' => NULL, 187 ); 188 $options += $default_options; 189 190 // Determine the list of instances to iterate on. 191 list(, , $bundle) = entity_extract_ids($entity_type, $entity); 192 $instances = _field_invoke_get_instances($entity_type, $bundle, $options); 193 194 // Iterate through the instances and collect results. 195 $return = array(); 196 foreach ($instances as $instance) { 197 // field_info_field() is not available for deleted fields, so use 198 // field_info_field_by_id(). 199 $field = field_info_field_by_id($instance['field_id']); 200 $field_name = $field['field_name']; 201 $function = $options['default'] ? 'field_default_' . $op : $field['module'] . '_field_' . $op; 202 if (function_exists($function)) { 203 // Determine the list of languages to iterate on. 204 $available_languages = field_available_languages($entity_type, $field); 205 $languages = _field_language_suggestion($available_languages, $options['language'], $field_name); 206 207 foreach ($languages as $langcode) { 208 $items = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array(); 209 $result = $function($entity_type, $entity, $field, $instance, $langcode, $items, $a, $b); 210 if (isset($result)) { 211 // For hooks with array results, we merge results together. 212 // For hooks with scalar results, we collect results in an array. 213 if (is_array($result)) { 214 $return = array_merge($return, $result); 215 } 216 else { 217 $return[] = $result; 218 } 219 } 220 221 // Populate $items back in the field values, but avoid replacing missing 222 // fields with an empty array (those are not equivalent on update). 223 if ($items !== array() || isset($entity->{$field_name}[$langcode])) { 224 $entity->{$field_name}[$langcode] = $items; 225 } 226 } 227 } 228 } 229 230 return $return; 231 } 232 233 /** 234 * Invoke a field hook across fields on multiple entities. 235 * 236 * @param $op 237 * Possible operations include: 238 * - load 239 * - prepare_view 240 * For all other operations, use _field_invoke() / field_invoke_default() 241 * instead. 242 * @param $entity_type 243 * The type of $entity; e.g. 'node' or 'user'. 244 * @param $entities 245 * An array of entities, keyed by entity id. 246 * @param $a 247 * - The $age parameter in the 'load' operation. 248 * - Otherwise NULL. 249 * @param $b 250 * Currently always NULL. 251 * @param $options 252 * An associative array of additional options, with the following keys: 253 * - 'field_name': The name of the field whose operation should be 254 * invoked. By default, the operation is invoked on all the fields 255 * in the entity's bundle. NOTE: This option is not compatible with 256 * the 'deleted' option; the 'field_id' option should be used instead. 257 * - 'field_id': The id of the field whose operation should be 258 * invoked. By default, the operation is invoked on all the fields 259 * in the entity's' bundles. 260 * - 'default': A boolean value, specifying which implementation of 261 * the operation should be invoked. 262 * - if FALSE (default), the field types implementation of the operation 263 * will be invoked (hook_field_[op]) 264 * - If TRUE, the default field implementation of the field operation 265 * will be invoked (field_default_[op]) 266 * Internal use only. Do not explicitely set to TRUE, but use 267 * _field_invoke_multiple_default() instead. 268 * - 'deleted': If TRUE, the function will operate on deleted fields 269 * as well as non-deleted fields. If unset or FALSE, only 270 * non-deleted fields are operated on. 271 * - 'language': A language code or an array of arrays of language codes keyed 272 * by entity id and field name. It will be used to narrow down to a single 273 * value the available languages to act on. 274 * 275 * @return 276 * An array of returned values keyed by entity id. 277 */ 278 function _field_invoke_multiple($op, $entity_type, $entities, &$a = NULL, &$b = NULL, $options = array()) { 279 // Merge default options. 280 $default_options = array( 281 'default' => FALSE, 282 'deleted' => FALSE, 283 'language' => NULL, 284 ); 285 $options += $default_options; 286 $field_info = field_info_field_by_ids(); 287 288 $fields = array(); 289 $grouped_instances = array(); 290 $grouped_entities = array(); 291 $grouped_items = array(); 292 $return = array(); 293 294 // Go through the entities and collect the fields on which the hook should be 295 // invoked. 296 // 297 // We group fields by id, not by name, because this function can operate on 298 // deleted fields which may have non-unique names. However, entities can only 299 // contain data for a single field for each name, even if that field 300 // is deleted, so we reference field data via the 301 // $entity->$field_name property. 302 foreach ($entities as $entity) { 303 // Determine the list of instances to iterate on. 304 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); 305 $instances = _field_invoke_get_instances($entity_type, $bundle, $options); 306 307 foreach ($instances as $instance) { 308 $field_id = $instance['field_id']; 309 $field_name = $instance['field_name']; 310 $field = $field_info[$field_id]; 311 $function = $options['default'] ? 'field_default_' . $op : $field['module'] . '_field_' . $op; 312 if (function_exists($function)) { 313 // Add the field to the list of fields to invoke the hook on. 314 if (!isset($fields[$field_id])) { 315 $fields[$field_id] = $field; 316 } 317 // Extract the field values into a separate variable, easily accessed 318 // by hook implementations. 319 // Unless a language suggestion is provided we iterate on all the 320 // available languages. 321 $available_languages = field_available_languages($entity_type, $field); 322 $language = !empty($options['language'][$id]) ? $options['language'][$id] : $options['language']; 323 $languages = _field_language_suggestion($available_languages, $language, $field_name); 324 foreach ($languages as $langcode) { 325 $grouped_items[$field_id][$langcode][$id] = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array(); 326 // Group the instances and entities corresponding to the current 327 // field. 328 $grouped_instances[$field_id][$langcode][$id] = $instance; 329 $grouped_entities[$field_id][$langcode][$id] = $entities[$id]; 330 } 331 } 332 } 333 // Initialize the return value for each entity. 334 $return[$id] = array(); 335 } 336 337 // For each field, invoke the field hook and collect results. 338 foreach ($fields as $field_id => $field) { 339 $field_name = $field['field_name']; 340 $function = $options['default'] ? 'field_default_' . $op : $field['module'] . '_field_' . $op; 341 // Iterate over all the field translations. 342 foreach ($grouped_items[$field_id] as $langcode => &$items) { 343 $entities = $grouped_entities[$field_id][$langcode]; 344 $instances = $grouped_instances[$field_id][$langcode]; 345 $results = $function($entity_type, $entities, $field, $instances, $langcode, $items, $a, $b); 346 if (isset($results)) { 347 // Collect results by entity. 348 // For hooks with array results, we merge results together. 349 // For hooks with scalar results, we collect results in an array. 350 foreach ($results as $id => $result) { 351 if (is_array($result)) { 352 $return[$id] = array_merge($return[$id], $result); 353 } 354 else { 355 $return[$id][] = $result; 356 } 357 } 358 } 359 } 360 361 // Populate field values back in the entities, but avoid replacing missing 362 // fields with an empty array (those are not equivalent on update). 363 foreach ($grouped_entities[$field_id] as $langcode => $entities) { 364 foreach ($entities as $id => $entity) { 365 if ($grouped_items[$field_id][$langcode][$id] !== array() || isset($entity->{$field_name}[$langcode])) { 366 $entity->{$field_name}[$langcode] = $grouped_items[$field_id][$langcode][$id]; 367 } 368 } 369 } 370 } 371 372 return $return; 373 } 374 375 /** 376 * Invoke field.module's version of a field hook. 377 * 378 * This function invokes the field_default_[op]() function. 379 * Use _field_invoke() to invoke the field type implementation, 380 * hook_field_[op](). 381 * 382 * @see _field_invoke() 383 */ 384 function _field_invoke_default($op, $entity_type, $entity, &$a = NULL, &$b = NULL, $options = array()) { 385 $options['default'] = TRUE; 386 return _field_invoke($op, $entity_type, $entity, $a, $b, $options); 387 } 388 389 /** 390 * Invoke field.module's version of a field hook on multiple entities. 391 * 392 * This function invokes the field_default_[op]() function. 393 * Use _field_invoke_multiple() to invoke the field type implementation, 394 * hook_field_[op](). 395 * 396 * @see _field_invoke_multiple() 397 */ 398 function _field_invoke_multiple_default($op, $entity_type, $entities, &$a = NULL, &$b = NULL, $options = array()) { 399 $options['default'] = TRUE; 400 return _field_invoke_multiple($op, $entity_type, $entities, $a, $b, $options); 401 } 402 403 /** 404 * Helper for _field_invoke(): retrieves a list of instances to operate on. 405 * 406 * @param $entity_type 407 * The entity type. 408 * @param $bundle 409 * The bundle name. 410 * @param $options 411 * An associative array of options, as provided to _field_invoke(). Only the 412 * following keys are considered : 413 * - deleted 414 * - field_name 415 * - field_id 416 * See _field_invoke() for details. 417 * 418 * @return 419 * The array of selected instance definitions. 420 */ 421 function _field_invoke_get_instances($entity_type, $bundle, $options) { 422 if ($options['deleted']) { 423 // Deleted fields are not included in field_info_instances(), and need to 424 // be fetched from the database with field_read_instances(). 425 $params = array('entity_type' => $entity_type, 'bundle' => $bundle); 426 if (isset($options['field_id'])) { 427 // Single-field mode by field id: field_read_instances() does the filtering. 428 // Single-field mode by field name is not compatible with the 'deleted' 429 // option. 430 $params['field_id'] = $options['field_id']; 431 } 432 $instances = field_read_instances($params, array('include_deleted' => TRUE)); 433 } 434 elseif (isset($options['field_name'])) { 435 // Single-field mode by field name: field_info_instance() does the 436 // filtering. 437 $instances = array(field_info_instance($entity_type, $options['field_name'], $bundle)); 438 } 439 else { 440 $instances = field_info_instances($entity_type, $bundle); 441 if (isset($options['field_id'])) { 442 // Single-field mode by field id: we need to loop on each instance to 443 // find the right one. 444 foreach ($instances as $instance) { 445 if ($instance['field_id'] == $options['field_id']) { 446 $instances = array($instance); 447 break; 448 } 449 } 450 } 451 } 452 453 return $instances; 454 } 455 456 /** 457 * Add form elements for all fields for an entity to a form structure. 458 * 459 * The form elements for the entity's fields are added by reference as direct 460 * children in the $form parameter. This parameter can be a full form structure 461 * (most common case for entity edit forms), or a sub-element of a larger form. 462 * 463 * By default, submitted field values appear at the top-level of 464 * $form_state['values']. A different location within $form_state['values'] can 465 * be specified by setting the '#parents' property on the incoming $form 466 * parameter. Because of name clashes, two instances of the same field cannot 467 * appear within the same $form element, or within the same '#parents' space. 468 * 469 * For each call to field_attach_form(), field values are processed by calling 470 * field_attach_form_validate() and field_attach_submit() on the same $form 471 * element. 472 * 473 * Sample resulting structure in $form: 474 * @code 475 * '#parents' => The location of field values in $form_state['values'], 476 * '#entity_type' => The name of the entity type, 477 * '#bundle' => The name of the bundle, 478 * // One sub-array per field appearing in the entity, keyed by field name. 479 * // The structure of the array differs slightly depending on whether the 480 * // widget is 'single-value' (provides the input for one field value, 481 * // most common case), and will therefore be repeated as many times as 482 * // needed, or 'multiple-values' (one single widget allows the input of 483 * // several values, e.g checkboxes, select box...). 484 * // The sub-array is nested into a $langcode key where $langcode has the 485 * // same value of the $langcode parameter above. 486 * // The '#language' key holds the same value of $langcode and it is used 487 * // to access the field sub-array when $langcode is unknown. 488 * 'field_foo' => array( 489 * '#tree' => TRUE, 490 * '#field_name' => The name of the field, 491 * '#language' => $langcode, 492 * $langcode => array( 493 * '#field_name' => The name of the field, 494 * '#language' => $langcode, 495 * '#field_parents' => The 'parents' space for the field in the form, 496 * equal to the #parents property of the $form parameter received by 497 * field_attach_form(), 498 * '#required' => Whether or not the field is required, 499 * '#title' => The label of the field instance, 500 * '#description' => The description text for the field instance, 501 * 502 * // Only for 'single' widgets: 503 * '#theme' => 'field_multiple_value_form', 504 * '#cardinality' => The field cardinality, 505 * // One sub-array per copy of the widget, keyed by delta. 506 * 0 => array( 507 * '#entity_type' => The name of the entity type, 508 * '#bundle' => The name of the bundle, 509 * '#field_name' => The name of the field, 510 * '#field_parents' => The 'parents' space for the field in the form, 511 * equal to the #parents property of the $form parameter received by 512 * field_attach_form(), 513 * '#title' => The title to be displayed by the widget, 514 * '#default_value' => The field value for delta 0, 515 * '#required' => Whether the widget should be marked required, 516 * '#delta' => 0, 517 * '#columns' => The array of field columns, 518 * // The remaining elements in the sub-array depend on the widget. 519 * '#type' => The type of the widget, 520 * ... 521 * ), 522 * 1 => array( 523 * ... 524 * ), 525 * 526 * // Only for multiple widgets: 527 * '#entity_type' => The name of the entity type, 528 * '#bundle' => $instance['bundle'], 529 * '#columns' => array_keys($field['columns']), 530 * // The remaining elements in the sub-array depend on the widget. 531 * '#type' => The type of the widget, 532 * ... 533 * ), 534 * ... 535 * ), 536 * ) 537 * @endcode 538 * 539 * Additionally, some processing data is placed in $form_state, and can be 540 * accessed by field_form_get_state() and field_form_set_state(). 541 * 542 * @param $entity_type 543 * The type of $entity; e.g. 'node' or 'user'. 544 * @param $entity 545 * The entity for which to load form elements, used to initialize 546 * default form values. 547 * @param $form 548 * The form structure to fill in. This can be a full form structure, or a 549 * sub-element of a larger form. The #parents property can be set to control 550 * the location of submitted field values within $form_state['values']. If 551 * not specified, $form['#parents'] is set to an empty array, placing field 552 * values at the top-level of $form_state['values']. 553 * @param $form_state 554 * An associative array containing the current state of the form. 555 * @param $langcode 556 * The language the field values are going to be entered, if no language 557 * is provided the default site language will be used. 558 * 559 * @see field_form_get_state() 560 * @see field_form_set_state() 561 */ 562 function field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode = NULL) { 563 // Set #parents to 'top-level' by default. 564 $form += array('#parents' => array()); 565 566 // If no language is provided use the default site language. 567 $options = array('language' => field_valid_language($langcode)); 568 $form += (array) _field_invoke_default('form', $entity_type, $entity, $form, $form_state, $options); 569 570 // Add custom weight handling. 571 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); 572 $form['#pre_render'][] = '_field_extra_fields_pre_render'; 573 $form['#entity_type'] = $entity_type; 574 $form['#bundle'] = $bundle; 575 576 // Let other modules make changes to the form. 577 // Avoid module_invoke_all() to let parameters be taken by reference. 578 foreach (module_implements('field_attach_form') as $module) { 579 $function = $module . '_field_attach_form'; 580 $function($entity_type, $entity, $form, $form_state, $langcode); 581 } 582 } 583 584 /** 585 * Loads fields for the current revisions of a group of entities. 586 * 587 * Loads all fields for each entity object in a group of a single entity type. 588 * The loaded field values are added directly to the entity objects. 589 * 590 * field_attach_load() is automatically called by the default entity controller 591 * class, and thus, in most cases, doesn't need to be explicitly called by the 592 * entity type module. 593 * 594 * @param $entity_type 595 * The type of $entity; e.g., 'node' or 'user'. 596 * @param $entities 597 * An array of entities for which to load fields, keyed by entity ID. 598 * Each entity needs to have its 'bundle', 'id' and (if applicable) 599 * 'revision' keys filled in. The function adds the loaded field data 600 * directly in the entity objects of the $entities array. 601 * @param $age 602 * FIELD_LOAD_CURRENT to load the most recent revision for all 603 * fields, or FIELD_LOAD_REVISION to load the version indicated by 604 * each entity. Defaults to FIELD_LOAD_CURRENT; use 605 * field_attach_load_revision() instead of passing FIELD_LOAD_REVISION. 606 * @param $options 607 * An associative array of additional options, with the following keys: 608 * - 'field_id': The field ID that should be loaded, instead of 609 * loading all fields, for each entity. Note that returned entities 610 * may contain data for other fields, for example if they are read 611 * from a cache. 612 * - 'deleted': If TRUE, the function will operate on deleted fields 613 * as well as non-deleted fields. If unset or FALSE, only 614 * non-deleted fields are operated on. 615 */ 616 function field_attach_load($entity_type, $entities, $age = FIELD_LOAD_CURRENT, $options = array()) { 617 $field_info = field_info_field_by_ids(); 618 $load_current = $age == FIELD_LOAD_CURRENT; 619 620 // Merge default options. 621 $default_options = array( 622 'deleted' => FALSE, 623 ); 624 $options += $default_options; 625 626 $info = entity_get_info($entity_type); 627 // Only the most current revision of non-deleted fields for cacheable entity 628 // types can be cached. 629 $cache_read = $load_current && $info['field cache'] && empty($options['deleted']); 630 // In addition, do not write to the cache when loading a single field. 631 $cache_write = $cache_read && !isset($options['field_id']); 632 633 if (empty($entities)) { 634 return; 635 } 636 637 // Assume all entities will need to be queried. Entities found in the cache 638 // will be removed from the list. 639 $queried_entities = $entities; 640 641 // Fetch available entities from cache, if applicable. 642 if ($cache_read) { 643 // Build the list of cache entries to retrieve. 644 $cids = array(); 645 foreach ($entities as $id => $entity) { 646 $cids[] = "field:$entity_type:$id"; 647 } 648 $cache = cache_get_multiple($cids, 'cache_field'); 649 // Put the cached field values back into the entities and remove them from 650 // the list of entities to query. 651 foreach ($entities as $id => $entity) { 652 $cid = "field:$entity_type:$id"; 653 if (isset($cache[$cid])) { 654 unset($queried_entities[$id]); 655 foreach ($cache[$cid]->data as $field_name => $values) { 656 $entity->$field_name = $values; 657 } 658 } 659 } 660 } 661 662 // Fetch other entities from their storage location. 663 if ($queried_entities) { 664 // The invoke order is: 665 // - hook_field_storage_pre_load() 666 // - storage backend's hook_field_storage_load() 667 // - field-type module's hook_field_load() 668 // - hook_field_attach_load() 669 670 // Invoke hook_field_storage_pre_load(): let any module load field 671 // data before the storage engine, accumulating along the way. 672 $skip_fields = array(); 673 foreach (module_implements('field_storage_pre_load') as $module) { 674 $function = $module . '_field_storage_pre_load'; 675 $function($entity_type, $queried_entities, $age, $skip_fields, $options); 676 } 677 678 $instances = array(); 679 680 // Collect the storage backends used by the remaining fields in the entities. 681 $storages = array(); 682 foreach ($queried_entities as $entity) { 683 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); 684 $instances = _field_invoke_get_instances($entity_type, $bundle, $options); 685 686 foreach ($instances as $instance) { 687 $field_name = $instance['field_name']; 688 $field_id = $instance['field_id']; 689 // Make sure all fields are present at least as empty arrays. 690 if (!isset($queried_entities[$id]->{$field_name})) { 691 $queried_entities[$id]->{$field_name} = array(); 692 } 693 // Collect the storage backend if the field has not been loaded yet. 694 if (!isset($skip_fields[$field_id])) { 695 $field = $field_info[$field_id]; 696 $storages[$field['storage']['type']][$field_id][] = $load_current ? $id : $vid; 697 } 698 } 699 } 700 701 // Invoke hook_field_storage_load() on the relevant storage backends. 702 foreach ($storages as $storage => $fields) { 703 $storage_info = field_info_storage_types($storage); 704 module_invoke($storage_info['module'], 'field_storage_load', $entity_type, $queried_entities, $age, $fields, $options); 705 } 706 707 // Invoke field-type module's hook_field_load(). 708 $null = NULL; 709 _field_invoke_multiple('load', $entity_type, $queried_entities, $age, $null, $options); 710 711 // Invoke hook_field_attach_load(): let other modules act on loading the 712 // entitiy. 713 module_invoke_all('field_attach_load', $entity_type, $queried_entities, $age, $options); 714 715 // Build cache data. 716 if ($cache_write) { 717 foreach ($queried_entities as $id => $entity) { 718 $data = array(); 719 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); 720 $instances = field_info_instances($entity_type, $bundle); 721 foreach ($instances as $instance) { 722 $data[$instance['field_name']] = $queried_entities[$id]->{$instance['field_name']}; 723 } 724 $cid = "field:$entity_type:$id"; 725 cache_set($cid, $data, 'cache_field'); 726 } 727 } 728 } 729 } 730 731 /** 732 * Load all fields for previous versions of a group of entities. 733 * 734 * Loading different versions of the same entities is not supported, and should 735 * be done by separate calls to the function. 736 * 737 * field_attach_load_revision() is automatically called by the default entity 738 * controller class, and thus, in most cases, doesn't need to be explicitly 739 * called by the entity type module. 740 * 741 * @param $entity_type 742 * The type of $entity; e.g. 'node' or 'user'. 743 * @param $entities 744 * An array of entities for which to load fields, keyed by entity ID. Each 745 * entity needs to have its 'bundle', 'id' and (if applicable) 'revision' 746 * keys filled. The function adds the loaded field data directly in the 747 * entity objects of the $entities array. 748 * @param $options 749 * An associative array of additional options. See field_attach_load() for 750 * details. 751 */ 752 function field_attach_load_revision($entity_type, $entities, $options = array()) { 753 return field_attach_load($entity_type, $entities, FIELD_LOAD_REVISION, $options); 754 } 755 756 /** 757 * Perform field validation against the field data in an entity. 758 * 759 * This function does not perform field widget validation on form 760 * submissions. It is intended to be called during API save 761 * operations. Use field_attach_form_validate() to validate form 762 * submissions. 763 * 764 * @param $entity_type 765 * The type of $entity; e.g. 'node' or 'user'. 766 * @param $entity 767 * The entity with fields to validate. 768 * @throws FieldValidationException 769 * If validation errors are found, a FieldValidationException is thrown. The 770 * 'errors' property contains the array of errors, keyed by field name, 771 * language and delta. 772 */ 773 function field_attach_validate($entity_type, $entity) { 774 $errors = array(); 775 // Check generic, field-type-agnostic errors first. 776 _field_invoke_default('validate', $entity_type, $entity, $errors); 777 // Check field-type specific errors. 778 _field_invoke('validate', $entity_type, $entity, $errors); 779 780 // Let other modules validate the entity. 781 // Avoid module_invoke_all() to let $errors be taken by reference. 782 foreach (module_implements('field_attach_validate') as $module) { 783 $function = $module . '_field_attach_validate'; 784 $function($entity_type, $entity, $errors); 785 } 786 787 if ($errors) { 788 throw new FieldValidationException($errors); 789 } 790 } 791 792 /** 793 * Perform field validation against form-submitted field values. 794 * 795 * There are two levels of validation for fields in forms: widget 796 * validation, and field validation. 797 * - Widget validation steps are specific to a given widget's own form 798 * structure and UI metaphors. They are executed through FAPI's 799 * #element_validate property during normal form validation. 800 * - Field validation steps are common to a given field type, independently of 801 * the specific widget being used in a given form. They are defined in the 802 * field type's implementation of hook_field_validate(). 803 * 804 * This function performs field validation in the context of a form 805 * submission. It converts field validation errors into form errors 806 * on the correct form elements. Fieldable entity types should call 807 * this function during their own form validation function. 808 * 809 * @param $entity_type 810 * The type of $entity; e.g. 'node' or 'user'. 811 * @param $entity 812 * The entity being submitted. The 'bundle', 'id' and (if applicable) 813 * 'revision' keys should be present. The actual field values will be read 814 * from $form_state['values']. 815 * @param $form 816 * The form structure where field elements are attached to. This might be a 817 * full form structure, or a sub-element of a larger form. 818 * @param $form_state 819 * An associative array containing the current state of the form. 820 */ 821 function field_attach_form_validate($entity_type, $entity, $form, &$form_state) { 822 // Extract field values from submitted values. 823 _field_invoke_default('extract_form_values', $entity_type, $entity, $form, $form_state); 824 825 // Perform field_level validation. 826 try { 827 field_attach_validate($entity_type, $entity); 828 } 829 catch (FieldValidationException $e) { 830 // Pass field-level validation errors back to widgets for accurate error 831 // flagging. 832 foreach ($e->errors as $field_name => $field_errors) { 833 foreach ($field_errors as $langcode => $errors) { 834 $field_state = field_form_get_state($form['#parents'], $field_name, $langcode, $form_state); 835 $field_state['errors'] = $errors; 836 field_form_set_state($form['#parents'], $field_name, $langcode, $form_state, $field_state); 837 } 838 } 839 _field_invoke_default('form_errors', $entity_type, $entity, $form, $form_state); 840 } 841 } 842 843 /** 844 * Perform necessary operations on field data submitted by a form. 845 * 846 * Currently, this accounts for drag-and-drop reordering of 847 * field values, and filtering of empty values. 848 * 849 * @param $entity_type 850 * The type of $entity; e.g. 'node' or 'user'. 851 * @param $entity 852 * The entity being submitted. The 'bundle', 'id' and (if applicable) 853 * 'revision' keys should be present. The actual field values will be read 854 * from $form_state['values']. 855 * @param $form 856 * The form structure where field elements are attached to. This might be a 857 * full form structure, or a sub-element of a larger form. 858 * @param $form_state 859 * An associative array containing the current state of the form. 860 */ 861 function field_attach_submit($entity_type, $entity, $form, &$form_state) { 862 // Extract field values from submitted values. 863 _field_invoke_default('extract_form_values', $entity_type, $entity, $form, $form_state); 864 865 _field_invoke_default('submit', $entity_type, $entity, $form, $form_state); 866 867 // Let other modules act on submitting the entity. 868 // Avoid module_invoke_all() to let $form_state be taken by reference. 869 foreach (module_implements('field_attach_submit') as $module) { 870 $function = $module . '_field_attach_submit'; 871 $function($entity_type, $entity, $form, $form_state); 872 } 873 } 874 875 /** 876 * Perform necessary operations just before fields data get saved. 877 * 878 * We take no specific action here, we just give other 879 * modules the opportunity to act. 880 * 881 * @param $entity_type 882 * The type of $entity; e.g. 'node' or 'user'. 883 * @param $entity 884 * The entity with fields to process. 885 */ 886 function field_attach_presave($entity_type, $entity) { 887 _field_invoke('presave', $entity_type, $entity); 888 889 // Let other modules act on presaving the entity. 890 module_invoke_all('field_attach_presave', $entity_type, $entity); 891 } 892 893 /** 894 * Save field data for a new entity. 895 * 896 * The passed-in entity must already contain its id and (if applicable) 897 * revision id attributes. 898 * Default values (if any) will be saved for fields not present in the 899 * $entity. 900 * 901 * @param $entity_type 902 * The type of $entity; e.g. 'node' or 'user'. 903 * @param $entity 904 * The entity with fields to save. 905 * @return 906 * Default values (if any) will be added to the $entity parameter for fields 907 * it leaves unspecified. 908 */ 909 function field_attach_insert($entity_type, $entity) { 910 _field_invoke_default('insert', $entity_type, $entity); 911 _field_invoke('insert', $entity_type, $entity); 912 913 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); 914 915 // Let any module insert field data before the storage engine, accumulating 916 // saved fields along the way. 917 $skip_fields = array(); 918 foreach (module_implements('field_storage_pre_insert') as $module) { 919 $function = $module . '_field_storage_pre_insert'; 920 $function($entity_type, $entity, $skip_fields); 921 } 922 923 // Collect the storage backends used by the remaining fields in the entities. 924 $storages = array(); 925 foreach (field_info_instances($entity_type, $bundle) as $instance) { 926 $field = field_info_field_by_id($instance['field_id']); 927 $field_id = $field['id']; 928 $field_name = $field['field_name']; 929 if (!empty($entity->$field_name)) { 930 // Collect the storage backend if the field has not been written yet. 931 if (!isset($skip_fields[$field_id])) { 932 $storages[$field['storage']['type']][$field_id] = $field_id; 933 } 934 } 935 } 936 937 // Field storage backends save any remaining unsaved fields. 938 foreach ($storages as $storage => $fields) { 939 $storage_info = field_info_storage_types($storage); 940 module_invoke($storage_info['module'], 'field_storage_write', $entity_type, $entity, FIELD_STORAGE_INSERT, $fields); 941 } 942 943 // Let other modules act on inserting the entity. 944 module_invoke_all('field_attach_insert', $entity_type, $entity); 945 946 $entity_info = entity_get_info($entity_type); 947 } 948 949 /** 950 * Save field data for an existing entity. 951 * 952 * @param $entity_type 953 * The type of $entity; e.g. 'node' or 'user'. 954 * @param $entity 955 * The entity with fields to save. 956 */ 957 function field_attach_update($entity_type, $entity) { 958 _field_invoke('update', $entity_type, $entity); 959 960 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); 961 962 // Let any module update field data before the storage engine, accumulating 963 // saved fields along the way. 964 $skip_fields = array(); 965 foreach (module_implements('field_storage_pre_update') as $module) { 966 $function = $module . '_field_storage_pre_update'; 967 $function($entity_type, $entity, $skip_fields); 968 } 969 970 // Collect the storage backends used by the remaining fields in the entities. 971 $storages = array(); 972 foreach (field_info_instances($entity_type, $bundle) as $instance) { 973 $field = field_info_field_by_id($instance['field_id']); 974 $field_id = $field['id']; 975 $field_name = $field['field_name']; 976 // Leave the field untouched if $entity comes with no $field_name property, 977 // but empty the field if it comes as a NULL value or an empty array. 978 // Function property_exists() is slower, so we catch the more frequent 979 // cases where it's an empty array with the faster isset(). 980 if (isset($entity->$field_name) || property_exists($entity, $field_name)) { 981 // Collect the storage backend if the field has not been written yet. 982 if (!isset($skip_fields[$field_id])) { 983 $storages[$field['storage']['type']][$field_id] = $field_id; 984 } 985 } 986 } 987 988 // Field storage backends save any remaining unsaved fields. 989 foreach ($storages as $storage => $fields) { 990 $storage_info = field_info_storage_types($storage); 991 module_invoke($storage_info['module'], 'field_storage_write', $entity_type, $entity, FIELD_STORAGE_UPDATE, $fields); 992 } 993 994 // Let other modules act on updating the entity. 995 module_invoke_all('field_attach_update', $entity_type, $entity); 996 997 $entity_info = entity_get_info($entity_type); 998 if ($entity_info['field cache']) { 999 cache_clear_all("field:$entity_type:$id", 'cache_field'); 1000 } 1001 } 1002 1003 /** 1004 * Delete field data for an existing entity. This deletes all 1005 * revisions of field data for the entity. 1006 * 1007 * @param $entity_type 1008 * The type of $entity; e.g. 'node' or 'user'. 1009 * @param $entity 1010 * The entity whose field data to delete. 1011 */ 1012 function field_attach_delete($entity_type, $entity) { 1013 _field_invoke('delete', $entity_type, $entity); 1014 1015 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); 1016 1017 // Collect the storage backends used by the fields in the entities. 1018 $storages = array(); 1019 foreach (field_info_instances($entity_type, $bundle) as $instance) { 1020 $field = field_info_field_by_id($instance['field_id']); 1021 $field_id = $field['id']; 1022 $storages[$field['storage']['type']][$field_id] = $field_id; 1023 } 1024 1025 // Field storage backends delete their data. 1026 foreach ($storages as $storage => $fields) { 1027 $storage_info = field_info_storage_types($storage); 1028 module_invoke($storage_info['module'], 'field_storage_delete', $entity_type, $entity, $fields); 1029 } 1030 1031 // Let other modules act on deleting the entity. 1032 module_invoke_all('field_attach_delete', $entity_type, $entity); 1033 1034 $entity_info = entity_get_info($entity_type); 1035 if ($entity_info['field cache']) { 1036 cache_clear_all("field:$entity_type:$id", 'cache_field'); 1037 } 1038 } 1039 1040 /** 1041 * Delete field data for a single revision of an existing entity. The 1042 * passed entity must have a revision id attribute. 1043 * 1044 * @param $entity_type 1045 * The type of $entity; e.g. 'node' or 'user'. 1046 * @param $entity 1047 * The entity with fields to save. 1048 */ 1049 function field_attach_delete_revision($entity_type, $entity) { 1050 _field_invoke('delete_revision', $entity_type, $entity); 1051 1052 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); 1053 1054 // Collect the storage backends used by the fields in the entities. 1055 $storages = array(); 1056 foreach (field_info_instances($entity_type, $bundle) as $instance) { 1057 $field = field_info_field_by_id($instance['field_id']); 1058 $field_id = $field['id']; 1059 $storages[$field['storage']['type']][$field_id] = $field_id; 1060 } 1061 1062 // Field storage backends delete their data. 1063 foreach ($storages as $storage => $fields) { 1064 $storage_info = field_info_storage_types($storage); 1065 module_invoke($storage_info['module'], 'field_storage_delete_revision', $entity_type, $entity, $fields); 1066 } 1067 1068 // Let other modules act on deleting the revision. 1069 module_invoke_all('field_attach_delete_revision', $entity_type, $entity); 1070 } 1071 1072 /** 1073 * Prepare field data prior to display. 1074 * 1075 * This function lets field types and formatters load additional data 1076 * needed for display that is not automatically loaded during 1077 * field_attach_load(). It accepts an array of entities to allow query 1078 * optimisation when displaying lists of entities. 1079 * 1080 * field_attach_prepare_view() and field_attach_view() are two halves 1081 * of the same operation. It is safe to call 1082 * field_attach_prepare_view() multiple times on the same entity 1083 * before calling field_attach_view() on it, but calling any Field 1084 * API operation on an entity between passing that entity to these two 1085 * functions may yield incorrect results. 1086 * 1087 * @param $entity_type 1088 * The type of $entities; e.g. 'node' or 'user'. 1089 * @param $entities 1090 * An array of entities, keyed by entity id. 1091 * @param $view_mode 1092 * View mode, e.g. 'full', 'teaser'... 1093 * @param $langcode 1094 * (Optional) The language the field values are to be shown in. If no language 1095 * is provided the current language is used. 1096 */ 1097 function field_attach_prepare_view($entity_type, $entities, $view_mode, $langcode = NULL) { 1098 $options = array('language' => array()); 1099 1100 // To ensure hooks are only run once per entity, only process items without 1101 // the _field_view_prepared flag. 1102 // @todo: resolve this more generally for both entity and field level hooks. 1103 $prepare = array(); 1104 foreach ($entities as $id => $entity) { 1105 if (empty($entity->_field_view_prepared)) { 1106 // Add this entity to the items to be prepared. 1107 $prepare[$id] = $entity; 1108 1109 // Determine the actual language to display for each field, given the 1110 // languages available in the field data. 1111 $options['language'][$id] = field_language($entity_type, $entity, NULL, $langcode); 1112 1113 // Mark this item as prepared. 1114 $entity->_field_view_prepared = TRUE; 1115 } 1116 } 1117 1118 $null = NULL; 1119 // First let the field types do their preparation. 1120 _field_invoke_multiple('prepare_view', $entity_type, $prepare, $null, $null, $options); 1121 // Then let the formatters do their own specific massaging. 1122 // field_default_prepare_view() takes care of dispatching to the correct 1123 // formatters according to the display settings for the view mode. 1124 _field_invoke_multiple_default('prepare_view', $entity_type, $prepare, $view_mode, $null, $options); 1125 } 1126 1127 /** 1128 * Returns a renderable array for the fields on an entity. 1129 * 1130 * Each field is displayed according to the display options specified in the 1131 * $instance definition for the given $view_mode. 1132 * 1133 * field_attach_prepare_view() and field_attach_view() are two halves 1134 * of the same operation. It is safe to call 1135 * field_attach_prepare_view() multiple times on the same entity 1136 * before calling field_attach_view() on it, but calling any Field 1137 * API operation on an entity between passing that entity to these two 1138 * functions may yield incorrect results. 1139 * 1140 * Sample structure: 1141 * @code 1142 * array( 1143 * 'field_foo' => array( 1144 * '#theme' => 'field', 1145 * '#title' => the label of the field instance, 1146 * '#label_display' => the label display mode, 1147 * '#object' => the fieldable entity being displayed, 1148 * '#entity_type' => the type of the entity being displayed, 1149 * '#language' => the language of the field values being displayed, 1150 * '#view_mode' => the view mode, 1151 * '#field_name' => the name of the field, 1152 * '#field_type' => the type of the field, 1153 * '#formatter' => the name of the formatter, 1154 * '#items' => the field values being displayed, 1155 * // The element's children are the formatted values returned by 1156 * // hook_field_formatter_view(). 1157 * ), 1158 * ); 1159 * @endcode 1160 * 1161 * @param $entity_type 1162 * The type of $entity; e.g. 'node' or 'user'. 1163 * @param $entity 1164 * The entity with fields to render. 1165 * @param $view_mode 1166 * View mode, e.g. 'full', 'teaser'... 1167 * @param $langcode 1168 * The language the field values are to be shown in. If no language is 1169 * provided the current language is used. 1170 * @return 1171 * A renderable array for the field values. 1172 */ 1173 function field_attach_view($entity_type, $entity, $view_mode, $langcode = NULL) { 1174 // Determine the actual language to display for each field, given the 1175 // languages available in the field data. 1176 $display_language = field_language($entity_type, $entity, NULL, $langcode); 1177 $options = array('language' => $display_language); 1178 1179 // Invoke field_default_view(). 1180 $null = NULL; 1181 $output = _field_invoke_default('view', $entity_type, $entity, $view_mode, $null, $options); 1182 1183 // Add custom weight handling. 1184 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); 1185 $output['#pre_render'][] = '_field_extra_fields_pre_render'; 1186 $output['#entity_type'] = $entity_type; 1187 $output['#bundle'] = $bundle; 1188 1189 // Let other modules alter the renderable array. 1190 $context = array( 1191 'entity_type' => $entity_type, 1192 'entity' => $entity, 1193 'view_mode' => $view_mode, 1194 'display' => $view_mode, 1195 'language' => $langcode, 1196 ); 1197 drupal_alter('field_attach_view', $output, $context); 1198 1199 // Reset the _field_view_prepared flag set in field_attach_prepare_view(), 1200 // in case the same entity is displayed with different settings later in 1201 // the request. 1202 unset($entity->_field_view_prepared); 1203 1204 return $output; 1205 } 1206 1207 /** 1208 * Populate the template variables with the field values available for rendering. 1209 * 1210 * The $variables array will be populated with all the field instance values 1211 * associated with the given entity type, keyed by field name; in case of 1212 * translatable fields the language currently chosen for display will be 1213 * selected. 1214 * 1215 * @param $entity_type 1216 * The type of $entity; e.g. 'node' or 'user'. 1217 * @param $entity 1218 * The entity with fields to render. 1219 * @param $element 1220 * The structured array containing the values ready for rendering. 1221 * @param $variables 1222 * The variables array is passed by reference and will be populated with field 1223 * values. 1224 */ 1225 function field_attach_preprocess($entity_type, $entity, $element, &$variables) { 1226 list(, , $bundle) = entity_extract_ids($entity_type, $entity); 1227 1228 foreach (field_info_instances($entity_type, $bundle) as $instance) { 1229 $field_name = $instance['field_name']; 1230 if (isset($element[$field_name]['#language'])) { 1231 $langcode = $element[$field_name]['#language']; 1232 $variables[$field_name] = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : NULL; 1233 } 1234 } 1235 1236 // Let other modules make changes to the $variables array. 1237 $context = array( 1238 'entity_type' => $entity_type, 1239 'entity' => $entity, 1240 'element' => $element, 1241 ); 1242 drupal_alter('field_attach_preprocess', $variables, $context); 1243 } 1244 1245 /** 1246 * Prepares an entity for translation. 1247 * 1248 * This function is used to fill-in the form default values for Field API fields 1249 * while performing entity translation. By default it copies all the source 1250 * values in the given source language to the new entity and assigns them the 1251 * target language. 1252 * 1253 * This is used as part of the 'per entity' translation pattern, which is 1254 * implemented only for nodes by translation.module. Other entity types may be 1255 * supported through contributed modules. 1256 * 1257 * @param $entity_type 1258 * The type of $entity; e.g. 'node' or 'user'. 1259 * @param $entity 1260 * The entity to be prepared for translation. 1261 * @param $langcode 1262 * The language the entity has to be translated in. 1263 * @param $source_entity 1264 * The source entity holding the field values to be translated. 1265 * @param $source_langcode 1266 * The source language from which translate. 1267 */ 1268 function field_attach_prepare_translation($entity_type, $entity, $langcode, $source_entity, $source_langcode) { 1269 $options = array('language' => $langcode); 1270 // Copy source field values into the entity to be prepared. 1271 _field_invoke_default('prepare_translation', $entity_type, $entity, $source_entity, $source_langcode, $options); 1272 // Let field types handle their own advanced translation pattern if needed. 1273 _field_invoke('prepare_translation', $entity_type, $entity, $source_entity, $source_langcode, $options); 1274 // Let other modules alter the entity translation. 1275 $context = array( 1276 'entity_type' => $entity_type, 1277 'langcode' => $langcode, 1278 'source_entity' => $source_entity, 1279 'source_langcode' => $source_langcode, 1280 ); 1281 drupal_alter('field_attach_prepare_translation', $entity, $context); 1282 } 1283 1284 /** 1285 * Notify field.module that a new bundle was created. 1286 * 1287 * The default SQL-based storage doesn't need to do anything about it, but 1288 * others might. 1289 * 1290 * @param $entity_type 1291 * The entity type to which the bundle is bound. 1292 * @param $bundle 1293 * The name of the newly created bundle. 1294 */ 1295 function field_attach_create_bundle($entity_type, $bundle) { 1296 // Clear the cache. 1297 field_cache_clear(); 1298 1299 // Let other modules act on creating the bundle. 1300 module_invoke_all('field_attach_create_bundle', $entity_type, $bundle); 1301 } 1302 1303 /** 1304 * Notify field.module that a bundle was renamed. 1305 * 1306 * @param $entity_type 1307 * The entity type to which the bundle is bound. 1308 * @param $bundle_old 1309 * The previous name of the bundle. 1310 * @param $bundle_new 1311 * The new name of the bundle. 1312 */ 1313 function field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) { 1314 db_update('field_config_instance') 1315 ->fields(array('bundle' => $bundle_new)) 1316 ->condition('entity_type', $entity_type) 1317 ->condition('bundle', $bundle_old) 1318 ->execute(); 1319 1320 // Clear the cache. 1321 field_cache_clear(); 1322 1323 // Update bundle settings. 1324 $settings = variable_get('field_bundle_settings_' . $entity_type . '__' . $bundle_old, array()); 1325 variable_set('field_bundle_settings_' . $entity_type . '__' . $bundle_new, $settings); 1326 variable_del('field_bundle_settings_' . $entity_type . '__' . $bundle_old); 1327 1328 // Let other modules act on renaming the bundle. 1329 module_invoke_all('field_attach_rename_bundle', $entity_type, $bundle_old, $bundle_new); 1330 } 1331 1332 /** 1333 * Notify field.module the a bundle was deleted. 1334 * 1335 * This deletes the data for the field instances as well as the field instances 1336 * themselves. This function actually just marks the data and field instances 1337 * and deleted, leaving the garbage collection for a separate process, because 1338 * it is not always possible to delete this much data in a single page request 1339 * (particularly since for some field types, the deletion is more than just a 1340 * simple DELETE query). 1341 * 1342 * @param $entity_type 1343 * The entity type to which the bundle is bound. 1344 * @param $bundle 1345 * The bundle to delete. 1346 */ 1347 function field_attach_delete_bundle($entity_type, $bundle) { 1348 // First, delete the instances themselves. field_read_instances() must be 1349 // used here since field_info_instances() does not return instances for 1350 // disabled entity types or bundles. 1351 $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle), array('include_inactive' => 1)); 1352 foreach ($instances as $instance) { 1353 field_delete_instance($instance); 1354 } 1355 1356 // Clear the cache. 1357 field_cache_clear(); 1358 1359 // Clear bundle display settings. 1360 variable_del('field_bundle_settings_' . $entity_type . '__' . $bundle); 1361 1362 // Let other modules act on deleting the bundle. 1363 module_invoke_all('field_attach_delete_bundle', $entity_type, $bundle, $instances); 1364 } 1365 1366 1367 /** 1368 * @} End of "defgroup field_attach". 1369 */
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
title