Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

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 SeoBundle\Middleware\MiddlewareAdapterInterface;
use SeoBundle\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 SeoBundle\MetaData\Extractor\ExtractorInterface;
use SeoBundle\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"
        });
    }
}