Last active
March 29, 2019 08:50
-
-
Save franxo/4b8d0542d4fb4596ccc1334bd599f17d to your computer and use it in GitHub Desktop.
Drupal 8: Move files from custom module to private files directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
<?php | |
/** | |
* @file | |
* Install, update, and uninstall functions for my_module. | |
*/ | |
/** | |
* Copies file to the private files folder. | |
*/ | |
function my_module_install() { | |
// Create the example file directory and ensure it's writable. | |
$directory = 'private://files/my_module'; | |
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); | |
// Copy the example file to example directory. | |
$module_path = drupal_get_path('module', 'my_module'); | |
$file_source = $module_path . '/data/example.xml'; | |
file_unmanaged_copy($file_source, $directory . '/example.xml', FILE_EXISTS_REPLACE); | |
} | |
/** | |
* Removes the private files folder | |
*/ | |
function my_module_uninstall() { | |
$directory = 'private://files/my_module'; | |
file_unmanaged_delete_recursive($directory); | |
} | |
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* Uncomment line | |
``` | |
$settings['file_private_path'] = ''; | |
``` | |
* Add folder location (A relative path can be used) | |
``` | |
$settings['file_private_path'] = '../private'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment