Skip to content

Commit

Permalink
implement seo panel editor, see #54
Browse files Browse the repository at this point in the history
  • Loading branch information
solverat committed Aug 10, 2023
1 parent 0a250a1 commit a27d15c
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 3 deletions.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions config/services/third_party/pimcore_seo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:

_defaults:
autowire: true
autoconfigure: true
public: false

SeoBundle\EventListener\Admin\SeoDocumentEditorListener:
tags:
- { name: kernel.event_subscriber }
File renamed without changes.
11 changes: 8 additions & 3 deletions src/DependencyInjection/SeoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function prepend(ContainerBuilder $container): void
$enabledThirdPartyConfigs = [];

$xliffBundleEnabled = $container->hasExtension('pimcore_xliff');
$pimcoreSeoBundleEnabled = $container->hasExtension('pimcore_seo');
$newsBundleEnabled = $container->hasExtension('news');
$coreShopSeoBundleEnabled = $container->hasExtension('core_shop_seo');

Expand All @@ -59,16 +60,20 @@ public function prepend(ContainerBuilder $container): void
}

if ($coreShopSeoBundleEnabled && ($thirdPartyConfig['coreshop']['disable_default_extractors'] ?? false) === false) {
$enabledThirdPartyConfigs['core_shop_seo'] = 'services/extractors/coreshop.yaml';
$enabledThirdPartyConfigs['core_shop_seo'] = 'services/third_party/coreshop.yaml';
}

if ($newsBundleEnabled && ($thirdPartyConfig['news']['disable_default_extractors'] ?? false) === false) {
$enabledThirdPartyConfigs['dachcom_news'] = 'services/extractors/news.yaml';
$enabledThirdPartyConfigs['dachcom_news'] = 'services/third_party/news.yaml';
}
}

if ($xliffBundleEnabled) {
$enabledThirdPartyConfigs['pimcore_xliff'] = 'services/xliff_bundle/services.yaml';
$enabledThirdPartyConfigs['pimcore_xliff'] = 'services/third_party/pimcore_xliff.yaml';
}

if ($pimcoreSeoBundleEnabled) {
$enabledThirdPartyConfigs['pimcore_seo'] = 'services/third_party/pimcore_seo.yaml';
}

foreach ($enabledThirdPartyConfigs as $enabledThirdPartyConfig) {
Expand Down
103 changes: 103 additions & 0 deletions src/EventListener/Admin/SeoDocumentEditorListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace SeoBundle\EventListener\Admin;

use Pimcore\Bundle\AdminBundle\Event\AdminEvents;
use Pimcore\Bundle\SeoBundle\Controller\Document\DocumentController;
use Pimcore\Event\DocumentEvents;
use Pimcore\Event\Model\DocumentEvent;
use SeoBundle\Manager\ElementMetaDataManagerInterface;
use SeoBundle\MetaData\MetaDataProviderInterface;
use SeoBundle\Model\ElementMetaData;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class SeoDocumentEditorListener implements EventSubscriberInterface
{
public function __construct(
protected RequestStack $requestStack,
protected ElementMetaDataManagerInterface $elementMetaDataManager,
protected MetaDataProviderInterface $metaDataProvider
) {
}

public static function getSubscribedEvents(): array
{
return [
AdminEvents::DOCUMENT_LIST_AFTER_LIST_LOAD => 'adjustData',
DocumentEvents::POST_UPDATE => 'updateData',
];
}

public function adjustData(GenericEvent $event): void
{
if (!$event->getSubject() instanceof DocumentController) {
return;
}

$list = $event->getArgument('list');

foreach ($list['data'] as $listIndex => $item) {

if ($item['type'] !== 'page') {
continue;
}

$metaData = array_values(
array_filter(
$this->elementMetaDataManager->getElementData('document', $item['id']),
static function (ElementMetaData $integratorData) {
return $integratorData->getIntegrator() === 'title_description';
}
)
);

if (count($metaData) === 0) {
continue;
}

/** @var ElementMetaData $titleDescriptionIntegrator */
$titleDescriptionIntegrator = $metaData[0];
$titleDescriptionIntegratorData = $titleDescriptionIntegrator->getData();

$list['data'][$listIndex]['title'] = $titleDescriptionIntegratorData['title'] ?? null;
$list['data'][$listIndex]['description'] = $titleDescriptionIntegratorData['description'] ?? null;
}

$event->setArgument('list', $list);
}

public function updateData(DocumentEvent $event): void
{
$request = $this->requestStack->getMainRequest();

if (!$request instanceof Request) {
return;
}

$document = $event->getDocument();
$attributes = $request->request->all();
$hasTitleAttribute = $request->request->has('title');
$hasDescriptionAttribute = $request->request->has('description');

// crazy. but there is no other way to determinate, that we're updating via seo panel context...
if (!$hasTitleAttribute || !$hasDescriptionAttribute || count($attributes) !== 4) {
return;
}

$integratorData = [
'title' => $request->request->get('title'),
'description' => $request->request->get('description')
];

$this->elementMetaDataManager->saveElementData(
'document',
$document->getId(),
'title_description',
$integratorData
);

}
}

0 comments on commit a27d15c

Please sign in to comment.