Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify poster load in metadata helper #92

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading