Skip to content

Commit

Permalink
Add meta tags (#49)
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 Jul 23, 2024
1 parent 275dc3f commit 61144da
Show file tree
Hide file tree
Showing 24 changed files with 706 additions and 57 deletions.
11 changes: 10 additions & 1 deletion Classes/Controller/DateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Wrm\Events\Domain\Repository\RegionRepository;
use Wrm\Events\Events\Controller\DateListVariables;
use Wrm\Events\Events\Controller\DateSearchVariables;
use Wrm\Events\Frontend\MetaInformation\DateMetaInformationInterface;
use Wrm\Events\Pagination\Factory;
use Wrm\Events\Service\DataProcessingForModels;

Expand Down Expand Up @@ -58,6 +59,11 @@ class DateController extends AbstractController
*/
protected $extensionService;

/**
* @var DateMetaInformationInterface
*/
protected $metaInformationService;

public function __construct(
DateDemandFactory $demandFactory,
DateRepository $dateRepository,
Expand All @@ -66,7 +72,8 @@ public function __construct(
Factory $paginationFactory,
DataProcessingForModels $dataProcessing,
EventDispatcher $eventDispatcher,
ExtensionService $extensionService
ExtensionService $extensionService,
DateMetaInformationInterface $metaInformationService
) {
$this->demandFactory = $demandFactory;
$this->dateRepository = $dateRepository;
Expand All @@ -76,6 +83,7 @@ public function __construct(
$this->dataProcessing = $dataProcessing;
$this->eventDispatcher = $eventDispatcher;
$this->extensionService = $extensionService;
$this->metaInformationService = $metaInformationService;
}

protected function initializeAction(): void
Expand Down Expand Up @@ -163,6 +171,7 @@ public function showAction(Date $date): void
$this->trigger404('No event found for requested date.');
}

$this->metaInformationService->setDate($date);
$this->view->assign('date', $date);
}

Expand Down
11 changes: 10 additions & 1 deletion Classes/Controller/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Wrm\Events\Controller;

use TYPO3\CMS\Extbase\Annotation as Extbase;
use Wrm\Events\Frontend\MetaInformation\EventMetaInformationInterface;
use Wrm\Events\Domain\Model\Dto\EventDemandFactory;
use Wrm\Events\Domain\Model\Event;
use Wrm\Events\Domain\Repository\EventRepository;
Expand All @@ -25,14 +26,21 @@ class EventController extends AbstractController
*/
protected $demandFactory;

/**
* @var EventMetaInformationInterface
*/
protected $metaInformationService;

public function __construct(
EventRepository $eventRepository,
DataProcessingForModels $dataProcessing,
EventDemandFactory $demandFactory
EventDemandFactory $demandFactory,
EventMetaInformationInterface $metaInformationService
) {
$this->eventRepository = $eventRepository;
$this->dataProcessing = $dataProcessing;
$this->demandFactory = $demandFactory;
$this->metaInformationService = $metaInformationService;
}

protected function initializeAction(): void
Expand All @@ -54,6 +62,7 @@ public function listAction(): void
*/
public function showAction(Event $event): void
{
$this->metaInformationService->setEvent($event);
$this->view->assign('event', $event);
}

Expand Down
10 changes: 10 additions & 0 deletions Classes/Domain/Model/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ class Event extends AbstractEntity
*/
protected $features;

/**
* @var string
*/
protected $keywords = '';

/**
* @var ObjectStorage<Partner>
*/
Expand Down Expand Up @@ -437,6 +442,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 Wrm\Events\Frontend\MetaInformation;

use Wrm\Events\Domain\Model\Date;

interface DateMetaInformationInterface
{
public function setDate(Date $date): void;
}
84 changes: 84 additions & 0 deletions Classes/Frontend/MetaInformation/DateMetaInformationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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 Wrm\Events\Frontend\MetaInformation;

use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
use Wrm\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
{
/**
* @var MetaTagManagerRegistry
*/
private $metaTagManagerRegistry;

public function __construct(
MetaTagManagerRegistry $metaTagManagerRegistry
) {
$this->metaTagManagerRegistry = $metaTagManagerRegistry;
}

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

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

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

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

$this->metaTagManagerRegistry
->getManagerForProperty('keywords')
->addProperty('keywords', $keywords)
;
}
}
31 changes: 31 additions & 0 deletions Classes/Frontend/MetaInformation/EventMetaInformationInterface.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 Wrm\Events\Frontend\MetaInformation;

use Wrm\Events\Domain\Model\Event;

interface EventMetaInformationInterface
{
public function setEvent(Event $event): void;
}
78 changes: 78 additions & 0 deletions Classes/Frontend/MetaInformation/EventMetaInformationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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 Wrm\Events\Frontend\MetaInformation;

use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
use Wrm\Events\Domain\Model\Event;

/**
* TYPO3 has many different APIs to set meta information like: Page Title, Meta Tags, OpenGraph Tags, etc.
* Those are combined here for Event detail view.
* That way there is a single place to connect the details to TYPO3 APIs.
*/
final class EventMetaInformationService implements EventMetaInformationInterface
{
/**
* @var MetaTagManagerRegistry
*/
private $metaTagManagerRegistry;

public function __construct(
MetaTagManagerRegistry $metaTagManagerRegistry
) {
$this->metaTagManagerRegistry = $metaTagManagerRegistry;
}

public function setEvent(Event $event): void
{
$this->setDescription($event);
$this->setKeywords($event);
}

private function setDescription(Event $event): void
{
$description = $event->getTeaser();
if ($description === '') {
return;
}

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

private function setKeywords(Event $event): void
{
$keywords = $event->getKeywords();
if ($keywords === '') {
return;
}

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

0 comments on commit 61144da

Please sign in to comment.