drowl_media-8.x-2.0-rc0/drowl_media.install

drowl_media.install
<?php

/**
 * @file
 * Install, update and uninstall functions for the DROWL Media module.
 */

use Drupal\Core\Extension\MissingDependencyException;
use Drupal\Core\File\FileExists;
use Drupal\Core\File\FileSystemInterface;
use Drupal\views\Entity\View;
use Symfony\Component\Yaml\Yaml;

/**
 * Implements hook_requirements().
 */
function drowl_media_requirements($phase) {
  $requirements = [];

  if ($phase == 'install') {
    // Add generic icon.
    $file_system = \Drupal::service('file_system');
    $destination = 'public://media-icons/generic';
    $file_system->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
    $is_writable = is_writable($destination);
    $is_directory = is_dir($destination);
    if (!$is_writable || !$is_directory) {
      if (!$is_directory) {
        $error = t('The directory %directory does not exist.', ['%directory' => $destination]);
      }
      else {
        $error = t('The directory %directory is not writable.', ['%directory' => $destination]);
      }
      $description = t('An automated attempt to create this directory failed, possibly due to a permissions problem. To proceed with the installation, either create the directory and modify its permissions manually or ensure that the installer has the permissions to create it automatically. For more information, see INSTALL.txt or the <a href=":handbook_url">online handbook</a>.', [':handbook_url' => 'https://www.drupal.org/server-permissions']);
      if (!empty($error)) {
        $description = $error . ' ' . $description;
        $requirements['drowl_media']['description'] = $description;
        $requirements['drowl_media']['severity'] = REQUIREMENT_ERROR;
      }
    }
  }
  elseif ($phase == 'runtime') {
    $drowlAdminIconsetLibrary = \Drupal::service('library.discovery')->getLibraryByName('drowl_media', 'admin_iconset');

    if (empty($drowlAdminIconsetLibrary)) {
      throw new Exception("Required libraries for the drowl_media module are not defined in the appropriate libraries.yml file.");
    }

    $drowlAdminIconseExist = file_exists(DRUPAL_ROOT . '/' . $drowlAdminIconsetLibrary['css'][0]['data']);

    $requirements['drowl_admin_iconset'] = [
      'title' => t('DROWL Media: Drowl Admin Iconset library (npm-asset/drowl-admin-iconset)'),
      'value' => $drowlAdminIconseExist ? t('Installed') : t('Not installed'),
      'description' => $drowlAdminIconseExist ? '' : t('To use the drowl_media module, the drowl admin iconset library needs to be required via composer, using "composer require npm-asset/drowl-admin-iconset". NOTE, that you need to set up the "asset-packagist" repository in your projects composer.json file.<br>See <a href="https://www.drupal.org/docs/develop/using-composer/using-composer-to-install-drupal-and-manage-dependencies#third-party-libraries">here</a> on how to do it.'),
      'severity' => $drowlAdminIconseExist ? REQUIREMENT_OK : REQUIREMENT_ERROR,
    ];
  }
  // This branch of drowl_media shouldn't be used with core < 8.4.x or with
  // media_entity 1.x enabled.
  $drupal_version = explode('.', \Drupal::VERSION);
  if ($drupal_version[0] == 8 && $drupal_version[1] < 4) {
    $requirements['drowl_media'] = [
      'title' => t('DROWL Media (drowl_media_requirements)'),
      'value' => t('This branch of DROWL Media is not compatible with the version of Drupal Core (< 8.4.x).'),
      'description' => t('This branch of DROWL Media is not compatible with the version of Drupal Core (< 8.4.x).'),
      'severity' => REQUIREMENT_ERROR,
    ];
  }
  if (\Drupal::moduleHandler()->moduleExists('media_entity')) {
    // Old version of media_entity_actions installed. Update to 2.x,
    // which will uninstall itself.
    // Afterwards the module can be removed.
    $info = system_get_info('module', 'media_entity');
    if (!empty($info['version']) && version_compare($info['version'], '8.x-2') < 0) {
      $requirements['drowl_media_media_entity'] = [
        'title' => t('DROWL Media (drowl_media_requirements)'),
        'value' => t('This branch of DROWL Media is not compatible with the version of Media Entity (1.x) installed.'),
        'description' => t('This branch of DROWL Media is not compatible with the version of Media Entity (1.x) installed.'),
        'severity' => REQUIREMENT_ERROR,
      ];
    }
  }
  if (\Drupal::moduleHandler()->moduleExists('media_entity_actions')) {
    // Old version of media_entity_actions installed. Update to 2.x,
    // which will uninstall itself.
    // Afterwards the module can be removed.
    $info = system_get_info('module', 'media_entity_actions');
    if (!empty($info['version']) && version_compare($info['version'], '8.x-2') < 0) {
      $requirements['drowl_media_media_entity_actions'] = [
        'title' => t('DROWL Media (drowl_media_requirements)'),
        'value' => t('This branch of DROWL Media is not compatible with the version of media_entity_actions (@installed_version) installed.', ['@installed_version' => $info['version']]),
        'description' => t('This branch of DROWL Media is not compatible with the version of media_entity_actions (@installed_version) installed.', ['@installed_version' => $info['version']]),
        'severity' => REQUIREMENT_ERROR,
      ];
    }
  }

  return $requirements;
}

/**
 * Implements hook_update_dependencies().
 */
function drowl_media_update_dependencies() {
  // Run after media_entity 2.x update (media in core migration)
  $dependencies['drowl_media'][8200] = [
    'media_entity' => 8201,
  ];
  return $dependencies;
}

/**
 * Uninstalling specific modules.
 *
 * Ensure media_entity, media_entity_document and media_entity_image
 * are uninstalled.
 */
function drowl_media_update_8200(&$sandbox) {
  $message = '';
  if (\Drupal::service('module_installer')->uninstall(['media_entity'])) {
    $message .= 'Uninstalled Module "media_entity" successfully. You may want to remove it now.';
  }
  $message = '';
  if (\Drupal::service('module_installer')->uninstall(['media_entity_image'])) {
    $message .= 'Uninstalled Module "media_entity_image" successfully. You may want to remove it now.';
  }
  $message .= ' ';
  if (\Drupal::service('module_installer')->uninstall(['media_entity_document '])) {
    $message .= 'Uninstalled Module "media_entity_document " successfully. You may want to remove it now.';
  }
  return $message;
}

/**
 * Enable media_entity_actions.
 */
function drowl_media_update_8201(&$sandbox) {
  try {
    if (\Drupal::service('module_installer')->install(['media_entity_actions'])) {
      return t('Enabled Module "media_entity_actions" successfully.');
    }
  }
  catch (MissingDependencyException $e) {
    return t('Module "@module" was not enabled due to the following error: "@message"', ['@module' => 'media_entity_actions', '@message' => $e->getMessage()]);
  }
}

/**
 * Enable media_entity_image_exif.
 */
function drowl_media_update_8202(&$sandbox) {
  try {
    if (\Drupal::service('module_installer')->install(['media_entity_image_exif'])) {
      return t('Enabled Module "media_entity_image_exif" successfully.');
    }
  }
  catch (MissingDependencyException $e) {
    return t('Module "@module" was not enabled due to the following error: "@message"', ['@module' => 'media_entity_image_exif', '@message' => $e->getMessage()]);
  }
}

/**
 * Clean up DROWL media entities after migration.
 */
function drowl_media_update_8203(&$sandbox) {
  /** @var \Drupal\Core\Entity\EntityFieldManager $entityFieldManager */
  $entityFieldManager = Drupal::service('entity_field.manager');

  /** @var \Drupal\media\MediaTypeInterface $mediaTypeStorage */
  $mediaTypeStorage = \Drupal::entityTypeManager()->getStorage('media_type');

  // Change field_media_image_1 to field_image.
  $field_media_image_fields = $entityFieldManager->getFieldDefinitions('media', 'image');
  if (isset($field_media_image_fields['field_image'])) {
    $mediaTypeStorageImage = $mediaTypeStorage->load('image');
    $mediaTypeStorageImage->set('source_configuration', [
      'source_field' => 'field_image',
    ])->save();
  }
  // Delete field_media_image_1.
  if (isset($field_media_image_fields['field_media_image_1'])) {
    $field_media_image_fields['field_media_image_1']->delete();
  }

  // Change field_media_generic to field_media_slides_ref.
  $field_media_slideshow_fields = $entityFieldManager->getFieldDefinitions('media', 'slideshow');
  if (isset($field_media_slideshow_fields['field_media_slides_ref'])) {
    $mediaTypeStorageImage = $mediaTypeStorage->load('slideshow');
    $mediaTypeStorageImage->set('source_configuration', [
      'source_field' => 'field_media_slides_ref',
    ])->save();
  }
  // Delete field_media_generic.
  if (isset($field_media_slideshow_fields['field_media_generic'])) {
    $field_media_slideshow_fields['field_media_generic']->delete();
  }

  // Change field_media_video_file_1 to field_video_file.
  $field_media_slideshow_fields = $entityFieldManager->getFieldDefinitions('media', 'video');
  if (isset($field_media_slideshow_fields['field_video'])) {
    $mediaTypeStorageImage = $mediaTypeStorage->load('video');
    $mediaTypeStorageImage->set('source_configuration', [
      'source_field' => 'field_video',
    ])->save();
  }
  // Delete field_media_video_file_1.
  if (isset($field_media_slideshow_fields['field_media_video_file_1'])) {
    $field_media_slideshow_fields['field_media_video_file_1']->delete();
  }

  return t('Removed duplicate media fields created by media in core update and replaced them by our old fields keeping their values.');
}

/**
 * Reinstall drowl_media to recreate correct config.
 */
function drowl_media_update_8204(&$sandbox) {
  \Drupal::service('module_installer')->uninstall(['drowl_media']);
  \Drupal::service('module_installer')->install(['drowl_media']);
}

/**
 * Update 8205.
 *
 * Uninstall media_entity_instagram, media_entity_twitter
 * & media_entity_slideshow.
 */
function drowl_media_update_8205(&$sandbox) {
  $message = '';
  if (\Drupal::service('module_installer')->uninstall(['media_entity_twitter'])) {
    $message .= t('Uninstalled Module "media_entity_twitter" successfully. It is not yet stable enough. You may want to remove it now.');
  }
  $message .= "\n";
  if (\Drupal::service('module_installer')->uninstall(['media_entity_slideshow'])) {
    $message .= t('Uninstalled Module "media_entity_slideshow" successfully. We use our custom solution. You may want to remove it now.');
  }
  $message .= "\n";
  // Does not work due to uninstall error. Uninstall manually and report an
  // issue to the maintainer!
  // if(\Drupal::service('module_installer')->uninstall(['media_entity_instagram'])){
  // $message .= 'Uninstalled Module "media_entity_instagram" successfully.
  // It is not yet stable enough. You may want to remove it now.';
  // }.
  $message .= t('Could NOT uninstall Module "media_entity_instagram" successfully due to a module error. Please uninstall manually and remove it afterwards.');
  return $message;
}

/**
 * Install Drupal Core Media Library module.
 */
function drowl_media_update_8206(&$sandbox) {
  // Doesn't work - no clue why. In UI it does, perhaps cache!?
  try {
    // Remove old media_entity media_library view:
    \Drupal::configFactory()->getEditable('views.view.media_library')->delete();
    if (\Drupal::service('module_installer')->install(['media_library'])) {
      return t('Enabled Module "media_library" successfully.');
    }
  }
  catch (MissingDependencyException $e) {
    return t('Module "@module" was not enabled due to the following error: "@message"', ['@module' => 'media_library', '@message' => $e->getMessage()]);
  }
}

/**
 * Implements hook_install().
 */
function drowl_media_install() {
  /** @var \Drupal\Core\Extension\ModuleExtensionList $extension_list */
  $source = \Drupal::service('extension.list.module')->getPath('drowl_media') . '/images/icons';
  $destination = \Drupal::config('media.settings')->get('icon_base_uri');
  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
  $file_system->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);

  $files = $file_system->scanDirectory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/');
  foreach ($files as $file) {
    // When reinstalling the media module we don't want to copy the icons when
    // they already exist. The icons could be replaced (by a contrib module or
    // manually), so we don't want to replace the existing files. Removing the
    // files when we uninstall could also be a problem if the files are
    // referenced somewhere else. Since showing an error that it was not
    // possible to copy the files is also confusing, we silently do nothing.
    if (!file_exists($destination . DIRECTORY_SEPARATOR . $file->filename)) {
      $file_system->copy($file->uri, $destination, FileExists::Error);
    }
  }
}

/**
 * Change Media Slideshow Media Source to slideshow and set
 * field_media_slides_ref as source field.
 */
function drowl_media_update_8300(&$sandbox) {
  /** @var \Drupal\Core\Entity\EntityFieldManager $entityFieldManager */
  $entityFieldManager = Drupal::service('entity_field.manager');

  /** @var \Drupal\media\MediaTypeInterface $mediaTypeStorage */
  $mediaTypeStorage = \Drupal::entityTypeManager()->getStorage('media_type');

  // Change source_field to field_image.
  $field_media_image_fields = $entityFieldManager->getFieldDefinitions('media', 'slideshow');
  $mediaTypeStorageSlideshow = $mediaTypeStorage->load('slideshow');
  if (!empty($mediaTypeStorageSlideshow) && isset($field_media_image_fields['field_media_slides_ref']) && $mediaTypeStorageSlideshow->get('source') == 'generic') {
    $mediaTypeStorageSlideshow->set('source', 'slideshow');
    $mediaTypeStorageSlideshow->set('source_configuration', [
      'source_field' => 'field_media_slides_ref',
    ]);
    $mediaTypeStorageSlideshow->save();

    // Due to unknown cause, to recalculate dependencies (remove media_entity_generic)
    // we have to reload the storage and safe it again. Other ways didn't work.
    $mediaTypeStorageSlideshow = $mediaTypeStorage->load('slideshow');
    $mediaTypeStorageSlideshow->save();
    return t('Updated media slideshow source field to field_media_slides_ref successfully!');
  }
  return t('Updating media slideshow source field to field_media_slides_ref FAILED. Please check the media slideshow settings (admin/structure/media/manage/slideshow)!');
}

/**
 * Change Media slide Media Source to image and set
 * field_media_slides_ref as source field.
 */
function drowl_media_update_8301(&$sandbox) {
  /** @var \Drupal\Core\Entity\EntityFieldManager $entityFieldManager */
  $entityFieldManager = Drupal::service('entity_field.manager');

  /** @var \Drupal\media\MediaTypeInterface $mediaTypeStorage */
  $mediaTypeStorage = \Drupal::entityTypeManager()->getStorage('media_type');

  // Change source_field to field_image.
  $field_media_image_fields = $entityFieldManager->getFieldDefinitions('media', 'slide');
  $mediaTypeStorageSlide = $mediaTypeStorage->load('slide');
  if (isset($field_media_image_fields['field_image']) && $mediaTypeStorageSlide->get('source') == 'generic') {
    $mediaTypeStorageSlide->set('source', 'image');
    $mediaTypeStorageSlide->set('source_configuration', [
      'source_field' => 'field_image',
    ]);
    $mediaTypeStorageSlide->save();

    // Due to unknown cause, to recalculate dependencies (remove media_entity_generic)
    // we have to reload the storage and safe it again. Other ways didn't work.
    $mediaTypeStorageSlide = $mediaTypeStorage->load('slide');
    $mediaTypeStorageSlide->save();
    return t('Updated media slide source field to field_image successfully!');
  }
  return t('Updating media slide source field to field_image FAILED. Please check the media slide settings (admin/structure/media/manage/slide)!');
}

/**
 * Add information to uninstall media_entity_generic now.
 */
function drowl_media_update_8302(&$sandbox) {
  if (\Drupal::moduleHandler()->moduleExists('media_entity_generic')) {
    return t('You may now want to UNINSTALL AND REMOVE "media_entity_generic" module, if it is not used by other modules.');
  }
  else {
    return t('The "media_entity_generic" module is not installed, everything is fine already!');
  }
}

/**
 * Version 3.0.1: Replace media_library view with latest release from drowl_media module.
 * Warning: All customizations will be overwritten!
 */
function drowl_media_update_8303(&$sandbox) {
  _drowl_media_install_replace_view_media_library($sandbox);
}

/**
 * Version 3.1.1: Replace media_library view with latest release from drowl_media module.
 * Warning: All customizations will be overwritten!
 */
function drowl_media_update_8304(&$sandbox) {
  _drowl_media_install_replace_view_media_library($sandbox);
}

/**
 * Helper function to replace the existing media_library view with the
 * media_library view from the modules config.
 *
 * @param mixed $sandbox
 *
 * @return void
 */
function _drowl_media_install_replace_view_media_library(&$sandbox) {
  $message = 'Replacing media_library view with latest release from drowl_media module.';
  $message .= "\n";
  $view_id = 'media_library';
  // Only delete the old view, if it is existing.
  if (\Drupal::moduleHandler()->moduleExists('views') && View::load($view_id)) {
    // Delete the old view.
    $oldViewConfig = \Drupal::service('config.factory')->getEditable('views.view.' . $view_id);
    $oldViewConfig->delete();
    $message .= 'The old "' . $view_id . '" view has been deleted.';
  }
  else {
    $message .= 'Not deleting the old "' . $view_id . '" view, since it did not exists.';
  }
  $message .= "\n";

  // Only create if the new view doesn't exist and views is enabled.
  $config_path = \Drupal::service('extension.list.module')->getPath('drowl_media') . '/config/optional/views.view.' . $view_id . '.yml';
  $data = Yaml::parseFile($config_path);
  if (\Drupal::moduleHandler()->moduleExists('views') && !View::load($view_id) && !empty($data)) {
    \Drupal::configFactory()->getEditable('views.view.' . $view_id)->setData($data)->save(TRUE);
    $message .= 'The new "' . $view_id . '" view has been created.';
  }
  else {
    $message .= 'Not creating the "' . $view_id . '" view, since it already exists.';
  }
  return $message;
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc