You are here

function file_entity_install in File Entity (fieldable files) 8.2

Same name and namespace in other branches
  1. 7.3 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 53
Install, update and uninstall functions for the file_entity module.

Code

function file_entity_install() {

  // Install the type field first, then update the entity type, and then
  // update the field again.

  /** @var \Drupal\Core\Field\BaseFieldDefinition $type_storage_definition */
  $type_storage_definition = \Drupal::service('entity_field.manager')
    ->getFieldStorageDefinitions('file')['type'];
  $type_storage_definition
    ->setInitialValue(FILE_TYPE_NONE);
  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('type', 'file', 'file_entity', $type_storage_definition);
  $entity_type = \Drupal::entityDefinitionUpdateManager()
    ->getEntityType('file');
  $keys = $entity_type
    ->getKeys();
  $keys['bundle'] = 'type';
  $entity_type
    ->set('entity_keys', $keys);
  \Drupal::entityDefinitionUpdateManager()
    ->updateEntityType($entity_type);
  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('type', 'file', 'file_entity', $type_storage_definition);

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

  // Classify existing files according to the currently defined file types.
  // Queue all files to be classified during cron runs using the Queue API.
  $queue = \Drupal::queue('file_entity_type_determine');
  $ids = \Drupal::entityQuery('file')
    ->execute();
  foreach ($ids as $id) {
    $queue
      ->createItem($id);
  }

  // Warn users that existing files will not have a file type until the queue
  // has been processed.
  if ($queue
    ->numberOfItems()) {
    \Drupal::messenger()
      ->addMessage(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.'));
  }

  // Disable the core files view.
  if ($view = View::load('files')) {
    $view
      ->set('status', FALSE);
    $view
      ->save();
  }
}