|
|
|
@ -3,6 +3,7 @@
|
|
|
|
|
namespace Drupal\islandora\Form\AddChildrenWizard; |
|
|
|
|
|
|
|
|
|
use Drupal\Core\Database\Connection; |
|
|
|
|
use Drupal\Core\Datetime\DateFormatterInterface; |
|
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
|
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
|
|
|
use Drupal\Core\Messenger\MessengerInterface; |
|
|
|
@ -53,6 +54,13 @@ abstract class AbstractBatchProcessor {
|
|
|
|
|
*/ |
|
|
|
|
protected MessengerInterface $messenger; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* The date formatter service. |
|
|
|
|
* |
|
|
|
|
* @var \Drupal\Core\Datetime\DateFormatterInterface |
|
|
|
|
*/ |
|
|
|
|
protected DateFormatterInterface $dateFormatter; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Constructor. |
|
|
|
|
*/ |
|
|
|
@ -60,12 +68,14 @@ abstract class AbstractBatchProcessor {
|
|
|
|
|
EntityTypeManagerInterface $entity_type_manager, |
|
|
|
|
Connection $database, |
|
|
|
|
AccountProxyInterface $current_user, |
|
|
|
|
MessengerInterface $messenger |
|
|
|
|
MessengerInterface $messenger, |
|
|
|
|
DateFormatterInterface $date_formatter |
|
|
|
|
) { |
|
|
|
|
$this->entityTypeManager = $entity_type_manager; |
|
|
|
|
$this->database = $database; |
|
|
|
|
$this->currentUser = $current_user; |
|
|
|
|
$this->messenger = $messenger; |
|
|
|
|
$this->dateFormatter = $date_formatter; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -96,24 +106,25 @@ abstract class AbstractBatchProcessor {
|
|
|
|
|
/** |
|
|
|
|
* Loads the file indicated. |
|
|
|
|
* |
|
|
|
|
* @param array $info |
|
|
|
|
* An associative array containing at least: |
|
|
|
|
* - target_id: The id of the file to load. |
|
|
|
|
* @param mixed $info |
|
|
|
|
* Widget values. |
|
|
|
|
* |
|
|
|
|
* @return \Drupal\file\FileInterface |
|
|
|
|
* @return \Drupal\file\FileInterface|null |
|
|
|
|
* The loaded file. |
|
|
|
|
* |
|
|
|
|
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException |
|
|
|
|
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException |
|
|
|
|
*/ |
|
|
|
|
protected function getFile(array $info) : FileInterface { |
|
|
|
|
return $this->entityTypeManager->getStorage('file')->load($info['target_id']); |
|
|
|
|
protected function getFile($info) : ?FileInterface { |
|
|
|
|
return (is_array($info) && isset($info['target_id'])) ? |
|
|
|
|
$this->entityTypeManager->getStorage('file')->load($info['target_id']) : |
|
|
|
|
NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get the node to which to attach our media. |
|
|
|
|
* |
|
|
|
|
* @param array $info |
|
|
|
|
* @param mixed $info |
|
|
|
|
* Info from the widget used to create the request. |
|
|
|
|
* @param array $values |
|
|
|
|
* Additional form inputs. |
|
|
|
@ -121,16 +132,36 @@ abstract class AbstractBatchProcessor {
|
|
|
|
|
* @return \Drupal\node\NodeInterface |
|
|
|
|
* The node to which to attach the created media. |
|
|
|
|
*/ |
|
|
|
|
abstract protected function getNode(array $info, array $values) : NodeInterface; |
|
|
|
|
abstract protected function getNode($info, array $values) : NodeInterface; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get a name to use for bulk-created assets. |
|
|
|
|
* |
|
|
|
|
* @param mixed $info |
|
|
|
|
* Widget values. |
|
|
|
|
* @param array $values |
|
|
|
|
* Form values. |
|
|
|
|
* |
|
|
|
|
* @return string |
|
|
|
|
* An applicable name. |
|
|
|
|
* |
|
|
|
|
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException |
|
|
|
|
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException |
|
|
|
|
*/ |
|
|
|
|
protected function getName($info, array $values) : string { |
|
|
|
|
$file = $this->getFile($info); |
|
|
|
|
return $file ? $file->getFilename() : strtr('Bulk ingest, {date}', [ |
|
|
|
|
'{date}' => $this->dateFormatter->format(time(), 'long'), |
|
|
|
|
]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Create a media referencing the given file, associated with the given node. |
|
|
|
|
* |
|
|
|
|
* @param \Drupal\node\NodeInterface $node |
|
|
|
|
* The node to which the media should be associated. |
|
|
|
|
* @param array $info |
|
|
|
|
* The widget info, which should have a 'target_id' identifying the target |
|
|
|
|
* file. |
|
|
|
|
* @param mixed $info |
|
|
|
|
* The widget info for the media source field. |
|
|
|
|
* @param array $values |
|
|
|
|
* Values from the wizard, which should contain at least: |
|
|
|
|
* - media_type: The machine name/ID of the media type as which to create |
|
|
|
@ -144,18 +175,16 @@ abstract class AbstractBatchProcessor {
|
|
|
|
|
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException |
|
|
|
|
* @throws \Drupal\Core\Entity\EntityStorageException |
|
|
|
|
*/ |
|
|
|
|
protected function createMedia(NodeInterface $node, array $info, array $values) : MediaInterface { |
|
|
|
|
protected function createMedia(NodeInterface $node, $info, array $values) : MediaInterface { |
|
|
|
|
$taxonomy_term_storage = $this->entityTypeManager->getStorage('taxonomy_term'); |
|
|
|
|
|
|
|
|
|
$file = $this->getFile($info); |
|
|
|
|
|
|
|
|
|
// Create a media with the file attached and also pointing at the node. |
|
|
|
|
$field = $this->getField($values); |
|
|
|
|
|
|
|
|
|
$media_values = array_merge( |
|
|
|
|
[ |
|
|
|
|
'bundle' => $values['media_type'], |
|
|
|
|
'name' => $file->getFilename(), |
|
|
|
|
'name' => $this->getName($info, $values), |
|
|
|
|
IslandoraUtils::MEDIA_OF_FIELD => $node, |
|
|
|
|
IslandoraUtils::MEDIA_USAGE_FIELD => ($values['use'] ? |
|
|
|
|
$taxonomy_term_storage->loadMultiple($values['use']) : |
|
|
|
@ -172,7 +201,7 @@ abstract class AbstractBatchProcessor {
|
|
|
|
|
); |
|
|
|
|
$media = $this->entityTypeManager->getStorage('media')->create($media_values); |
|
|
|
|
if ($media->save() !== SAVED_NEW) { |
|
|
|
|
throw new \Exception("Failed to create media for file '{$file->id()}."); |
|
|
|
|
throw new \Exception("Failed to create media."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return $media; |
|
|
|
|