Skip to content

Commit

Permalink
Updated hook uninstall
Browse files Browse the repository at this point in the history
  • Loading branch information
yeniatencio committed Sep 20, 2024
1 parent 1eaacef commit 796364c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 77 deletions.
116 changes: 41 additions & 75 deletions modules/tide_media_secure_files/tide_media_secure_files.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,51 @@
* Tide Media Secure Files.
*/

use Drupal\user\Entity\Role;

/**
* Implements hook_uninstall().
*/
function tide_media_secure_files_uninstall() {
\Drupal::configFactory()->getEditable('pfdp.pfdp_directory.secure')->delete();

$secure_file_media_type = 'secure_file';
$ids = \Drupal::entityQuery('media')
->condition('bundle', $secure_file_media_type)
->accessCheck(FALSE)
->execute();
$storageHandler = \Drupal::entityTypeManager()->getStorage('media');
$entities = $storageHandler->loadMultiple($ids);
if ($entities) {
foreach ($entities as $entity) {
$entity->delete();
}
}

$media_type = \Drupal::entityTypeManager()
->getStorage('media_type')
->load($secure_file_media_type);
if ($media_type) {
$media_type->delete();
}

$secure_file_user_role = 'secure_file_user';
$secure_file_users = \Drupal::entityTypeManager()->getStorage('user')->loadByProperties(['roles' => 'secure_file_user']);
if ($secure_file_users) {
foreach ($secure_file_users as $user) {
$user->removeRole($secure_file_user_role);
$user->save();
}
}

$role = Role::load($secure_file_user_role);
if ($role) {
$role->delete();
}

drupal_flush_all_caches();

\Drupal::service('module_installer')->uninstall(['pfdp']);
}

/**
Expand All @@ -21,78 +62,3 @@ function tide_media_secure_files_install() {
$module_installer->install(['pfdp']);
}
}

/**
* Enable Private files download permission module.
*/
/* function tide_media_update_10004() {
if (!\Drupal::moduleHandler()->moduleExists('pfdp')) {
$module_installer = \Drupal::service('module_installer');
$module_installer->install(['pfdp']);

$configs = [
'pfdp.pfdp_directory.secure',
'media.type.secure_file',
'user.role.secure_file_user',
];

$config_path = \Drupal::service('extension.list.module')->getPath('tide_media') . '/config/install';
$source = new FileStorage($config_path);
$config_storage = \Drupal::service('config.storage');

foreach ($configs as $config) {
$config_storage->write($config, $source->read($config));
}

// Add field.
$field_configs = [
'field.storage.media.field_secure_file' => 'field_storage_config',
'field.field.media.secure_file.field_secure_file' => 'field_config',
];
module_load_include('inc', 'tide_core', 'includes/helpers');
$config_location = [\Drupal::service('extension.list.module')->getPath('tide_media') . '/config/install'];

// Check if field already exported to config/sync.
foreach ($field_configs as $config => $type) {
$config_read = _tide_read_config($config, $config_location, TRUE);
$storage = \Drupal::entityTypeManager()->getStorage($type);
$id = substr($config, strrpos($config, '.') + 1);
if ($storage->load($id) == NULL) {
$config_entity = $storage->createFromStorageRecord($config_read);
$config_entity->save();
}
}

$update_configs = [
'core.entity_form_display.media.secure_file.default' => 'entity_form_display',
'core.entity_view_display.media.secure_file.default' => 'entity_view_display',
];
$skipped_settings = [
'uuid',
'langcode',
'status',
'id',
'bundle',
];
foreach ($update_configs as $update_config => $type) {
$config_read = _tide_read_config($update_config, $config_location, FALSE);
$config = \Drupal::configFactory()->getEditable($update_config);
if (!$config) {
continue;
}
$data = $config->getRawData();
foreach ($config_read as $key => $item) {
if (in_array($key, $skipped_settings)) {
continue;
}
NestedArray::setValue($data, [
$key,
], NestedArray::getValue($config_read, [
$key,
]));
}
$config->setData($data)->save();
}
}
} */

2 changes: 0 additions & 2 deletions tide_media.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* Tide Media install.
*/

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\FileStorage;
use Drupal\field\Entity\FieldConfig;
use Drupal\tide_media\TideOperation;

Expand Down

0 comments on commit 796364c

Please sign in to comment.