jamiemax
jamiemax

Reputation: 189

How to make Sulu page no-index by default

I am using Sulu 2.1 with Symfony 4.4

I want to make certain page types have the no-index option on by default when they are first created. Currently no-index is off by default.

enter image description here

Is it possible to configure this in either the webspace or page template XML?

Upvotes: 0

Views: 220

Answers (1)

Alexander Schranz
Alexander Schranz

Reputation: 2450

Its not possible to set this on webspace but you can create your custom EventSubscriber to set this value on persist, an example subscriber could look like the following:

namespace App\Subscriber\Document;

use Sulu\Component\DocumentManager\Event\PersistEvent;
use Sulu\Component\DocumentManager\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Sulu\Bundle\PageBundle\Document\PageDocument;

class NoIndexSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            Events::PERSIST => 'handlePersist',
        ];
    }


    public function handlePersist(PersistEvent $event)
    {
        $document = $event->getDocument();

        if (!$document instanceof PageDocument) {
            return;
        }

        $extensionData = $document->getExtensionsData()->toArray();
        if (array_key_exists('noIndex', $extensionData['seo'])) {
            // do nothing when extension data was set manually
            return;
        }

        $extensionData['seo']['noIndex'] = true;
    }
}

Its important that the service is tagged with sulu_document_manager.event_subscriber you can do this in the config/services.yamlthe following way:

    App\Subscriber\Document\:
        resource: '../src/Subscriber/Document'
        tags: ['sulu_document_manager.event_subscriber'}]

Make sure it is listed here:

bin/console sulu:document:subscriber:debug persist

Upvotes: 1

Related Questions