You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.3 KiB
92 lines
2.3 KiB
<?php |
|
|
|
declare(strict_types=1); |
|
|
|
namespace Drupal\ocfl_export\Plugin\Action; |
|
|
|
use Drupal\Core\Action\ActionBase; |
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
use Drupal\Core\Session\AccountInterface; |
|
use Drupal\node\NodeInterface; |
|
use Drupal\ocfl_export\OcflExporter; |
|
use Psr\Log\LoggerInterface; |
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
|
|
/** |
|
* Exports a node to OCFL storage. |
|
* |
|
* @Action( |
|
* id = "ocfl_export_node", |
|
* label = @Translation("Export to OCFL"), |
|
* type = "node", |
|
* confirm = TRUE, |
|
* ) |
|
*/ |
|
class ExportNodeToOcfl extends ActionBase implements ContainerFactoryPluginInterface { |
|
|
|
public function __construct( |
|
array $configuration, |
|
$plugin_id, |
|
$plugin_definition, |
|
protected OcflExporter $exporter, |
|
protected LoggerInterface $logger, |
|
) { |
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
|
} |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { |
|
return new static( |
|
$configuration, |
|
$plugin_id, |
|
$plugin_definition, |
|
$container->get('ocfl_export.exporter'), |
|
$container->get('logger.factory')->get('ocfl_export'), |
|
); |
|
} |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public function execute(?NodeInterface $node = NULL): void { |
|
if (!$node) { |
|
return; |
|
} |
|
|
|
try { |
|
$success = $this->exporter->exportNode($node, 'Bulk action export'); |
|
|
|
if ($success) { |
|
$this->logger->info('Batch exported node @nid (@title) to OCFL.', [ |
|
'@nid' => $node->id(), |
|
'@title' => $node->getTitle(), |
|
]); |
|
} |
|
else { |
|
$this->logger->error('Failed to batch export node @nid to OCFL.', [ |
|
'@nid' => $node->id(), |
|
]); |
|
} |
|
} |
|
catch (\Exception $e) { |
|
$this->logger->error('Error exporting node @nid to OCFL: @message', [ |
|
'@nid' => $node->id(), |
|
'@message' => $e->getMessage(), |
|
]); |
|
} |
|
} |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public function access($object, ?AccountInterface $account = NULL, $return_as_object = FALSE): bool { |
|
/** @var \Drupal\node\NodeInterface $object */ |
|
$access = $object->access('view', $account, TRUE) |
|
->andIf($object->access('update', $account, TRUE)); |
|
|
|
return $return_as_object ? $access : $access->isAllowed(); |
|
} |
|
|
|
}
|
|
|