You are here

media_entity.install in Media entity 8

Same filename and directory in other branches
  1. 8.2 media_entity.install

Install, uninstall and update hooks for Media entity module.

File

media_entity.install
View source
<?php

/**
 * @file
 * Install, uninstall and update hooks for Media entity module.
 */

/**
 * Checks if required version of the Entity API is installed.
 *
 * @return bool
 *   TRUE if dependency is met and FALSE if not.
 */
function _media_entity_check_entity_version() {
  if (\Drupal::moduleHandler()
    ->moduleExists('entity')) {
    $info = system_get_info('module', 'entity');

    // Do not attempt version checks on development versions, which don't have
    // a version tag.
    if (empty($info['version']) || version_compare($info['version'], '8.x-1.0-alpha3') >= 0) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Implements hook_requirements().
 */
function media_entity_requirements($phase) {
  $requirements = [];
  if ($phase == 'update' && !_media_entity_check_entity_version()) {
    $requirements['entity'] = [
      'title' => t('Media entity'),
      'value' => t('Entity API missing'),
      'description' => t('<a href=":url">Entity API >= 8.x-1.0-alpha3</a> module is now a dependency and needs to be installed before running updates.', [
        ':url' => 'https://www.drupal.org/project/entity',
      ]),
      'severity' => REQUIREMENT_ERROR,
    ];
  }
  if ($phase == 'install' && \Drupal::moduleHandler()
    ->moduleExists('media')) {
    $version = explode('.', \Drupal::VERSION);
    if ($version[1] >= 4) {
      $requirements['media_module_incompatibility'] = [
        'title' => t('Media Entity'),
        'value' => t('The Media Entity module is not compatible with media in core.'),
        'description' => t('The 1.x branch of Media Entity cannot be used in conjunction with the media module in core. Please check the 2.x branch for an upgrade path.'),
        'severity' => REQUIREMENT_ERROR,
      ];
    }
  }
  return $requirements;
}

/**
 * Implements hook_install().
 */
function media_entity_install() {
  $source = drupal_get_path('module', 'media_entity') . '/images/icons';
  $destination = \Drupal::config('media_entity.settings')
    ->get('icon_base');
  media_entity_copy_icons($source, $destination);
}

/**
 * Remove "type" base field.
 */
function media_entity_update_8001() {
  $fields = \Drupal::database()
    ->query('DESCRIBE {media_field_data}')
    ->fetchCol();
  if (in_array('type', $fields)) {
    \Drupal::database()
      ->update('media_field_data')
      ->fields([
      'type' => NULL,
    ])
      ->execute();
  }
  $manager = \Drupal::entityDefinitionUpdateManager();
  if ($field = $manager
    ->getFieldStorageDefinition('type', 'media')) {
    $manager
      ->uninstallFieldStorageDefinition($field);
  }
}

/**
 * Ensure media entity status value (defaulting to 1).
 */
function media_entity_update_8002() {

  // Ensure status values in 'media_field_data' table.
  if (\Drupal::database()
    ->schema()
    ->tableExists('media_field_data')) {
    \Drupal::database()
      ->update('media_field_data')
      ->fields([
      'status' => 1,
    ])
      ->condition('status', NULL, 'IS NULL')
      ->execute();
  }

  // Ensure status values in 'media_field_revision' table.
  if (\Drupal::database()
    ->schema()
    ->tableExists('media_field_revision')) {
    \Drupal::database()
      ->update('media_field_revision')
      ->fields([
      'status' => 1,
    ])
      ->condition('status', NULL, 'IS NULL')
      ->execute();
  }

  // Flush all caches.
  drupal_flush_all_caches();
}

/**
 * Ensure Entity API is installed.
 */
function media_entity_update_8003() {
  if (!_media_entity_check_entity_version()) {
    throw new \Drupal\Core\Utility\UpdateException('Entity API >= 8.x-1.0-alpha3 (drupal.org/project/entity) module is now a dependency and needs to be installed before running updates.');
  }
}

Functions

Namesort descending Description
media_entity_install Implements hook_install().
media_entity_requirements Implements hook_requirements().
media_entity_update_8001 Remove "type" base field.
media_entity_update_8002 Ensure media entity status value (defaulting to 1).
media_entity_update_8003 Ensure Entity API is installed.
_media_entity_check_entity_version Checks if required version of the Entity API is installed.