From 0b27e337bbb8351c67aef47bee5a757e9e0d8f8c Mon Sep 17 00:00:00 2001 From: Subhojit Paul Date: Thu, 28 Feb 2019 17:48:10 +0530 Subject: [PATCH] removed template hardcodings --- src/Repec.php | 77 +++++++++--------------------------------- src/RepecInterface.php | 11 ------ 2 files changed, 16 insertions(+), 72 deletions(-) diff --git a/src/Repec.php b/src/Repec.php index 15e5d5a..4a7d9e0 100644 --- a/src/Repec.php +++ b/src/Repec.php @@ -248,40 +248,6 @@ public function getSeriesTemplate() { ]; } - /** - * {@inheritdoc} - */ - public function getPaperTemplate(ContentEntityInterface $entity) { - $result = [ - [ - 'attribute' => 'Template-Type', - 'value' => 'ReDIF-Paper 1.0', - ], - [ - 'attribute' => 'Title', - 'value' => $entity->label(), - ], - [ - 'attribute' => 'Number', - // Entity id cannot be used here as there could be - // probably several entity types in a further release. - 'value' => $entity->uuid(), - ], - [ - 'attribute' => 'Handle', - // @todo review unicity of node id for a shared series within several entity types. - 'value' => 'RePEc:' . $this->settings->get('archive_code') . ':wpaper:' . $entity->id(), - ], - ]; - $templateFields = $this->getTemplateFields(RepecInterface::SERIES_WORKING_PAPER); - foreach ($templateFields as $attributeKey => $attributeName) { - foreach ($this->getFieldValues($entity, $attributeKey, $attributeName->render()) as $fieldValue) { - $result[] = $fieldValue; - } - } - return $result; - } - /** * Gets the value of a field based on a RePEC attribute. * @@ -568,11 +534,13 @@ public function getTemplateFields($templateType) { * {@inheritdoc} */ public function getEntityTemplate(ContentEntityInterface $entity) { + /** @var string $series_type */ + $series_type = $this->getEntityBundleSettings('serie_type', $entity->getEntityTypeId(), $entity->bundle()); /** @var \Drupal\repec\Series\Base $template_class */ - $template_class = $this->templateFactory->create($this->getEntityBundleSettings('serie_type', $entity->getEntityTypeId(), $entity->bundle()), $entity); + $template_class = $this->templateFactory->create($series_type, $entity); $template = $template_class->getDefault(); - $templateFields = $this->getTemplateFields(RepecInterface::SERIES_WORKING_PAPER); + $templateFields = $this->getTemplateFields($series_type); foreach ($templateFields as $attributeKey => $attributeName) { foreach ($this->getFieldValues($entity, $attributeKey, $attributeName->render()) as $fieldValue) { $template[] = $fieldValue; @@ -620,10 +588,18 @@ public function createTemplate(array $template, $templateType) { * {@inheritdoc} */ public function createEntityTemplate(ContentEntityInterface $entity, $templateType) { - // @todo based on the bundle configuration, select series - // via a factory to get the right template. - // Currently limiting it to the Working Paper series. - $this->createPaperTemplate($entity); + /** @var array $template */ + $template = $this->getEntityTemplate($entity); + + /** @var \Drupal\repec\Series\Base $template_class */ + $template_class = $this->templateFactory->create($this->getEntityBundleSettings('serie_type', $entity->getEntityTypeId(), $entity->bundle()), $entity); + + try { + $template_class->create($template); + } + catch (\Exception $e) { + $this->messenger->addError($e->getMessage()); + } } /** @@ -652,27 +628,6 @@ public function deleteEntityTemplate(ContentEntityInterface $entity) { } } - /** - * Maps the series fields with the node fields to create the template file. - * - * @param \Drupal\Core\Entity\ContentEntityInterface $entity - * The entity that is the subject of the mapping. - */ - private function createPaperTemplate(ContentEntityInterface $entity) { - /** @var array $template */ - $template = $this->getEntityTemplate($entity); - - /** @var \Drupal\repec\Series\Base $template_class */ - $template_class = $this->templateFactory->create($this->getEntityBundleSettings('serie_type', $entity->getEntityTypeId(), $entity->bundle()), $entity); - - try { - $template_class->create($template); - } - catch (\Exception $e) { - $this->messenger->addError($e->getMessage()); - } - } - /** * {@inheritdoc} */ diff --git a/src/RepecInterface.php b/src/RepecInterface.php index 69509ba..26001ee 100644 --- a/src/RepecInterface.php +++ b/src/RepecInterface.php @@ -53,17 +53,6 @@ public function getArchiveTemplate(); */ public function getSeriesTemplate(); - /** - * Maps a template to an entity based on its bundle configuration. - * - * @param \Drupal\Core\Entity\ContentEntityInterface $entity - * The entity that is the subject of the template. - * - * @return array - * RDF template. - */ - public function getPaperTemplate(ContentEntityInterface $entity); - /** * Maps a template to an entity based on its bundle configuration. *