Skip to content

Commit

Permalink
Merge branch 'Islandora:2.x' into filter-by-tid
Browse files Browse the repository at this point in the history
  • Loading branch information
rosiel authored Feb 22, 2023
2 parents e73f4fc + 2794f01 commit f6ee055
Show file tree
Hide file tree
Showing 27 changed files with 693 additions and 116 deletions.
24 changes: 12 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@
}
],
"require": {
"drupal/context": "^4.0@beta",
"drupal/search_api": "~1.8",
"islandora/jsonld": "^2",
"stomp-php/stomp-php": "4.* || ^5",
"drupal/jwt": "^1.0.0-beta5",
"drupal/filehash": "^1.1 || ^2",
"drupal/prepopulate" : "^2.2",
"drupal/eva" : "^2.0",
"drupal/context": "^4",
"drupal/ctools": "^3.8 || ^4",
"drupal/eva" : "^3.0",
"drupal/features" : "^3.7",
"drupal/migrate_plus" : "^5.1",
"drupal/file_replace": "^1.1",
"drupal/filehash": "^2",
"drupal/flysystem" : "^2.0@alpha",
"drupal/jwt": "^1.0",
"drupal/migrate_plus" : "^5.1 || ^6",
"drupal/migrate_source_csv" : "^3.4",
"drupal/prepopulate" : "^2.2",
"drupal/search_api": "^1.8",
"drupal/token" : "^1.3",
"drupal/flysystem" : "^2.0@alpha",
"islandora/crayfish-commons": "^2",
"drupal/file_replace": "^1.1",
"drupal/ctools": "^3.8 || ^4"
"islandora/jsonld": "^2",
"stomp-php/stomp-php": "4.* || ^5"
},
"require-dev": {
"phpunit/phpunit": "^6",
Expand Down
1 change: 1 addition & 0 deletions config/install/islandora.settings.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
broker_url: 'tcp://localhost:61613'
jwt_expiry: '+2 hour'
gemini_url: ''
delete_media_and_files: TRUE
gemini_pseudo_bundles: []
5 changes: 4 additions & 1 deletion config/schema/islandora.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ islandora.settings:
jwt_expiry:
type: string
label: 'How long JWTs should last before expiring.'
delete_media_and_files:
type: boolean
label: 'Node Delete with Media and Files'
upload_form_location:
type: string
label: 'Upload Form Location'
Expand Down Expand Up @@ -166,7 +169,7 @@ condition.plugin.node_had_namespace:
pid_field:
type: ignore
label: 'PID field'

field.formatter.settings.islandora_image:
type: field.formatter.settings.image
label: 'Islandora image field display format settings'
Expand Down
3 changes: 3 additions & 0 deletions css/islandora.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.container .islandora-media-items {
margin: 0;
}
5 changes: 5 additions & 0 deletions islandora.libraries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
islandora:
version: VERSION
css:
theme:
css/islandora.css: {}
198 changes: 196 additions & 2 deletions islandora.module
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use Drupal\file\FileInterface;
use Drupal\taxonomy\TermInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\file\Entity\File;

/**
* Implements hook_help().
Expand Down Expand Up @@ -181,7 +183,8 @@ function islandora_file_insert(FileInterface $file) {
*/
function islandora_file_update(FileInterface $file) {
// Exit early if unchanged.
if ($file->filehash != NULL && $file->original->filehash != NULL && $file->filehash['sha1'] == $file->original->filehash['sha1']) {
if ($file->hasField('sha1') && $file->original->hasField('sha1')
&& $file->sha1->getString() == $file->original->sha1->getString()) {
return;
}

Expand Down Expand Up @@ -331,6 +334,197 @@ function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id)
}
}
}

$form_object = $form_state->getFormObject();
$utils = \Drupal::service('islandora.utils');
$config = \Drupal::config('islandora.settings')->get('delete_media_and_files');

if ($config == 1 && $form_object instanceof EntityForm) {
$entity = $form_object->getEntity();

if ($entity->getEntityTypeId() == "node" && $utils->isIslandoraType($entity->getEntityTypeId(), $entity->bundle()) && strpos($form['#form_id'], 'delete_form') !== FALSE) {
$medias = $utils->getMedia($form_state->getFormObject()->getEntity());
if (count($medias) != 0) {
$form['delete_associated_content'] = [
'#type' => 'checkbox',
'#title' => t('Delete all associated medias and nodes'),
];

$media_list = [];

foreach ($medias as $media) {
$media_list[] = $media->getName();
}

$form['container'] = [
'#type' => 'container',
'#states' => [
'visible' => [
':input[name="delete_associated_content"]' => ['checked' => TRUE],
],
],
];

$form['container']['media_items'] = [
'#theme' => 'item_list',
'#type' => 'ul',
'#items' => $media_list,
'#attributes' => ['class' => ['islandora-media-items']],
'#wrapper_attributes' => ['class' => ['container']],
'#attached' => [
'library' => [
'islandora/islandora',
],
],
];

$form['actions']['submit']['#submit'][] = 'islandora_object_delete_form_submit';
return $form;
}
}
}

return $form;
}

/**
* Implements a submit handler for the delete form.
*/
function islandora_object_delete_form_submit($form, FormStateInterface $form_state) {

$result = $form_state->getValues('delete_associated_content');
$utils = \Drupal::service('islandora.utils');

if ($result['delete_associated_content'] == 1) {

$node = $form_state->getFormObject()->getEntity();
$medias = $utils->getMedia($node);
$media_list = [];

$entity_field_manager = \Drupal::service('entity_field.manager');
$current_user = \Drupal::currentUser();
$logger = \Drupal::logger('logger.channel.islandora');
$messenger = \Drupal::messenger();

$delete_media = [];
$media_translations = [];
$media_files = [];
$entity_protected_medias = [];
$inaccessible_entities = [];

foreach ($medias as $id => $media) {
$lang = $media->language()->getId();
$selected_langcodes[$lang] = $lang;

if (!$media->access('delete', $current_user)) {
$inaccessible_entities[] = $media;
continue;
}
// Check for files.
$fields = $entity_field_manager->getFieldDefinitions('media', $media->bundle());
foreach ($fields as $field) {
$type = $field->getType();
if ($type == 'file' || $type == 'image') {
$target_id = $media->get($field->getName())->target_id;
$file = File::load($target_id);
if ($file) {
if (!$file->access('delete', $current_user)) {
$inaccessible_entities[] = $file;
continue;
}
$media_files[$id][$file->id()] = $file;
}
}
}

foreach ($selected_langcodes as $langcode) {
// We're only working with media, which are translatable.
$entity = $media->getTranslation($langcode);
if ($entity->isDefaultTranslation()) {
$delete_media[$id] = $entity;
unset($media_translations[$id]);
}
elseif (!isset($delete_media[$id])) {
$media_translations[$id][] = $entity;
}
}
}

if ($delete_media) {
foreach ($delete_media as $id => $media) {
try {
$media->delete();
$media_list[] = $id;
$logger->notice('The media %label has been deleted.', [
'%label' => $media->label(),
]);
}
catch (Exception $e) {
$entity_protected_medias[] = $id;
}
}
}

$delete_files = array_filter($media_files, function ($media) use ($entity_protected_medias) {
return !in_array($media, $entity_protected_medias);
}, ARRAY_FILTER_USE_KEY);

if ($delete_files) {
foreach ($delete_files as $files_array) {
foreach ($files_array as $file) {
$file->delete();
$logger->notice('The file %label has been deleted.', [
'%label' => $file->label(),
]);
}
}
}

$delete_media_translations = array_filter($media_translations, function ($media) use ($entity_protected_medias) {
return !in_array($media, $entity_protected_medias);
}, ARRAY_FILTER_USE_KEY);

if ($delete_media_translations) {
foreach ($delete_media_translations as $id => $translations) {
$media = $medias[$id];
foreach ($translations as $translation) {
$media->removeTranslation($translation->language()->getId());
}
$media->save();
foreach ($translations as $translation) {
$logger->notice('The media %label @language translation has been deleted', [
'%label' => $media->label(),
'@language' => $translation->language()->getName(),
]);
}
}
}

if ($inaccessible_entities) {
$messenger->addWarning("@count items have not been deleted because you do not have the necessary permissions.", [
'@count' => count($inaccessible_entities),
]);
}

$build = [
'heading' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => t("The repository item @node and @media", [
'@node' => $node->getTitle(),
'@media' => \Drupal::translation()->formatPlural(
count($media_list), 'the media with the id @media has been deleted.',
'the medias with the ids @media have been deleted.',
['@media' => implode(", ", $media_list)],
),
]),
],
];

$message = \Drupal::service('renderer')->renderPlain($build);
$messenger->deleteByType('status');
$messenger->addStatus($message);
}
}

/**
Expand Down Expand Up @@ -515,7 +709,7 @@ function islandora_preprocess_views_view_table(&$variables) {

// Check for a weight selector field.
foreach ($variables['view']->field as $field_key => $field) {
if ($field->options['plugin_id'] == 'integer_weight_selector') {
if ($field->getPluginId() == 'integer_weight_selector') {

// Check if the weight selector is on the first column.
$is_first_column = array_search($field_key, array_keys($variables['view']->field)) > 0 ? FALSE : TRUE;
Expand Down
16 changes: 16 additions & 0 deletions islandora.post_update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* @file
* Post updates.
*/

/**
* Set default value for delete_media_and_files field in settings.
*/
function islandora_post_update_delete_media_and_files() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('islandora.settings');
$config->set('delete_media_and_files', TRUE);
$config->save(TRUE);
}
2 changes: 1 addition & 1 deletion islandora.tokens.inc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function islandora_tokens($type, $tokens, array $data, array $options, Bubbleabl
if ($media) {
$file = \Drupal::service('islandora.media_source_service')->getSourceFile($media);
if (!empty($file)) {
$url = $file->url();
$url = $file->createFileUrl();
$replacements[$original] = $url;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module: core
locked: false
cardinality: 1
translatable: true
indexes: { }
indexes:
value:
- value
persist_with_no_fields: false
custom_storage: false
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
algos:
sha1: sha1
blake2b_128: '0'
blake2b_160: '0'
blake2b_224: '0'
blake2b_256: '0'
blake2b_384: '0'
blake2b_512: '0'
md5: '0'
sha1: sha1
sha224: '0'
sha256: '0'
sha384: '0'
sha512_224: '0'
sha512_256: '0'
sha512: '0'
sha3_224: '0'
sha3_256: '0'
sha3_384: '0'
sha3_512: '0'
dedupe: 0
rehash: true
original: true
dedupe_original: false
mime_types: { }
Loading

0 comments on commit f6ee055

Please sign in to comment.