1. drupal
    1. 8
    2. 7
7 – 8 field_attach_insert($entity_type, $entity)

Save field data for a new entity.

The passed-in entity must already contain its id and (if applicable) revision id attributes. Default values (if any) will be saved for fields not present in the $entity.


$entity_type The type of $entity; e.g. 'node' or 'user'.

$entity The entity with fields to save.

Return value

Default values (if any) will be added to the $entity parameter for fields it leaves unspecified.

Related topics

▾ 22 functions call field_attach_insert()

comment_save in modules/comment/comment.module
Accepts a submission of new or changed comment content.
EntityFieldQueryTestCase::setUp in modules/simpletest/tests/entity_query.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…
EntityFieldQueryTestCase::testEntityFieldQuery in modules/simpletest/tests/entity_query.test
Tests EntityFieldQuery.
FieldAttachOtherTestCase::testFieldAttachCache in modules/field/tests/field.test
Test field cache.
FieldAttachStorageTestCase::testFieldAttachCreateRenameBundle in modules/field/tests/field.test
Test field_attach_create_bundle() and field_attach_rename_bundle().
FieldAttachStorageTestCase::testFieldAttachDelete in modules/field/tests/field.test
Test field_attach_delete().
FieldAttachStorageTestCase::testFieldAttachDeleteBundle in modules/field/tests/field.test
Test field_attach_delete_bundle().
FieldAttachStorageTestCase::testFieldAttachLoadMultiple in modules/field/tests/field.test
Test the 'multiple' load feature.
FieldAttachStorageTestCase::testFieldAttachSaveLoad in modules/field/tests/field.test
Check field values insert, update and load.
FieldAttachStorageTestCase::testFieldAttachSaveLoadDifferentStorage in modules/field/tests/field.test
Test saving and loading fields using different storage backends.
FieldAttachStorageTestCase::testFieldAttachSaveMissingData in modules/field/tests/field.test
Tests insert and update with missing or NULL fields.
FieldAttachStorageTestCase::testFieldAttachSaveMissingDataDefaultValue in modules/field/tests/field.test
Test insert with missing or NULL fields, with default value.
FieldBulkDeleteTestCase::setUp in modules/field/tests/field.test
Set the default field storage backend for fields created during tests.
FieldCrudTestCase::testDeleteField in modules/field/tests/field.test
Test the deletion of a field.
FieldCrudTestCase::testUpdateField in modules/field/tests/field.test
Test updating a field.
FieldSqlStorageTestCase::testFieldAttachInsertAndUpdate in modules/field/modules/field_sql_storage/field_sql_storage.test
Reads mysql to verify correct data is written when using insert and update.
FieldSqlStorageTestCase::testFieldAttachSaveMissingData in modules/field/modules/field_sql_storage/field_sql_storage.test
Tests insert and update with missing or NULL fields.
FieldSqlStorageTestCase::testFieldUpdateIndexesWithData in modules/field/modules/field_sql_storage/field_sql_storage.test
Test adding and removing indexes while data is present.
FieldSqlStorageTestCase::testUpdateFieldSchemaWithData in modules/field/modules/field_sql_storage/field_sql_storage.test
Test trying to update a field with data.
FieldTranslationsTestCase::testTranslatableFieldSaveLoad in modules/field/tests/field.test
Test translatable fields storage/retrieval.
taxonomy_term_save in modules/taxonomy/taxonomy.module
Saves a term object to the database.
user_save in modules/user/user.module
Save changes to a user account or add a new user.


modules/field/, line 897

function field_attach_insert($entity_type, $entity) {
  _field_invoke_default('insert', $entity_type, $entity);
  _field_invoke('insert', $entity_type, $entity);

  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);

  // Let any module insert field data before the storage engine, accumulating
  // saved fields along the way.
  $skip_fields = array();
  foreach (module_implements('field_storage_pre_insert') as $module) {
    $function = $module . '_field_storage_pre_insert';
    $function($entity_type, $entity, $skip_fields);

  // Collect the storage backends used by the remaining fields in the entities.
  $storages = array();
  foreach (field_info_instances($entity_type, $bundle) as $instance) {
    $field = field_info_field_by_id($instance['field_id']);
    $field_id = $field['id'];
    $field_name = $field['field_name'];
    if (!empty($entity->$field_name)) {
      // Collect the storage backend if the field has not been written yet.
      if (!isset($skip_fields[$field_id])) {
        $storages[$field['storage']['type']][$field_id] = $field_id;

  // Field storage backends save any remaining unsaved fields.
  foreach ($storages as $storage => $fields) {
    $storage_info = field_info_storage_types($storage);
    module_invoke($storage_info['module'], 'field_storage_write', $entity_type, $entity, FIELD_STORAGE_INSERT, $fields);

  // Let other modules act on inserting the entity.
  module_invoke_all('field_attach_insert', $entity_type, $entity);

  $entity_info = entity_get_info($entity_type);