file_browser-8.x-1.3/file_browser.install

file_browser.install
<?php

/**
 * @file
 * Defines library requirements and install routines for File Browser.
 */

use Drupal\Component\Serialization\Yaml;
use Drupal\Core\File\FileExists;
use Drupal\image\Entity\ImageStyle;
use Drupal\views\Entity\View;

/**
 * Implements hook_install().
 */
function file_browser_install() {
  // Add an icon for File Browser if we aren't installing from configuration.
  if (!\Drupal::isConfigSyncing()) {
    $data = file_get_contents(dirname(__FILE__) . '/file_browser_icon.svg');
    /** @var \Drupal\file\FileInterface $file */
    $file = \Drupal::service('file.repository')->writeData($data, 'public://file_browser_icon.svg', FileExists::Replace);
    if ($file) {
      // Set file uuid same as default config.
      $uuid = Yaml::decode(file_get_contents(dirname(__FILE__) . '/config/install/embed.button.file_browser.yml'))['icon_uuid'];
      $file->set('uuid', $uuid);
      $file->save();
      \Drupal::service('file.usage')->add($file, 'embed', 'embed_button', 'file_browser');
    }
  }
}

/**
 * Implements hook_uninstall().
 */
function file_browser_uninstall() {
  Drupal::configFactory()->getEditable('image.style.file_entity_browser_small')->delete();
  Drupal::configFactory()->getEditable('image.style.file_entity_browser_thumbnail')->delete();
  Drupal::configFactory()->getEditable('embed.button.file_browser')->delete();
}

/**
 * Implements hook_requirements().
 */
function file_browser_requirements($phase) {

  $requirements = [];

  $backbone_path = DRUPAL_ROOT . '/libraries/backbone/backbone-min.js';
  $underscore_path = DRUPAL_ROOT . '/libraries/underscore/underscore-min.js';
  $imagesloaded_path = DRUPAL_ROOT . '/libraries/imagesloaded/imagesloaded.pkgd.min.js';
  $masonry_path = DRUPAL_ROOT . '/libraries/masonry/dist/masonry.pkgd.min.js';

  // Optionally use the Libraries module to determine our library paths.
  if (\Drupal::moduleHandler()->moduleExists('libraries')) {
    // Use the library.libraries_directory_file_finder service if available.
    if (\Drupal::hasService('library.libraries_directory_file_finder')) {
      $backbone_path = \Drupal::service('library.libraries_directory_file_finder')->find('backbone');
      $underscore_lib_path = \Drupal::service('library.libraries_directory_file_finder')->find('underscore');
      $imagesloaded_lib_path = \Drupal::service('library.libraries_directory_file_finder')->find('imagesloaded');
      $masonry_lib_path = \Drupal::service('library.libraries_directory_file_finder')->find('masonry');

      if ($backbone_path) {
        $backbone_path = $backbone_path . '/backbone-min.js';
      }

      if ($underscore_lib_path) {
        $underscore_path = $underscore_lib_path . '/underscore-min.js';
      }

      if ($imagesloaded_lib_path) {
        $imagesloaded_path = $imagesloaded_lib_path . '/imagesloaded.pkgd.min.js';
      }

      if ($masonry_lib_path) {
        $masonry_path = $masonry_lib_path . '/dist/masonry.pkgd.min.js';
      }
    }
  }

  if (!file_exists($backbone_path)) {
    $requirements['backbone'] = [
      'title' => t('Backbone library missing'),
      'description' => t(
    'File Browser requires the Backbone library. Download the newest release from
https://backbonejs.org/ and place it in /libraries'
      ),
      'severity' => REQUIREMENT_ERROR,
    ];
  }

  if (!file_exists($underscore_path)) {
    $requirements['underscore'] = [
      'title' => t('Underscore library missing'),
      'description' => t(
    'File Browser requires the Underscore library. Download the newest release from
https://underscorejs.org/ and place it in /libraries'
      ),
      'severity' => REQUIREMENT_ERROR,
    ];
  }

  if (!file_exists($imagesloaded_path)) {
    $requirements['imagesloaded'] = [
      'title' => t('imagesLoaded library missing'),
      'description' => t(
    'File Browser requires the imagesLoaded library. Download the newest release
from https://github.com/desandro/imagesloaded releases and place it in /libraries'
      ),
      'severity' => REQUIREMENT_ERROR,
    ];
  }

  if (!file_exists($masonry_path)) {
    $requirements['masonry'] = [
      'title' => t('Masonry library missing'),
      'description' => t(
    'File Browser requires the Masonry library. Download the newest release from
https://github.com/desandro/masonry/releases and place it in /libraries'
      ),
      'severity' => REQUIREMENT_ERROR,
    ];
  }

  // Prevent installation if the webserver cannot write to "public://" .
  $public_path = \Drupal::service('file_system')->realpath('public://');
  if ($public_path !== FALSE && !\Drupal::isConfigSyncing() && !is_writable($public_path)) {
    $requirements['public_files_not_writable'] = [
      'title' => t('Public files directory not writable'),
      'description' => t('The webserver needs to be able to write to the public files directory (public://). Please check your server configuration and try again.'),
      'severity' => REQUIREMENT_ERROR,
    ];
  }

  return $requirements;
}

/**
 * Updates entity browser configuration to remove outdated values.
 */
function file_browser_update_8001() {
  /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
  $config_factory = \Drupal::service('config.factory');
  $list = $config_factory->listAll('entity_browser.browser');

  foreach (['entity_browser.browser.browse_files', 'entity_browser.browser.browse_files_modal'] as $name) {
    if (in_array($name, $list, TRUE)) {
      $entity_browser = $config_factory->getEditable($name);
      if ($text = $entity_browser->get('display_configuration.widget_ids')) {
        $entity_browser->clear('display_configuration.widget_ids');
      }
      if ($name === 'entity_browser.browser.browse_files_modal') {
        $entity_browser->set('display_configuration.auto_open', FALSE);
      }
      $entity_browser->save();
    }
  }
}

/**
 * Updates entity browser configuration to update outdated values.
 */
function file_browser_update_8002() {
  /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
  $config_factory = \Drupal::service('config.factory');
  $list = $config_factory->listAll('entity_browser.browser');

  foreach (['entity_browser.browser.browse_files', 'entity_browser.browser.browse_files_modal'] as $name) {
    if (in_array($name, $list, TRUE)) {
      $entity_browser = $config_factory->getEditable($name);
      $entity_browser->set('selection_display_configuration.selection_hidden', FALSE);
      if ($entity_browser->get('widgets.a4ad947c-9669-497c-9988-24351955a02f')) {
        $entity_browser->set('widgets.a4ad947c-9669-497c-9988-24351955a02f.settings.auto_select', TRUE);
      }
      $entity_browser->save();
    }
  }

  /** @var \Drupal\image\Entity\ImageStyle $image_style */
  if ($image_style = ImageStyle::load('file_entity_browser_small')) {
    $effect = $image_style->getEffect('374fe20a-a27c-45f5-b76d-d851cabcf7b0');
    $effect->setConfiguration([
      'uuid' => '374fe20a-a27c-45f5-b76d-d851cabcf7b0',
      'id' => 'image_scale_and_crop',
      'weight' => 1,
      'data' => [
        'width' => 100,
        'height' => 100,
      ],
    ]);
    $image_style->save();
  }
}

/**
 * Updates the file browser view to only show permanent files.
 */
function file_browser_update_8003() {
  /** @var \Drupal\views\Entity\View $view */
  if ($view = View::load('file_entity_browser')) {
    $display = &$view->getDisplay('default');
    $display['display_options']['filters']['status'] = [
      'id' => 'status',
      'table' => 'file_managed',
      'field' => 'status',
      'relationship' => 'none',
      'group_type' => 'group',
      'admin_label' => '',
      'operator' => 'in',
      'value' => [
        1 => '1',
      ],
      'group' => 1,
      'exposed' => FALSE,
      'expose' => [
        'operator_id' => '',
        'label' => '',
        'description' => '',
        'use_operator' => FALSE,
        'operator' => '',
        'identifier' => '',
        'required' => FALSE,
        'remember' => FALSE,
        'multiple' => FALSE,
        'remember_roles' => [
          'authenticated' => 'authenticated',
        ],
        'reduce' => FALSE,
      ],
      'is_grouped' => FALSE,
      'group_info' => [
        'label' => '',
        'description' => '',
        'identifier' => '',
        'optional' => TRUE,
        'widget' => 'select',
        'multiple' => FALSE,
        'remember' => FALSE,
        'default_group' => 'All',
        'default_group_multiple' => [],
        'group_items' => [],
      ],
      'entity_type' => 'file',
      'entity_field' => 'status',
      'plugin_id' => 'file_status',
    ];
    $view->save();
  }
}

/**
 * Updates alpha1 users to use the correct View machine name.
 */
function file_browser_update_8004() {
  /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
  $config_factory = \Drupal::service('config.factory');
  if (empty($config_factory->get('views.view.file_entity_browser')->getRawData()) && !empty($config_factory->get('views.view.files_entity_browser')->getRawData())) {
    $config_factory->rename('views.view.files_entity_browser', 'views.view.file_entity_browser');
    $view = $config_factory->getEditable('views.view.file_entity_browser');
    $view->set('id', 'file_entity_browser');
    $view->save();

    // Re-run file_browser_update_8003(), which never ran on the old view.
    file_browser_update_8003();

    $list = $config_factory->listAll('entity_browser.browser');

    foreach (['entity_browser.browser.browse_files', 'entity_browser.browser.browse_files_modal'] as $name) {
      if (in_array($name, $list, TRUE)) {
        $entity_browser = $config_factory->getEditable($name);
        if ($entity_browser->get('widgets.a4ad947c-9669-497c-9988-24351955a02f')) {
          $entity_browser->set('widgets.a4ad947c-9669-497c-9988-24351955a02f.settings.view', 'file_entity_browser');
        }
        $entity_browser->save();
      }
    }
  }
}

/**
 * Fix widget configuration if it was broken by a previous update.
 */
function file_browser_update_8005() {
  /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
  $config_factory = \Drupal::service('config.factory');
  $list = $config_factory->listAll('entity_browser.browser');

  $default_uuid = 'a4ad947c-9669-497c-9988-24351955a02f';

  foreach (['entity_browser.browser.browse_files', 'entity_browser.browser.browse_files_modal'] as $name) {
    if (in_array($name, $list, TRUE)) {
      $entity_browser = $config_factory->getEditable($name);
      $widgets = $entity_browser->get('widgets');
      // If the UUID exists but has no plugin ID, it was added accidentally.
      if (isset($widgets[$default_uuid]) && !isset($widgets[$default_uuid]['id'])) {
        $entity_browser->clear("widgets.$default_uuid");
        $entity_browser->save();
      }
    }
  }
}

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

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