foldershare-8.x-1.2/foldershare.module
foldershare.module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | <?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 , ]); } } |