Repository URL to install this package:
|
Version:
2.0.2 ▾
|
novicell/custom_forms
/
modules
/
default_fields
/
src
/
EventSubscriber
/
SubmissionEventSubscriber.php
|
|---|
<?php
namespace Drupal\custom_forms_default_fields\EventSubscriber;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\file\Entity\File;
use Drupal\file\FileUsage\FileUsageInterface;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class SubmissionEventSubscriber
*
* @package Drupal\custom_forms_default_fields\EventSubscriber
*/
class SubmissionEventSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* The logger channel factory.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* The event dispatcher service.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* The file usage service.
*
* @var \Drupal\file\FileUsage\FileUsageInterface
*/
protected $fileUsage;
public function __construct(LoggerChannelFactoryInterface $logger_factory, EventDispatcherInterface $event_dispatcher, FileUsageInterface $file_usage) {
$this->logger = $logger_factory->get('Custom Forms');
$this->eventDispatcher = $event_dispatcher;
$this->fileUsage = $file_usage;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* @return array
* The event names to listen to.
*/
public static function getSubscribedEvents() : array {
return [
'custom_form.initialize.post_transition' => ['setFileUsage', -100],
];
}
/**
* Set the file usage of any attached files to be associated with
* the submission that added them.
*
* @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
* The transition event.
*/
public function setFileUsage(WorkflowTransitionEvent $event) : void {
/** @var \Drupal\custom_forms\Entity\CustomFormSubmissionInterface $submission */
$submission = $event->getEntity();
$values = $submission->getData();
// Return if file usage processing is disabled or no files defined.
if (empty($values['_process_file_usage']) && empty($values['_files'])) {
return;
}
foreach ($values['_files'] as $machine_name => $file_data) {
/** @var \Drupal\file\FileInterface $file */
$file = File::load($file_data['fid']);
// If file failed to load, log error and skip it.
if ($file === NULL) {
$message = $this->t('Failed loading file with id @fid when processing file usage.',
['@fid' => $file_data['fid']]
);
$this->logger->error($message);
continue;
}
// Add the file usage so the uploaded file doesn't get removed by
// garbage collection.
$this->fileUsage->add($file, 'custom_forms_submission', 'image_upload', $submission->id(), 1);
}
// Remove the processing tag, as we don't need it any more.
unset($values['_process_file_usage']);
$submission->setDataArray($values);
}
}