SEO MetaData Middleware
Sometimes an extractor may need some more tools to create meta data. For example, the Schema Bundle allows you to use the fancy graph builder while creating schema blocks!
Adding Middleware
# config/services.yaml
services:
App\Seo\Middleware\MyCustomAdapter:
tags:
- { name: seo.meta_data.middleware.adapter, identifier: my_custom_adapter }
<?php
namespace App\Seo\Middleware;
use OpenDxp\Bundle\AdvancedSeoBundle\Middleware\MiddlewareAdapterInterface;
use OpenDxp\Bundle\AdvancedSeoBundle\Model\SeoMetaDataInterface;
class MyCustomAdapter implements MiddlewareAdapterInterface
{
protected string $string;
public function boot(): void
{
$this->string = 'Important string!';
}
public function getTaskArguments(): array
{
return [$this->string];
}
public function onFinish(SeoMetaDataInterface $seoMetadata): void
{
// nothing to do by default
// in some extended usages you may want to finalize and add some data to the SeoMetaData object after all extractors have been dispatched.
}
}
Usage
In your extractor you could use it like this:
<?php
namespace App\MetaData\Extractor;
use OpenDxp\Bundle\AdvancedSeoBundle\MetaData\Extractor\ExtractorInterface;
use OpenDxp\Bundle\AdvancedSeoBundle\Model\SeoMetaDataInterface;
class MyCustomExtractor implements ExtractorInterface
{
public function supports(mixed $element): bool
{
return $element instanceof MyClass;
}
public function updateMetaData(mixed $element, ?string $locale, SeoMetaDataInterface $seoMetadata): void
{
$middleware = $seoMetadata->getMiddleware('my_custom_adapter');
$middleware->addTask(static function (SeoMetaDataInterface $seoMetadata, string $string) {
$seoMetadata->setTitle($string); // output: "Important string"
});
}
}