Browse Source

move to a confirmation action form

pull/792/head
qadan 4 years ago
parent
commit
599ff072d3
  1. 8
      islandora.routing.yml
  2. 111
      src/Plugin/Action/DeleteMediaAndFile.php

8
islandora.routing.yml

@ -57,3 +57,11 @@ islandora.media_source_put_to_node:
_custom_access: '\Drupal\islandora\Controller\MediaSourceController::putToNodeAccess' _custom_access: '\Drupal\islandora\Controller\MediaSourceController::putToNodeAccess'
options: options:
_auth: ['basic_auth', 'cookie', 'jwt_auth'] _auth: ['basic_auth', 'cookie', 'jwt_auth']
islandora.confirm_delete_media_and_file:
path: '/media/delete_with_files'
defaults:
_form: 'Drupal\islandora\Form\ConfirmDeleteMediaAndFile'
requirements:
_permission: 'delete media,delete files'

111
src/Plugin/Action/DeleteMediaAndFile.php

@ -2,13 +2,10 @@
namespace Drupal\islandora\Plugin\Action; namespace Drupal\islandora\Plugin\Action;
use Drupal\Core\Action\ActionBase; use Drupal\Core\Action\Plugin\Action\DeleteAction;
use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\islandora\MediaSource\MediaSourceService; use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Deletes a media and its source file. * Deletes a media and its source file.
@ -16,105 +13,35 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* @Action( * @Action(
* id = "delete_media_and_file", * id = "delete_media_and_file",
* label = @Translation("Delete media and file"), * label = @Translation("Delete media and file"),
* type = "media" * type = "media",
* confirm_form_route_name = "islandora.confirm_delete_media_and_file"
* ) * )
*/ */
class DeleteMediaAndFile extends ActionBase implements ContainerFactoryPluginInterface { class DeleteMediaAndFile extends DeleteAction {
/**
* Media source service.
*
* @var \Drupal\islandora\MediaSource\MediaSourceService
*/
protected $mediaSourceService;
/**
* Database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Logger.
*
* @var Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\islandora\MediaSource\MediaSourceService $media_source_service
* Media source service.
* @param \Drupal\Core\Database\Connection $connection
* Database connection.
* @param Psr\Log\LoggerInterface $logger
* Logger.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
MediaSourceService $media_source_service,
Connection $connection,
LoggerInterface $logger
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->mediaSourceService = $media_source_service;
$this->connection = $connection;
$this->logger = $logger;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, PrivateTempStoreFactory $temp_store_factory, AccountInterface $current_user) {
return new static( $this->currentUser = $current_user;
$configuration, $this->tempStore = $temp_store_factory->get('media_and_file_delete_confirm');
$plugin_id, $this->entityTypeManager = $entity_type_manager;
$plugin_definition, $this->configuration = $configuration;
$container->get('islandora.media_source_service'), $this->pluginId = $plugin_id;
$container->get('database'), $this->pluginDefinition = $plugin_definition;
$container->get('logger.channel.islandora')
);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function execute($entity = NULL) { public function executeMultiple(array $entities) {
if (!$entity) {
return;
}
$transaction = $this->connection->startTransaction();
try { $selection = [];
// Delete all the source files and then the media. foreach ($entities as $entity) {
$source_field = $this->mediaSourceService->getSourceFieldName($entity->bundle()); $langcode = $entity->language()->getId();
foreach ($entity->get($source_field)->referencedEntities() as $file) { $selection[$entity->id()][$langcode] = $langcode;
$file->delete();
}
$entity->delete();
} }
catch (\Exception $e) { $this->tempStore->set("{$this->currentUser->id()}:media", $selection);
$transaction->rollBack();
$this->logger->error("Cannot delete media and its files. Rolling back transaction: @msg", ["@msg" => $e->getMessage()]);
}
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
return $object->access('delete', $account, $return_as_object);
} }
} }

Loading…
Cancel
Save