function hook_field_presave

Define custom presave behavior for this module's field types.

Make changes or additions to field values by altering the $items parameter by reference. There is no return value.

Parameters

$entity_type: The type of $entity.

$entity: The entity for the operation.

$field: The field structure for the operation.

$instance: The instance structure for $field on $entity's bundle.

$langcode: The language associated with $items.

$items: $entity->{$field['field_name']}[$langcode], or an empty array if unset.

Related topics

4 functions implement hook_field_presave()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

image_field_presave in drupal/core/modules/image/image.field.inc
Implements hook_field_presave().
link_field_presave in drupal/core/modules/field/modules/link/link.module
Implements hook_field_presave().
number_field_presave in drupal/core/modules/field/modules/number/number.module
Implements hook_field_presave().
taxonomy_field_presave in drupal/core/modules/taxonomy/taxonomy.module
Implements hook_field_presave().
1 invocation of hook_field_presave()
field_attach_presave in drupal/core/modules/field/field.attach.inc
Performs necessary operations just before fields data get saved.

File

drupal/core/modules/field/field.api.php, line 419

Code

function hook_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  if ($field['type'] == 'number_decimal') {

    // Let PHP round the value to ensure consistent behavior across storage
    // backends.
    foreach ($items as $delta => $item) {
      if (isset($item['value'])) {
        $items[$delta]['value'] = round($item['value'], $field['settings']['scale']);
      }
    }
  }
}