Skip to content

Commit

Permalink
Merge pull request #92 from chialab/improve-metadata-helper
Browse files Browse the repository at this point in the history
Simplify poster load in metadata helper
  • Loading branch information
le0m authored Apr 30, 2024
2 parents d011265 + 06e5c8e commit 32e80fd
Showing 1 changed file with 37 additions and 24 deletions.
61 changes: 37 additions & 24 deletions src/View/Helper/MetadataHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,26 @@ public function getPublisher(ObjectEntity|null $object = null, Folder|null $publ
return null;
}

/**
* Get poster url.
* Use main object `poster` relation, fallback to publication `poster`.
*
* @param \BEdita\Core\Model\Entity\ObjectEntity|null The main object.
* @param \BEdita\Core\Model\Entity\Folder|null The publication folder.
* @return string|null
*/
public function getPoster(ObjectEntity|null $object = null, Folder|null $publication = null): string|null
{
if ($object !== null && $this->Poster->exists($object)) {
return $this->Poster->url($object, 'default');
}
if ($publication !== null && $this->Poster->exists($publication)) {
return $this->Poster->url($publication, 'default');
}

return null;
}

/**
* Get publication status of the main object.
*
Expand Down Expand Up @@ -184,12 +204,12 @@ public function metaDc(array $data = [], ObjectEntity|null $object = null, Folde
$output = '<link rel="schema.DC" href="http://purl.org/dc/elements/1.1/" />';
$defaults = [
'DC.format' => 'text/html',
'DC.title' => htmlspecialchars($this->getTitle($object, null) ?? $this->getTitle(null, $publication)),
'DC.description' => htmlspecialchars($this->getDescription($object, $publication)),
'DC.creator' => htmlspecialchars($this->getCreator($object, $publication)),
'DC.publisher' => htmlspecialchars($this->getPublisher($object, $publication)),
'DC.rights' => htmlspecialchars($this->getPublisher($object, $publication)),
'DC.license' => $publication?->license ?? null,
'DC.title' => htmlspecialchars($this->getTitle($object, null) ?? $this->getTitle(null, $publication) ?? ''),
'DC.description' => htmlspecialchars($this->getDescription($object, $publication) ?? ''),
'DC.creator' => htmlspecialchars($this->getCreator($object, $publication) ?? ''),
'DC.publisher' => htmlspecialchars($this->getPublisher($object, $publication) ?? ''),
'DC.rights' => htmlspecialchars($this->getPublisher($object, $publication) ?? ''),
'DC.license' => $object->license ?? $publication?->license ?? null,
];
if ($object !== null) {
$defaults = [
Expand All @@ -199,7 +219,6 @@ public function metaDc(array $data = [], ObjectEntity|null $object = null, Folde
'DC.type' => $object->type,
'DC.identifier' => $object->uname,
'DC.language' => $object->lang ?? I18n::getLocale(),
'DC.license' => $object->license ?? null,
] + $defaults;
}

Expand Down Expand Up @@ -229,16 +248,15 @@ public function metaOg(array $data = [], ObjectEntity|null $object = null, Folde
$output = '';
$defaults = [
'og:url' => Router::url(null, true),
'og:title' => htmlspecialchars($this->getTitle($object, null) ?? $this->getTitle(null, $publication)),
'og:description' => htmlspecialchars($this->getDescription($object, $publication)),
'og:image' => $publication && $this->Poster->exists($publication) ? $this->Poster->url($publication, 'default') : null,
'og:site_name' => htmlspecialchars($this->getPublisher(null, $publication)),
'og:title' => htmlspecialchars($this->getTitle($object, null) ?? $this->getTitle(null, $publication) ?? ''),
'og:description' => htmlspecialchars($this->getDescription($object, $publication) ?? ''),
'og:image' => $this->getPoster($object, $publication),
'og:site_name' => htmlspecialchars($this->getPublisher(null, $publication) ?? ''),
];
if ($object !== null) {
$defaults = [
'og:type' => $object->type,
'og:updated_time' => $object->modified,
'og:image' => $object && $this->Poster->exists($object) ? $this->Poster->url($object, 'default') : null,
] + $defaults;
}

Expand All @@ -264,22 +282,17 @@ public function metaTwitter(array $data = [], ObjectEntity|null $object = null,
{
$object = $object ?? $this->getObject();
$publication = $publication ?? $this->getPublication();
$poster = $this->getPoster($object, $publication);

$output = '';
$defaults = [
'twitter:title' => htmlspecialchars($this->getTitle($object, null) ?? $this->getTitle(null, $publication)),
'twitter:description' => htmlspecialchars($this->getDescription($object, $publication)),
'twitter:site' => htmlspecialchars($this->getPublisher(null, $publication)),
'twitter:creator' => htmlspecialchars($this->getCreator($object, $publication)),
'twitter:card' => $publication && $this->Poster->exists($publication) ? 'summary_large_image' : null,
'twitter:image' => $publication && $this->Poster->exists($publication) ? $this->Poster->url($publication, 'default') : null,
'twitter:title' => htmlspecialchars($this->getTitle($object, null) ?? $this->getTitle(null, $publication) ?? ''),
'twitter:description' => htmlspecialchars($this->getDescription($object, $publication) ?? ''),
'twitter:site' => htmlspecialchars($this->getPublisher(null, $publication) ?? ''),
'twitter:creator' => htmlspecialchars($this->getCreator($object, $publication) ?? ''),
'twitter:card' => $poster ? 'summary_large_image' : null,
'twitter:image' => $poster,
];
if ($object !== null) {
$defaults = [
'twitter:card' => $object && $this->Poster->exists($object) ? 'summary_large_image' : null,
'twitter:image' => $object && $this->Poster->exists($object) ? $this->Poster->url($object, 'default') : null,
] + $defaults;
}

$data = $data + $defaults;
foreach ($data as $key => $value) {
Expand Down

0 comments on commit 32e80fd

Please sign in to comment.