Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node and media delete action #1041

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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);
}

}
Loading