You are here

function file_entity_install in File Entity (fieldable files) 7

Same name and namespace in other branches
  1. 8.2 file_entity.install \file_entity_install()
  2. 7.3 file_entity.install \file_entity_install()
  3. 7.2 file_entity.install \file_entity_install()

Implements hook_install().

File

./file_entity.install, line 88
Install, update and uninstall functions for the file_entity module.

Code

function file_entity_install() {
  $schema = array();
  file_entity_schema_alter($schema);
  $spec = $schema['file_managed']['fields']['type'];
  $indexes_new = array(
    'indexes' => $schema['file_managed']['indexes'],
  );

  // If another module (e.g., Media) had added a {file_managed}.type field,
  // then change it to the expected specification. Otherwise, add the field.
  if (db_field_exists('file_managed', 'type')) {

    // db_change_field() will fail if any records have type=NULL, so update
    // them to the new default value.
    db_update('file_managed')
      ->fields(array(
      'type' => $spec['default'],
    ))
      ->isNull('type')
      ->execute();

    // Indexes using a field being changed must be dropped prior to calling
    // db_change_field(). However, the database API doesn't provide a way to do
    // this without knowing what the old indexes are. Therefore, it is the
    // responsibility of the module that added them to drop them prior to
    // allowing this module to be installed.
    db_change_field('file_managed', 'type', 'type', $spec, $indexes_new);
  }
  else {
    db_add_field('file_managed', 'type', $spec, $indexes_new);
  }

  // Update all files with empty types to use the first part of filemime.
  db_update('file_managed')
    ->expression('type', "SUBSTRING_INDEX(filemime, '/', 1)")
    ->condition('type', '')
    ->execute();

  // Set permissions.
  $roles = user_roles();
  foreach ($roles as $rid => $role) {
    user_role_grant_permissions($rid, array(
      'view file',
    ));
  }
}