Skip to content

Commit

Permalink
Node and media delete action (#1041)
Browse files Browse the repository at this point in the history
* added node and media deletion action

* Update islandora.schema.yml

* Added config install hook

* Codered
  • Loading branch information
ajstanley authored Jul 30, 2024
1 parent 4821cca commit c9d778e
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 2 deletions.
12 changes: 12 additions & 0 deletions config/install/system.action.delete_node_and_media.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
status: true
dependencies:
enforced:
module:
- islandora
module:
- islandora
id: delete_node_and_media
label: 'Delete node(s) and associated media'
type: node
plugin: delete_node_and_media
configuration: { }
4 changes: 4 additions & 0 deletions config/schema/islandora.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ action.configuration.delete_media_and_file:
type: action_configuration_default
label: 'Delete media and file'

action.configuration.delete_node_and_media:
type: action_configuration_default
label: 'Delete node and media'

condition.plugin.node_has_term:
type: condition.plugin
mapping:
Expand Down
15 changes: 13 additions & 2 deletions islandora.install
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\Core\Extension\ExtensionNameLengthException;
use Drupal\Core\Extension\MissingDependencyException;
use Drupal\Core\Utility\UpdateException;
use Symfony\Component\Yaml\Yaml;

/**
* Adds common namespaces to jsonld.settings.
Expand Down Expand Up @@ -221,8 +222,18 @@ function islandora_update_8008() {
if ($config) {
$config->set('redirect_after_media_save', FALSE);
$config->save(TRUE);
return t('A new configuration option, "Redirect after media save" is now available.
It has been turned off to preserve existing behaviour. To enable this setting visit
return t('A new configuration option, "Redirect after media save" is now available.
It has been turned off to preserve existing behaviour. To enable this setting visit
Configuration > Islandora > Core Settings.');
}
}

/**
* Add "Delete node and media" action.
*/
function islandora_update_9001(&$sandbox) {
$config_id = 'system.action.delete_node_and_media';
$config_path = \Drupal::service('extension.list.module')->getPath('islandora') . '/config/install/' . $config_id . '.yml';
$data = Yaml::parseFile($config_path);
\Drupal::configFactory()->getEditable($config_id)->setData($data)->save(TRUE);
}
7 changes: 7 additions & 0 deletions islandora.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,10 @@ islandora.confirm_delete_media_and_file:
_form: 'Drupal\islandora\Form\ConfirmDeleteMediaAndFile'
requirements:
_permission: 'administer media+delete any media'

islandora.confirm_delete_node_and_media:
path: '/node/delete_with_media'
defaults:
_form: 'Drupal\islandora\Form\ConfirmDeleteNodeAndMedia'
requirements:
_permission: 'administer media+delete any media'
163 changes: 163 additions & 0 deletions src/Form/ConfirmDeleteNodeAndMedia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

namespace Drupal\islandora\Form;

use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Form\DeleteMultipleForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
use Drupal\islandora\IslandoraUtils;
use Drupal\islandora\MediaSource\MediaSourceService;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Confirmation form for the 'Delete node(s) and media' action.
*/
class ConfirmDeleteNodeAndMedia extends DeleteMultipleForm {

/**
* Media source service.
*
* @var \Drupal\islandora\MediaSource\MediaSourceService
*/
protected $mediaSourceService;

/**
* Logger.
*
* @var Psr\Log\LoggerInterface
*/
protected $logger;

/**
* Deleted media count.
*
* @var string
*/
protected $deletedMediaCount = [];

/**
* Deleted file count.
*
* @var string
*/
protected $deletedFileCount = [];

/**
* List of nodes targeted.
*
* @var array
*/
protected $selection = [];

/**
* Entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;

/**
* The Islandora Utils service.
*
* @var \Drupal\islandora\IslandoraUtils
*/
protected IslandoraUtils $utils;

/**
* {@inheritdoc}
*/
public function __construct(AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, PrivateTempStoreFactory $temp_store_factory, MessengerInterface $messenger, IslandoraUtils $utils, MediaSourceService $media_source_service, LoggerInterface $logger) {
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->tempStore = $temp_store_factory->get('node_and_media_delete_confirm');
$this->messenger = $messenger;
$this->utils = $utils;
$this->mediaSourceService = $media_source_service;
$this->logger = $logger;
$this->deletedMediaCount = 0;
$this->deletedFileCount = 0;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_user'),
$container->get('entity_type.manager'),
$container->get('entity_field.manager'),
$container->get('tempstore.private'),
$container->get('messenger'),
$container->get('islandora.utils'),
$container->get('islandora.media_source_service'),
$container->get('logger.channel.islandora'));
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'node_and_media_delete_confirm_form';
}

/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->formatPlural(count($this->selection),
'Are you sure you want to delete this node and its associated media and files?',
'Are you sure you want to delete these nodes and their associated media and files?');
}

/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.media.collection');
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
return parent::buildForm($form, $form_state, 'node');
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$deleted_media = 0;
$node_storage = $this->entityTypeManager->getStorage('node');
$nodes = $node_storage->loadMultiple(array_keys($this->selection));
$deleteable_nodes = [];
foreach ($nodes as $node) {
if ($node->access('delete', $this->currentUser)) {
$deleteable_nodes[] = $node;
}
else {
$nondeleteable_nodes = $node;
}
}
foreach ($deleteable_nodes as $candidate) {
$media = $this->utils->getMedia($candidate);
$this->utils->deleteMediaAndFiles($media);
$candidate->delete();
}
$this->messenger->addStatus($this->getDeletedMessage(count($deleteable_nodes)));
if ($nondeleteable_nodes) {
$failures = count($nondeleteable_nodes);
$this->messenger->addStatus($this->formatPlural($failures, 'Unable to delete 1 node', 'Unable to delete @count nodes'));
}
$this->tempStore->delete($this->currentUser->id());
$form_state->setRedirectUrl($this->getCancelUrl());
}

}
46 changes: 46 additions & 0 deletions src/Plugin/Action/DeleteNodeAndMedia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Drupal\islandora\Plugin\Action;

use Drupal\Core\Action\Plugin\Action\DeleteAction;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;

/**
* Deletes a node, its media and its source file.
*
* @Action(
* id = "delete_node_and_media",
* label = @Translation("Delete node(s) and associated media"),
* type = "node",
* confirm_form_route_name = "islandora.confirm_delete_node_and_media"
* )
*/
class DeleteNodeAndMedia extends DeleteAction {

/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, PrivateTempStoreFactory $temp_store_factory, AccountInterface $current_user) {
$this->currentUser = $current_user;
$this->tempStore = $temp_store_factory->get('node_and_media_delete_confirm');
$this->entityTypeManager = $entity_type_manager;
$this->configuration = $configuration;
$this->pluginId = $plugin_id;
$this->pluginDefinition = $plugin_definition;
}

/**
* {@inheritdoc}
*/
public function executeMultiple(array $entities): void {
$selection = [];
foreach ($entities as $entity) {
$langcode = $entity->language()->getId();
$selection[$entity->id()][$langcode] = $langcode;
}
$this->tempStore->set("{$this->currentUser->id()}:node", $selection);
}

}

0 comments on commit c9d778e

Please sign in to comment.