View source
<?php
namespace Drupal\flipping_book\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\flipping_book\Entity\FlippingBook;
use Drupal\flipping_book\Entity\FlippingBookType;
use Drupal\flipping_book\FlippingBookInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FlippingBookForm extends ContentEntityForm {
protected $fileSystem;
protected $archiverManager;
protected $flippingBook;
protected $location;
protected $destination;
protected $filepath;
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, FileSystemInterface $file_system, PluginManagerInterface $pluginManager, FlippingBookInterface $flipping_book) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->fileSystem = $file_system;
$this->archiverManager = $pluginManager;
$this->flippingBook = $flipping_book;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('file_system'), $container
->get('plugin.manager.archiver'), $container
->get('flipping_book'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['file']['widget'][0]['#upload_location'] = $this
->getUploadLocation();
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$this
->handleArchive($form_state);
$entity = $this->entity;
$status = parent::save($form, $form_state);
switch ($status) {
case SAVED_NEW:
$this
->messenger()
->addStatus($this
->t('Created the %label Flipping Book.', [
'%label' => $entity
->label(),
]));
break;
default:
$this
->messenger()
->addStatus($this
->t('Saved the %label Flipping Book.', [
'%label' => $entity
->label(),
]));
}
$form_state
->setRedirect('entity.flipping_book.canonical', [
'flipping_book' => $entity
->id(),
]);
}
public function getUploadLocation() {
if (!isset($this->location)) {
$this
->setUploadLocation();
}
return $this->location;
}
protected function extractUploadLocation(FlippingBook $flippingBook) {
$flipping_book_type = FlippingBookType::load($flippingBook
->bundle());
return $flipping_book_type
->get('location');
}
protected function setUploadLocation() {
$this->location = $this->flippingBook
->extractUploadLocation($this->entity);
}
protected function getFileFieldAccess() {
if ($this->entity
->get('directory')->value) {
return FALSE;
}
return TRUE;
}
protected function handleArchive(FormStateInterface $form_state) {
$file = $form_state
->getValue('file');
if (!empty($file[0]['fids'])) {
$this
->prepareExport($form_state);
try {
$this->flippingBook
->extractArchive($this->filepath, $this->destination);
} catch (\Exception $e) {
$message = $this
->t('Cannot extract Flipping Book file: @message', [
'@message' => $e
->getMessage(),
]);
$this
->messenger()
->addError($message);
return;
}
$this->entity
->set('directory', str_replace($this
->getUploadLocation() . '/', '', $this->destination));
return;
}
$storage = $this->entityTypeManager
->getStorage('flipping_book');
$orig = $storage
->load($this->entity
->id());
if (!empty($orig
->get('file')
->getValue()) && empty($this->entity
->get('file')
->getValue())) {
$this
->deleteFlippingBook($orig);
}
}
protected function deleteFlippingBook(FlippingBook $flippingBook) {
$this->flippingBook
->deleteArchive($flippingBook);
$data = $flippingBook
->get('file')
->getValue();
$file = $this
->getFileStorage()
->load($data[0]['target_id']);
$file
->delete();
}
protected function prepareExport(FormStateInterface $form_state) {
$file = reset($form_state
->getValue('file'));
$file = $this
->getFileStorage()
->load($file['fids'][0]);
$file
->setPermanent();
$file
->save();
$info = $this->flippingBook
->prepareExportDirectory($file, $this
->getUploadLocation());
$this->filepath = $info['filepath'];
$this->destination = $info['destination'];
}
protected function getFileStorage() {
return $this->entityTypeManager
->getStorage('file');
}
}