file_browser.install in File Entity Browser 8
Defines library requirements and install routines for File Browser.
File
file_browser.installView source
<?php
/**
* @file
* Defines library requirements and install routines for File Browser.
*/
use Drupal\Component\Serialization\Yaml;
use Drupal\image\Entity\ImageStyle;
use Drupal\views\Entity\View;
use Drupal\Core\File\FileSystemInterface;
/**
* 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.png');
/** @var \Drupal\file\FileInterface $file */
$file = file_save_data($data, 'public://file_browser_icon.png', FileSystemInterface::EXISTS_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 = [];
// Optionally use the Libraries module to determine our library paths.
if (\Drupal::moduleHandler()
->moduleExists('libraries')) {
$imagesloaded_path = libraries_get_path('imagesloaded') . '/imagesloaded.pkgd.min.js';
$masonry_path = libraries_get_path('masonry') . '/dist/masonry.pkgd.min.js';
}
else {
$imagesloaded_path = DRUPAL_ROOT . '/libraries/imagesloaded/imagesloaded.pkgd.min.js';
$masonry_path = DRUPAL_ROOT . '/libraries/masonry/dist/masonry.pkgd.min.js';
}
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();
}
}
}
}
Functions
Name | Description |
---|---|
file_browser_install | Implements hook_install(). |
file_browser_requirements | Implements hook_requirements(). |
file_browser_uninstall | Implements hook_uninstall(). |
file_browser_update_8001 | Updates entity browser configuration to remove outdated values. |
file_browser_update_8002 | Updates entity browser configuration to update outdated values. |
file_browser_update_8003 | Updates the file browser view to only show permanent files. |
file_browser_update_8004 | Updates alpha1 users to use the correct View machine name. |
file_browser_update_8005 | Fix widget configuration if it was broken by a previous update. |