Skip to content

Commit

Permalink
Add meta tags
Browse files Browse the repository at this point in the history
A new class is added which will add meta tags for dates and events.
The class has an interface which allows it to be replaced via DI to
alter behaviour.

Refactor import regarding data handler. We now also need to add a new
column "keywords". We use the new DataHandler approach.
But that approach only covered relations so far. We therefore refactor
that area to be more generic and use that one for new keywords column.

Relates: #10642
  • Loading branch information
DanielSiepmann committed Nov 29, 2023
1 parent 8851a4e commit d720814
Show file tree
Hide file tree
Showing 17 changed files with 446 additions and 61 deletions.
5 changes: 4 additions & 1 deletion Classes/Controller/DateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use WerkraumMedia\Events\Domain\Repository\RegionRepository;
use WerkraumMedia\Events\Events\Controller\DateListVariables;
use WerkraumMedia\Events\Events\Controller\DateSearchVariables;
use WerkraumMedia\Events\Frontend\MetaInformation\DateMetaInformationInterface;
use WerkraumMedia\Events\Pagination\Factory;
use WerkraumMedia\Events\Service\DataProcessingForModels;

Expand All @@ -28,7 +29,8 @@ public function __construct(
private readonly CategoryRepository $categoryRepository,
private readonly Factory $paginationFactory,
private readonly DataProcessingForModels $dataProcessing,
private readonly ExtensionService $extensionService
private readonly ExtensionService $extensionService,
private readonly DateMetaInformationInterface $metaInformationService
) {
}

Expand Down Expand Up @@ -110,6 +112,7 @@ public function showAction(Date $date): ResponseInterface
$this->trigger404('No event found for requested date.');
}

$this->metaInformationService->setDate($date);
$this->view->assign('date', $date);
return $this->htmlResponse();
}
Expand Down
10 changes: 8 additions & 2 deletions Classes/Domain/Model/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ class Date extends AbstractEntity

protected ?Date $originalDate = null;

protected Event $event;
/**
* Can not be null in theory.
* But editors might disable an event.
* The date might still be available by TYPO3 than, but without event.
* This needs to be handled properly by consuming code for now.
*/
protected ?Event $event;

protected string $canceledLink = '';

Expand Down Expand Up @@ -65,7 +71,7 @@ public function getEndsOnSameDay(): bool
return $end && $this->getStart()->format('Y-m-d') === $end->format('Y-m-d');
}

public function getEvent(): Event
public function getEvent(): ?Event
{
return $this->event;
}
Expand Down
7 changes: 7 additions & 0 deletions Classes/Domain/Model/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class Event extends AbstractEntity
*/
protected ObjectStorage $features;

protected string $keywords;

/**
* @var ObjectStorage<Partner>
*/
Expand Down Expand Up @@ -370,6 +372,11 @@ public function getFeatures(): array
return $this->getSortedCategory($this->features);
}

public function getKeywords(): string
{
return $this->keywords;
}

public function setLanguageUid(int $languageUid): void
{
$this->_languageUid = $languageUid;
Expand Down
31 changes: 31 additions & 0 deletions Classes/Frontend/MetaInformation/DateMetaInformationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2023 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

namespace WerkraumMedia\Events\Frontend\MetaInformation;

use WerkraumMedia\Events\Domain\Model\Date;

interface DateMetaInformationInterface
{
public function setDate(Date $date): void;
}
73 changes: 73 additions & 0 deletions Classes/Frontend/MetaInformation/DateMetaInformationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2023 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

namespace WerkraumMedia\Events\Frontend\MetaInformation;

use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
use WerkraumMedia\Events\Domain\Model\Date;


/**
* TYPO3 has many different APIs to set meta information like: Page Title, Meta Tags, OpenGraph Tags, etc.
* Those are combined here for Date detail view.
* That way there is a single place to connect the details to TYPO3 APIs.
*/
final class DateMetaInformationService implements DateMetaInformationInterface
{
public function __construct(
private readonly MetaTagManagerRegistry $metaTagManagerRegistry
) {
}

public function setDate(Date $date): void
{
$this->setDescription($date);
$this->setKeywords($date);
}

private function setDescription(Date $date): void
{
$description = $date->getEvent()?->getTeaser() ?? '';
if ($description === '') {
return;
}

$this->metaTagManagerRegistry
->getManagerForProperty('description')
->addProperty('description', $description)
;
}

private function setKeywords(Date $date): void
{
$keywords = $date->getEvent()?->getKeywords() ?? '';
if ($keywords === '') {
return;
}

$this->metaTagManagerRegistry
->getManagerForProperty('keywords')
->addProperty('keywords', $keywords)
;
}
}
33 changes: 19 additions & 14 deletions Classes/Service/DestinationDataImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,21 @@ public function processData(array $data): int
$this->persistenceManager->persistAll();

// Apply changes via DataHandler (The new way)
$this->logger->info('Apply changes via DataHandler');
if ($event['categories'] ?? false) {
$this->setCategories($event['categories']);
}
if ($event['features']) {
$this->setFeatures($event['features']);
$eventUid = $this->tmpCurrentEvent->getUid();
if (is_numeric($eventUid) === false) {
throw new Exception('Could not persist and fetch uid of event.', 1701244570);
}

$this->logger->info('Apply changes via DataHandler');
$this->dataHandler->updateEvent(
$eventUid,
[
new Assignment('keywords', implode(', ', $event['keywords'] ?? [])),
$this->getCategories($event['categories'] ?? []),
$this->getFeatures($event['features'] ?? []),
]
);

$this->logger->info('Update slugs');
$this->slugger->update('tx_events_domain_model_event');
$this->slugger->update('tx_events_domain_model_date');
Expand All @@ -202,7 +209,7 @@ public function processData(array $data): int
return 0;
}

private function setCategories(array $categories): void
private function getCategories(array $categories): Assignment
{
$categories = $this->categoriesAssignment->getCategories(new CategoryImport(
$this->import->getCategoryParent(),
Expand All @@ -216,14 +223,13 @@ private function setCategories(array $categories): void
);
$this->eventDispatcher->dispatch($event);

$this->dataHandler->storeAssignments(new Assignment(
$this->tmpCurrentEvent->getUid(),
return Assignment::createFromDomainObjects(
'categories',
$event->getCategories()->toArray()
));
);
}

private function setFeatures(array $features): void
private function getFeatures(array $features): Assignment
{
$features = $this->categoriesAssignment->getCategories(new CategoryImport(
$this->import->getFeaturesParent(),
Expand All @@ -232,11 +238,10 @@ private function setFeatures(array $features): void
true
));

$this->dataHandler->storeAssignments(new Assignment(
$this->tmpCurrentEvent->getUid(),
return Assignment::createFromDomainObjects(
'features',
$features->toArray()
));
);
}

private function setDates(
Expand Down
26 changes: 14 additions & 12 deletions Classes/Service/DestinationDataImportService/DataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use TYPO3\CMS\Core\DataHandling\DataHandler as Typo3DataHandler;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use WerkraumMedia\Events\Domain\Model\Event;
use WerkraumMedia\Events\Service\DestinationDataImportService\DataHandler\Assignment;

final class DataHandler
Expand All @@ -39,25 +40,26 @@ public function __construct(
$this->logger = $logManager->getLogger(self::class);
}

public function storeAssignments(
Assignment $assignment
/**
* @param Assignment[] $assignments
*/
public function updateEvent(
int $eventUid,
array $assignments
): void {
$data = [
'tx_events_domain_model_event' => [
$assignment->getUid() => [
$assignment->getColumnName() => implode(',', $assignment->getUids()),
],
],
];
$data = ['tx_events_domain_model_event' => [$eventUid => []]];
foreach ($assignments as $assignment) {
$data['tx_events_domain_model_event'][$eventUid][$assignment->getColumnName()] = $assignment->getValue();
}

$this->logger->debug('Import assignment.', $data);
$this->logger->debug('Update event data.', $data);
$dataHandler = GeneralUtility::makeInstance(Typo3DataHandler::class);
$dataHandler->start($data, []);
$dataHandler->process_datamap();

if ($dataHandler->errorLog !== []) {
$this->logger->error('Error during import of assignments.', [
'assignment' => $assignment,
$this->logger->error('Error during update of event data.', [
'assignments' => $assignments,
'errors' => $dataHandler->errorLog,
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,40 @@

final class Assignment
{
private readonly int $uid;

/**
* @var int[]
*/
private readonly array $uids;

/**
* @param AbstractDomainObject[] $assignments
*/
public function __construct(
?int $uid,
private readonly string $columnName,
array $assignments
private readonly string $value,
) {
if (is_int($uid) === false) {
throw new InvalidArgumentException('Only integer allowed as uid, need a persisted entity.', 1699352008);
}

$this->uid = $uid;
$this->uids = array_map(static function (AbstractDomainObject $model): int {
$uid = $model->getUid();
if (is_int($uid) === false) {
throw new InvalidArgumentException('Only object with uid supported.', 1698936965);
}
return $uid;
}, $assignments);
}

public function getUid(): int
public function getColumnName(): string
{
return $this->uid;
return $this->columnName;
}

public function getColumnName(): string
public function getValue(): string
{
return $this->columnName;
return $this->value;
}

/**
* @return int[]
* @param AbstractDomainObject[] $objects
*/
public function getUids(): array
{
return $this->uids;
public static function createFromDomainObjects(
string $columnName,
array $objects
): self {
$uids = array_map(static function (AbstractDomainObject $model): int {
$uid = $model->getUid();
if (is_int($uid) === false) {
throw new InvalidArgumentException('Only object with uid supported.', 1698936965);
}
return $uid;
}, $objects);

return new self(
$columnName,
implode(',', $uids)
);
}
}
10 changes: 10 additions & 0 deletions Configuration/TCA/tx_events_domain_model_event.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
partner,
categories,
features,
keywords,
references_events,
pages,
--div--;' . $l10nPath . ':tx_events_domain_model_event.tabs.media,
Expand Down Expand Up @@ -357,6 +358,15 @@
'multiple' => true,
],
],
'keywords' => [
'exclude' => true,
'label' => 'Keywords',
'config' => [
'type' => 'text',
'cols' => 40,
'rows' => 3,
],
],

'dates' => [
'exclude' => true,
Expand Down
Loading

0 comments on commit d720814

Please sign in to comment.