Skip to content

Commit

Permalink
removed template hardcodings
Browse files Browse the repository at this point in the history
  • Loading branch information
subhojit777 committed Feb 28, 2019
1 parent 0ff27d2 commit 0b27e33
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 72 deletions.
77 changes: 16 additions & 61 deletions src/Repec.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

/**
Expand Down Expand Up @@ -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}
*/
Expand Down
11 changes: 0 additions & 11 deletions src/RepecInterface.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit 0b27e33

Please sign in to comment.