epub_reader_framework-2.0.0-alpha2/src/Entity/ReaderEntityPostsave.php
src/Entity/ReaderEntityPostsave.php
<?php
namespace Drupal\epub_reader_framework\Entity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\epub_reader_framework\Batch\ReaderChapterExtractorBatchProcessor;
use Drupal\epub_reader_framework\EpubReaderFrameworkHelpers;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Handle post save actions.
*/
class ReaderEntityPostsave {
use StringTranslationTrait;
/**
* The render entity cross reference service.
*
* @var \Drupal\epub_reader_framework\Entity\ReaderEntityCrossReference
*/
protected $readerEntityCrossReference;
/**
* The render entity cross reference service.
*
* @var \Drupal\epub_reader_framework\Entity\ReaderEntityChapterHeadingsSave
*/
protected $readerEntityChapterHeadingsSave;
/**
* Constructor.
*
* @param \Drupal\epub_reader_framework\Entity\ReaderEntityCrossReference $reader_entity_cross_reference
* The reader entity cross reference service.
* @param \Drupal\epub_reader_framework\Entity\ReaderEntityChapterHeadingsSave $reader_entity_chapter_headings_save
* The reader entity chapter headings save service.
*/
public function __construct(
ReaderEntityCrossReference $reader_entity_cross_reference,
ReaderEntityChapterHeadingsSave $reader_entity_chapter_headings_save
) {
$this->readerEntityCrossReference = $reader_entity_cross_reference;
$this->readerEntityChapterHeadingsSave = $reader_entity_chapter_headings_save;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('epub_reader_framework.reader_entity_cross_reference'),
$container->get('epub_reader_framework.reader_entity_chapter_headings_save')
);
}
/**
* Callback implementation of insert and update entity hooks.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being inserted or updated.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function entityPostsave(EntityInterface $entity) {
$route_name = \Drupal::routeMatch()->getRouteName();
// Only when this particular node type was being edited.
if ($entity->bundle() === 'reader_chapter') {
$this->chapterCrossReference($entity);
$this->chapterHeadingsSave($entity);
// Redirect to the publication if editing or adding a chapter.
if (in_array($route_name, ['entity.node.edit_form', 'node.add'])) {
$this->redirectToPublication($entity);
}
}
// Check if we still have not processed the epub.
if (
$entity instanceof NodeInterface
&& $entity->bundle() === 'reader_publication'
&& $entity->hasField('field_reader_file_processed')
&& !$entity->get('field_reader_file_processed')->value
&& EpubReaderFrameworkHelpers::getEpubFile($entity)
) {
EpubReaderFrameworkHelpers::startExtractionBatch($entity);
}
}
/**
* Maybe add a chapter cross-reference.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being inserted or updated.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function chapterCrossReference(EntityInterface $entity) {
/** @var \Drupal\node\NodeInterface $entity */
/** @var \Drupal\node\NodeInterface $reader_publication */
$reader_publication = $entity->field_reader_publication->entity;
/** @var \Drupal\epub_reader_framework\Entity\ReaderEntityCrossReference $reader_entity_cross_reference */
$reader_entity_cross_reference = $this->readerEntityCrossReference;
$reader_entity_cross_reference->ensureChapterIsCrossReferencedInPublication(
$reader_publication,
$entity
);
}
/**
* Save chapter headings.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being inserted or updated.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function chapterHeadingsSave(EntityInterface $entity) {
/** @var \Drupal\node\NodeInterface $entity */
/** @var \Drupal\node\NodeInterface $reader_publication */
$reader_publication = $entity->field_reader_publication->entity;
/** @var \Drupal\epub_reader_framework\Entity\ReaderEntityChapterHeadingsSave $reader_entity_chapter_headings_save */
$reader_entity_chapter_headings_save = $this->readerEntityChapterHeadingsSave;
$reader_entity_chapter_headings_save->saveHeadings(
$reader_publication,
$entity
);
}
/**
* Redirect to the publication.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being inserted or updated.
*/
protected function redirectToPublication(EntityInterface $entity) {
/** @var \Drupal\node\NodeInterface $entity */
/** @var \Drupal\node\NodeInterface $reader_publication */
$reader_publication_id = $entity->field_reader_publication->target_id;
// Redirect to adding a publication.
$url = Url::fromRoute('entity.node.edit_form', [
'node' => $reader_publication_id,
])->toString();
$response = new RedirectResponse($url);
$response->send();
}
}
