Browse Source

altered delete function

pull/756/head
Alan Stanley 5 years ago
parent
commit
df1200042e
  1. 36
      src/Form/ConfirmDeleteMediaAndFile.php
  2. 34
      src/Plugin/Action/AbstractGenerateDerivativeMediaFile.php
  3. 81
      src/Plugin/Action/DeleteMediaAndMultifiles.php

36
src/Form/ConfirmDeleteMediaAndFile.php

@ -2,6 +2,7 @@
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;
@ -9,6 +10,7 @@ use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\islandora\MediaSource\MediaSourceService;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -39,12 +41,20 @@ class ConfirmDeleteMediaAndFile extends DeleteMultipleForm {
*/
protected $selection = [];
/**
* Entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* {@inheritdoc}
*/
public function __construct(AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, PrivateTempStoreFactory $temp_store_factory, MessengerInterface $messenger, MediaSourceService $media_source_service, LoggerInterface $logger) {
public function __construct(AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, PrivateTempStoreFactory $temp_store_factory, MessengerInterface $messenger, 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('media_and_file_delete_confirm');
$this->messenger = $messenger;
$this->mediaSourceService = $media_source_service;
@ -58,6 +68,7 @@ class ConfirmDeleteMediaAndFile extends DeleteMultipleForm {
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.media_source_service'),
@ -115,15 +126,24 @@ class ConfirmDeleteMediaAndFile extends DeleteMultipleForm {
continue;
}
// Check for files.
$source_field = $this->mediaSourceService->getSourceFieldName($entity->bundle());
foreach ($entity->get($source_field)->referencedEntities() as $file) {
if (!$file->access('delete', $this->currentUser)) {
$inaccessible_entities[] = $file;
continue;
$fields = $this->entityFieldManager->getFieldDefinitions('media', $entity->bundle());
$files = [];
foreach ($fields as $field) {
$type = $field->getType();
if ($type == 'file' || $type == 'image') {
$target_id = $entity->get($field->getName())->target_id;
$file = File::load($target_id);
if ($file) {
if (!$file->access('delete', $this->currentUser)) {
$inaccessible_entities[] = $file;
continue;
}
$delete_files[$file->id()] = $file;
$total_count++;
}
}
$delete_files[$file->id()] = $file;
$total_count++;
}
foreach ($selected_langcodes as $langcode) {
// We're only working with media, which are translatable.
$entity = $entity->getTranslation($langcode);

34
src/Plugin/Action/AbstractGenerateDerivativeMediaFile.php

@ -8,9 +8,9 @@ use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\islandora\IslandoraUtils;
use Drupal\islandora\EventGenerator\EmitEvent;
use Drupal\islandora\EventGenerator\EventGeneratorInterface;
use Drupal\islandora\IslandoraUtils;
use Drupal\islandora\MediaSource\MediaSourceService;
use Drupal\jwt\Authentication\Provider\JwtAuth;
use Drupal\token\Token;
@ -177,11 +177,14 @@ class AbstractGenerateDerivativeMediaFile extends EmitEvent {
'media' => $entity,
];
$destination_field = $this->configuration['destination_field_name'];
$field = \Drupal::entityTypeManager()->getStorage('field_storage_config')->load("media.$destination_field");
$field = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->load("media.$destination_field");
$scheme = $field->getSetting('uri_scheme');
$path = $this->token->replace($data['path'], $token_data);
$data['file_upload_uri'] = $scheme . '://' . $path;
$allowed = ['queue',
$allowed = [
'queue',
'event',
'args',
'source_uri',
@ -216,6 +219,15 @@ class AbstractGenerateDerivativeMediaFile extends EmitEvent {
'#default_value' => $this->configuration['destination_field_name'],
'#description' => $this->t('File field on Media Type to hold derivative. Cannot be the same as source'),
];
$form['args'] = [
'#type' => 'textfield',
'#title' => t('Additional arguments'),
'#default_value' => $this->configuration['args'],
'#rows' => '8',
'#description' => t('Additional command line arguments'),
];
$form['mimetype'] = [
'#type' => 'textfield',
'#title' => t('Mimetype'),
@ -224,13 +236,6 @@ class AbstractGenerateDerivativeMediaFile extends EmitEvent {
'#rows' => '8',
'#description' => t('Mimetype to convert to (e.g. image/jpeg, video/mp4, etc...)'),
];
$form['args'] = [
'#type' => 'textfield',
'#title' => t('Additional arguments'),
'#default_value' => $this->configuration['args'],
'#rows' => '8',
'#description' => t('Additional command line arguments'),
];
$form['path'] = [
'#type' => 'textfield',
@ -254,17 +259,16 @@ class AbstractGenerateDerivativeMediaFile extends EmitEvent {
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::validateConfigurationForm($form, $form_state);
$exploded_mime = explode('/', $form_state->getValue('mimetype'));
if (count($exploded_mime) != 2) {
$mimetype = $form_state->getValue('mimetype');
$exploded = explode('/', $form_state->getValue($mimetype));
if (count($exploded) != 2) {
$form_state->setErrorByName(
'mimetype',
t('Please enter a mimetype (e.g. image/jpeg, video/mp4, audio/mp3, etc...)')
);
}
if (empty($exploded_mime[1])) {
if (empty($exploded[1])) {
$form_state->setErrorByName(
'mimetype',
t('Please enter a mimetype (e.g. image/jpeg, video/mp4, audio/mp3, etc...)')

81
src/Plugin/Action/DeleteMediaAndMultifiles.php

@ -1,81 +0,0 @@
<?php
namespace Drupal\islandora\Plugin\Action;
use Drupal\Core\Session\AccountInterface;
use Drupal\file\Entity\File;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
/**
* Deletes a media and all associated files.
*
* @Action(
* id = "delete_media_and_all_files",
* label = @Translation("Delete media and all associated files"),
* type = "media"
* )
*/
class DeleteMediaAndMultifiles extends DeleteMediaAndFile implements ContainerFactoryPluginInterface {
/**
* Entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
private $entityFieldManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->setEntityFieldManager($container->get('entity_field.manager'));
return $instance;
}
/**
* Sets entity field manager.
*/
public function setEntityFieldManager(EntityFieldManagerInterface $entityFieldManager) {
$this->entityFieldManager = $entityFieldManager;
}
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
if (!$entity) {
return;
}
$delete = FALSE;
$fields = $this->entityFieldManager->getFieldDefinitions('media', $entity->bundle());
$files = [];
foreach ($fields as $field) {
$type = $field->getType();
if ($type == 'file' || $type == 'image') {
$files[] = $field->getName();
}
}
foreach ($files as $field) {
$target_id = $entity->get($field)->target_id;
$file = File::load($target_id);
if ($file) {
$file->delete();
$delete = TRUE;
}
}
if ($delete) {
$entity->delete();
}
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
return $object->access('delete', $account, $return_as_object);
}
}
Loading…
Cancel
Save