You are here

function file_type_save in File Entity (fieldable files) 7.2

Same name and namespace in other branches
  1. 7.3 file_entity.file_api.inc \file_type_save()

Updates an existing file type or creates a new one.

This function can be called on its own, or via the CTools exportables 'save callback' for {file_type} objects.

Related topics

3 calls to file_type_save()
FileEntityTestHelper::createFileType in ./file_entity.test
file_entity_file_type_form_submit in ./file_entity.admin.inc
Form submission handler for file_entity_file_type_form().
file_entity_update_7210 in ./file_entity.install
Merge MIME types into the {file_type} table.
2 string references to 'file_type_save'
file_entity_schema in ./file_entity.install
Implements hook_schema().
file_entity_update_7201 in ./file_entity.install
Add the {file_type}, {file_type_mimetypes} tables.

File

./file_entity.file_api.inc, line 548
API extensions of Drupal core's file.inc.

Code

function file_type_save($type) {

  // Get the old type object, so we now can issue the correct insert/update
  // queries.
  if (!empty($type->old_type) && $type->old_type != $type->type) {
    $rename_bundle = TRUE;
    $old_type = file_type_load($type->old_type);
  }
  else {
    $rename_bundle = FALSE;
    $old_type = file_type_load($type->type);
  }

  // The type and label fields are required, but description is optional.
  if (!isset($type->description)) {
    $type->description = '';
  }
  $fields = array(
    'type' => $type->type,
    'label' => $type->label,
    'description' => $type->description,
    'mimetypes' => serialize($type->mimetypes),
  );

  // Update an existing type object, whether with a modified 'type' property or
  // not.
  if ($old_type) {
    if ($old_type->export_type & EXPORT_IN_DATABASE) {
      db_update('file_type')
        ->fields($fields)
        ->condition('type', $old_type->type)
        ->execute();
    }
    else {
      db_insert('file_type')
        ->fields($fields)
        ->execute();
    }
    if ($rename_bundle) {
      field_attach_rename_bundle('file', $old_type->type, $type->type);
    }
    module_invoke_all('file_type_update', $type);
    $status = SAVED_UPDATED;
  }
  else {
    db_insert('file_type')
      ->fields($fields)
      ->execute();
    field_attach_create_bundle('file', $type->type);
    module_invoke_all('file_type_insert', $type);
    $status = SAVED_NEW;
  }

  // Clear the necessary caches.
  file_info_cache_clear();

  // Ensure the type has the correct export_type in case the $type parameter
  // continues to be used by the calling function after this function completes.
  if (empty($type->export_type)) {
    $type->export_type = 0;
  }
  $type->export_type |= EXPORT_IN_DATABASE;
  return $status;
}