foldershare-8.x-1.2/foldershare.module
foldershare.module
<?php
/**
* @file
* Implements the principal entry points and hooks for the module.
*
* @ingroup foldershare
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\foldershare\Constants;
use Drupal\foldershare\ManageLog;
use Drupal\foldershare\ManageFileSystem;
use Drupal\foldershare\Settings;
use Drupal\foldershare\Entity\FolderShareScheduledTask;
use Drupal\foldershare\Entity\Controller\FolderShareViewController;
use Drupal\foldershare\Utilities\FileUtilities;
// "foldershare.tokens.inc" and "foldershare.views.inc" are automatically
// included as needed by the hook_hook_info() implementations in the
// Drupal core "system" and "views" modules.
require_once __DIR__ . '/foldershare.help.inc';
require_once __DIR__ . '/foldershare.user.inc';
require_once __DIR__ . '/foldershare.theme.inc';
require_once __DIR__ . '/foldershare.file.inc';
require_once __DIR__ . '/foldershare.search.inc';
/*----------------------------------------------------------------------
*
* Define pseudo-entities.
*
* Pseudo-field handling for the field UI module's "Manage fields" and
* "Manage display" pages is forwarded to the FolderShareViewController
* class, which handles the presentation of an entity.
*
*----------------------------------------------------------------------*/
/**
* Implements hook_entity_extra_field_info().
*
* Forward to the FolderShareViewController class.
*/
function foldershare_entity_extra_field_info() {
return FolderShareViewController::getEntityExtraFieldInfo();
}
/**
* Implements hook_ENTITY_TYPE_view().
*
* Forward to the FolderShareViewController class.
*/
function foldershare_foldershare_view(
array &$build,
EntityInterface $entity,
EntityViewDisplayInterface $display,
string $viewMode) {
return FolderShareViewController::getFolderShareView(
$build,
$entity,
$display,
$viewMode);
}
/*----------------------------------------------------------------------
*
* Handle menu hook.
*
*----------------------------------------------------------------------*/
/**
* Implements hook_menu_links_discovered_alter().
*
* Update the module's root list menu items to use the same titles as
* the routes they use. Then add manage fields/form/display menu items
* for FolderShare if the "field_ui" module is installed.
*/
function foldershare_menu_links_discovered_alter(&$links) {
//
// Root list menu item titles.
// ---------------------------
// Menu items for each of the root lists are defined in FolderShare's
// "foldershare.links.menu.yml" file and given default titles. But we'd
// prefer that menu item titles match the titles of the routes the menu
// items use. There is no way to do that in YML, so they titles are
// patched here.
$routeProvider = \Drupal::service('router.route_provider');
$titleResolver = \Drupal::service('title_resolver');
$dummyRequest = new Request();
$menuLinksToFix = [
'foldershare.rootfolderlist',
'foldershare.rootitems.public',
'foldershare.rootitems.all',
];
foreach ($menuLinksToFix as $linkName) {
if (isset($links[$linkName]) === TRUE &&
isset($links[$linkName]['route_name']) === TRUE) {
$routeName = $links[$linkName]['route_name'];
$route = $routeProvider->getRouteByName($routeName);
$links[$linkName]['title'] = $titleResolver->getTitle($dummyRequest, $route);
}
}
//
// Field UI menu items.
// --------------------
// Menu items for the Field UI module's forms to manage fields, forms,
// and page display cannot be included in FolderShare's
// "foldershare.links.menu.yml" without making the Field UI module a
// required dependency. If it is not a dependency and the module is
// disabled, the menu items become invalid and Drupal complains.
// So we have to define them here, if the Field UI module is installed.
if (\Drupal::moduleHandler()->moduleExists('field_ui') === TRUE) {
// Manage fields.
$routeName = 'entity.foldershare.field_ui_fields';
$route = $routeProvider->getRouteByName($routeName);
$links['foldershare.admin.structure.fields'] = [
'title' => $titleResolver->getTitle($dummyRequest, $route),
'route_name' => $routeName,
'parent' => 'foldershare.admin.structure.settings',
'weight' => 1,
];
// Manage form display.
$routeName = 'entity.entity_form_display.foldershare.default';
$route = $routeProvider->getRouteByName($routeName);
$links['foldershare.admin.structure.form-display'] = [
'title' => $titleResolver->getTitle($dummyRequest, $route),
'route_name' => $routeName,
'parent' => 'foldershare.admin.structure.settings',
'weight' => 2,
];
// Manage display.
$routeName = 'entity.entity_view_display.foldershare.default';
$route = $routeProvider->getRouteByName($routeName);
$links['foldershare.admin.structure.display'] = [
'title' => $titleResolver->getTitle($dummyRequest, $route),
'route_name' => $routeName,
'parent' => 'foldershare.admin.structure.settings',
'weight' => 3,
];
}
}
/*----------------------------------------------------------------------
*
* Handle CRON hook.
*
*----------------------------------------------------------------------*/
/**
* Implements hook_cron().
*
* The module's scheduled task handler is invoked to execute any
* ready tasks.
*
* CRON and this hook are not invoked if the site is in maintenance mode.
*/
function foldershare_cron() {
FolderShareScheduledTask::executeTasks(\Drupal::time()->getRequestTime());
}
/*----------------------------------------------------------------------
*
* Handle REST resource hook.
*
*----------------------------------------------------------------------*/
/**
* Implements hook_rest_resource_alter().
*
* Block the REST module's default implementation for FolderShare entities.
* The default implementation blindly sets entity fields without the logic
* in FolderShare's API. This can lead to corrupted entities with bad parent
* and root IDs, invalid kinds, missing default grants, and more.
*/
function foldershare_rest_resource_alter(&$definitions) {
if (isset($definitions['entity:foldershare']) === TRUE) {
if ($definitions['entity:foldershare']['provider'] === 'rest') {
// Do not allow the default REST module's default implementation
// for the FolderShare entity.
unset($definitions['entity:foldershare']);
}
elseif ($definitions['entity:foldershare']['provider'] === 'foldershare') {
// Do not allow the old FolderShare module's REST resource, which may
// be left over after a module update.
unset($definitions['entity:foldershare']);
}
}
}
/*----------------------------------------------------------------------
*
* Handle module uninstall.
*
*----------------------------------------------------------------------*/
/**
* Implements hook_module_preuninstall().
*
* The module's pending task list is cleared and its directory tree of
* uploaded files is deleted.
*/
function foldershare_module_preuninstall($module) {
if ($module !== Constants::MODULE) {
return;
}
// Remove the work queue. This queue is no longer used, but it may be
// left over from an earlier install.
try {
$queue = \Drupal::queue('foldershare_handle_work_queue', TRUE);
if ($queue !== NULL) {
$queue->deleteQueue();
}
}
catch (\Exception $e) {
// Do nothing.
}
// Delete all scheduled tasks.
FolderShareScheduledTask::deleteAllTasks();
// Delete the search page. This should be done automatically by Drupal
// on module uninstall, but sometimes it is not.
if (\Drupal::moduleHandler()->moduleExists('search') === TRUE) {
$searchRepository = \Drupal::service('search.search_page_repository');
if ($searchRepository !== NULL) {
$searchPages = $searchRepository->getActiveSearchPages();
if (empty($searchPages) === FALSE &&
isset($searchPages[Constants::SEARCH_PLUGIN]) === TRUE) {
$searchPages[Constants::SEARCH_PLUGIN]->delete();
}
}
}
// Delete all files and directories created by the module.
$uri = Settings::getFileScheme() . '://' . ManageFileSystem::FILE_DIRECTORY;
try {
FileUtilities::rrmdir($uri);
}
catch (\Exception $e) {
// Deletion throws an exception if the directory, or any of its
// descendants, cannot be deleted. This could be due to a permissions
// problem, an unmounted file system, or perhaps the directory has
// already been deleted.
//
// In any case, there's nothing we can do.
ManageLog::error(
"Local file system: Uninstallation of the @module module cannot delete the module's local directory.\nThe directory is at \"@uri\". Attempting to delete it threw exception with the message \"@message\".",
[
'@module' => Constants::MODULE,
'@uri' => $uri,
'@message' => $e->getMessage(),
'exception' => $e,
]);
}
}
