function field_update_field

Updates a field.

Any module may forbid any update for any reason. For example, the field's storage module might forbid an update if it would change the storage schema while data for the field exists. A field type module might forbid an update if it would change existing data's semantics, or if there are external dependencies on field settings that cannot be updated.

Parameters

mixed $field: Either the \Drupal\field\Plugin\Core\Entity\Field object to update, or a field array structure. If the latter, $field['field_name'] must provided; it identifies the field that will be updated to match this structure. Any other properties of the field that are not specified in $field will be left unchanged, so it is not necessary to pass in a fully populated $field structure.

Throws

Drupal\field\FieldException

Deprecated

as of Drupal 8.0. Use $field->save().

See also

field_create_field()

Related topics

33 calls to field_update_field()
CommentLanguageTest::setUp in drupal/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php
Sets up a Drupal site for running functional and integration tests.
CommentTranslationUITest::setupTestFields in drupal/core/modules/comment/lib/Drupal/comment/Tests/CommentTranslationUITest.php
Overrides \Drupal\translation_entity\Tests\EntityTranslationUITest::setupTestFields().
CrudTest::testUpdateField in drupal/core/modules/field/lib/Drupal/field/Tests/CrudTest.php
Test updating a field.
CrudTest::testUpdateFieldForbid in drupal/core/modules/field/lib/Drupal/field/Tests/CrudTest.php
Test field type modules forbidding an update.
CrudTest::testUpdateFieldType in drupal/core/modules/field/lib/Drupal/field/Tests/CrudTest.php

... See full list

File

drupal/core/modules/field/field.crud.inc, line 93
Field CRUD API, handling field and field instance creation and deletion.

Code

function field_update_field($field) {

  // Module developers can still pass in an array of properties.
  if (is_array($field)) {
    $field_loaded = entity_load('field_entity', $field['field_name']);
    if (empty($field_loaded)) {
      throw new FieldException('Attempt to update a non-existent field.');
    }

    // Merge incoming values.
    foreach ($field as $key => $value) {
      $field_loaded[$key] = $value;
    }
    $field = $field_loaded;
  }
  $field
    ->save();
}