webdam-1.0.x-dev/webdam.install

webdam.install
<?php

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

use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Utility\UpdateException;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\webdam\Plugin\media\Source\Webdam;

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

  // Test if we can connect to the Webdam API.
  if ($phase === 'runtime') {
    try {
      /** @var \Drupal\webdam\WebdamConnector $connector */
      $connector = \Drupal::service('webdam.connector');
      $connector->getClient()
        ->checkAuth();
      $requirements['webdam'] = [
        'title' => t('Webdam'),
        'value' => t('Connected to @url', ['@url' => \Drupal::config('webdam.settings')->get('account_domain')]),
      ];
    }
    catch (\Exception $e) {
      $requirements['webdam'] = [
        'title' => t('Webdam'),
        'value' => t('Error connecting to @url', ['@url' => \Drupal::config('webdam.settings')->get('account_domain')]),
        'description' => $e->getMessage(),
        'severity' => REQUIREMENT_ERROR,
      ];
    }
  }

  return $requirements;
}

/**
 * Implements hook_install().
 */
function webdam_install() {
  $source = \Drupal::service('extension.list.module')->getPath('webdam') . '/images/icons';
  $destination = \Drupal::config('media.settings')->get('icon_base_uri');
  \Drupal::service('file_system')->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
  $files = \Drupal::service('file_system')->scanDirectory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/');
  foreach ($files as $file) {
    // When reinstalling this 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)) {
      \Drupal::service('file_system')->copy($file->uri, $destination, FileSystemInterface::EXISTS_ERROR);
    }
  }
}

/**
 * Creates webdam_tranformation field.
 */
function webdam_update_20000() {
  // Do nothing if the field already exists.
  if ($transformations_field_storage = FieldStorageConfig::loadByName('media', Webdam::TRANSFORMATIONS_FIELD_NAME)) {
    if ($transformations_field_storage->getType() !== 'text') {
      throw new UpdateException('The "webdam_transformations" media field already exists. Please remove the it before running the update function.');
    }

    return;
  }

  $transformations_field_storage = NULL;

  // Create a field instance for each of the Webdam media types.
  foreach (\Drupal::service('webdam.service')->getWebdamMediaTypes() as $media_type) {
    /** @var \Drupal\webdam\Plugin\media\Source\Webdam $source */
    $source = $media_type->getSource();
    if (!$transformations_field_storage) {
      $transformations_field_storage = $source->createTransformationsFieldStorage();
      $transformations_field_storage->save();
    }

    $transformations_field = $source->createTransformationsField($media_type);
    $transformations_field->save();
  }
}

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

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