You are here

function file_entity_install in File Entity (fieldable files) 7.3

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

Implements hook_install().

File

./file_entity.install, line 173
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);
  }

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

  // Create the title and alt text fields.
  _file_entity_create_alt_title_fields();

  // Configure default pathauto variables if it is currently installed.
  if (module_exists('pathauto')) {
    variable_set('pathauto_file_pattern', 'file/[file:name]');
  }

  // Classify existing files according to the currently defined file types.
  // Queue all files to be classified during cron runs using the Queue API.
  $queue = DrupalQueue::get('file_entity_type_determine');
  $result = db_query('SELECT fid FROM {file_managed}');
  foreach ($result as $record) {
    $queue
      ->createItem($record->fid);
  }

  // Warn users that existing files will not have a file type until the queue
  // has been processed.
  if ($queue
    ->numberOfItems()) {
    drupal_set_message(t('Existing files must be classified according to the currently defined file types. These files have been queued for processing and will have their file type determined during cron runs.'));
  }
}