diff --git a/CHANGELOG.md b/CHANGELOG.md index 5650b88..696a201 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. - Removed: json-ld backport - Removed: @WebPage schema integration, because it is provided by Contao core - Removed: @BreadcrumbList schema integration, because it is provided by Contao core +- Removed: canonical links backport ## [1.14.2] - 2025-12-17 - Fixed: Relative image paths had to be absolute diff --git a/README.md b/README.md index 3b8714b..098ea34 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ This bundle enhances the handling of html `` section tags. It provides ser - Sets important meta tags like og:title, og:description, og:url and twitter:card out of the box - Allow setting open graph and twitter fallback image on root page - Allow setting twitter author per root page -- Backport canonical url option from contao 4.13 for contao 4.9+ ## Usage diff --git a/contao/languages/de/tl_page.php b/contao/languages/de/tl_page.php index 0922cfd..637f3cf 100644 --- a/contao/languages/de/tl_page.php +++ b/contao/languages/de/tl_page.php @@ -31,13 +31,3 @@ $lang['head_legend'] = 'Head-Bundle'; $lang['head_twitter_legend'] = 'Head-Bundle - Twitter'; $lang['schema_legend'] = 'Strukturierte Daten (Schema.org)'; - -// if (version_compare(VERSION, '4.13', '<')) { -// $lang['canonical_legend'] = 'Kanonische URL'; -// $lang['enableCanonical'][0] = 'rel="canonical" aktivieren'; -// $lang['enableCanonical'][1] = 'Der Website rel="canonical"-Tags hinzufügen.'; -// $lang['canonicalLink'][0] = 'Individuelle URL'; -// $lang['canonicalLink'][1] = 'Hier können Sie eine individuelle kanonische URL wie z. B. https://example.com/ setzen.'; -// $lang['canonicalKeepParams'][0] = 'Query-Parameter'; -// $lang['canonicalKeepParams'][1] = 'Standardmäßig entfernt Contao die Query-Parameter in der kanonischen URL. Hier können Sie eine kommagetrennte Liste von Query-Parametern hinzufügen, die erhalten bleiben sollen. Verwenden Sie "*" als Platzhalter.'; -// } diff --git a/contao/languages/en/tl_page.php b/contao/languages/en/tl_page.php index 1ee2bb2..6bc9735 100644 --- a/contao/languages/en/tl_page.php +++ b/contao/languages/en/tl_page.php @@ -31,13 +31,3 @@ $lang['head_legend'] = 'Head-Bundle'; $lang['head_twitter_legend'] = 'Head-Bundle - Twitter'; $lang['schema_legend'] = 'Structured data (Schema.org)'; - -// if (version_compare(VERSION, '4.13', '<')) { -// $lang['canonical_legend'] = 'Canonical URL'; -// $lang['enableCanonical'][0] = 'Enable rel="canonical"'; -// $lang['enableCanonical'][1] = 'Add rel="canonical" tags to the website.'; -// $lang['canonicalLink'][0] = 'Custom URL'; -// $lang['canonicalLink'][1] = 'Here you can set a custom canonical URL like https://example.com/.'; -// $lang['canonicalKeepParams'][0] = 'Query parameters'; -// $lang['canonicalKeepParams'][1] = 'By default, Contao strips the query parameters in the canonical URL. Here you can add a comma-separated list of query parameters to preserve. Use "*" as a wildcard.'; -// } diff --git a/src/DependencyInjection/HeimrichHannotHeadBundleExtension.php b/src/DependencyInjection/HeimrichHannotHeadBundleExtension.php index f705d23..a69efd2 100644 --- a/src/DependencyInjection/HeimrichHannotHeadBundleExtension.php +++ b/src/DependencyInjection/HeimrichHannotHeadBundleExtension.php @@ -8,8 +8,6 @@ namespace HeimrichHannot\HeadBundle\DependencyInjection; -use Contao\CoreBundle\EventListener\DataContainer\DisableCanonicalFieldsListener; -use HeimrichHannot\HeadBundle\EventListener\CanonicalListener; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\Extension; @@ -25,10 +23,6 @@ public function load(array $configs, ContainerBuilder $container): void $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config')); $loader->load('services.yaml'); - - if (class_exists(DisableCanonicalFieldsListener::class)) { - $container->removeDefinition(CanonicalListener::class); - } } public function getAlias(): string diff --git a/src/EventListener/CanonicalListener.php b/src/EventListener/CanonicalListener.php deleted file mode 100644 index bae1c02..0000000 --- a/src/EventListener/CanonicalListener.php +++ /dev/null @@ -1,180 +0,0 @@ -utils = $utils; - $this->headTagManager = $headTagManager; - $this->requestStack = $requestStack; - $this->insertTagParser = $insertTagParser; - } - - /** - * @Hook("loadDataContainer") - */ - public function addFields(string $table): void - { - if (PageModel::getTable() !== $table) { - return; - } - - $dca = &$GLOBALS['TL_DCA'][$table]; - - $dca['fields']['enableCanonical'] = [ - 'exclude' => true, - 'inputType' => 'checkbox', - 'default' => true, - 'eval' => [ - 'tl_class' => 'w50 m12', - ], - 'sql' => "char(1) NOT NULL default ''", - ]; - $dca['fields']['canonicalLink'] = [ - 'exclude' => true, - 'search' => true, - 'inputType' => 'text', - 'eval' => [ - 'rgxp' => 'url', - 'decodeEntities' => true, - 'maxlength' => 255, - 'dcaPicker' => true, - 'tl_class' => 'w50', - ], - 'sql' => "varchar(255) NOT NULL default ''", - ]; - $dca['fields']['canonicalKeepParams'] = [ - 'exclude' => true, - 'inputType' => 'text', - 'eval' => [ - 'decodeEntities' => true, - 'maxlength' => 255, - 'tl_class' => 'w50', - ], - 'sql' => "varchar(255) NOT NULL default ''", - ]; - - PaletteManipulator::create() - ->addLegend('canonical_legend', 'meta_legend', PaletteManipulator::POSITION_AFTER, true) - ->addField('canonicalLink', 'canonical_legend', PaletteManipulator::POSITION_APPEND) - ->addField('canonicalKeepParams', 'canonical_legend', PaletteManipulator::POSITION_APPEND) - ->applyToPalette('regular', 'tl_page'); - - PaletteManipulator::create() - ->addField('enableCanonical', 'adminEmail', PaletteManipulator::POSITION_BEFORE) - ->applyToPalette('root', 'tl_page') - ->applyToPalette('rootfallback', 'tl_page'); - } - - /** - * @Callback(table="tl_page", target="fields.canonicalLink.load") - * @Callback(table="tl_page", target="fields.canonicalKeepParams.load") - */ - public function disableCanonicalFieldsListener(string $value, DataContainer $dc): string - { - if (!$dc->id || ($pageModel = PageModel::findByPk($dc->id)) === null) { - return $value; - } - - if (($rootPageModel = $this->utils->request()->getCurrentRootPageModel($pageModel)) === null) { - return $value; - } - - if (!$rootPageModel->enableCanonical) { - $GLOBALS['TL_DCA'][$dc->table]['fields'][$dc->field]['eval']['disabled'] = true; - } - - return $value; - } - - /** - * @Hook("generatePage", priority=-9) - */ - public function addCanonicalIfNotSet(PageModel $pageModel, LayoutModel $layout, PageRegular $pageRegular): void - { - $rootPageModel = $this->utils->request()->getCurrentRootPageModel($pageModel); - $request = $this->requestStack->getCurrentRequest(); - - if (!$request || !$rootPageModel->enableCanonical || $this->headTagManager->getCanonical()) { - return; - } - - if ($pageModel->canonicalLink) { - $url = $this->insertTagParser->replace($pageModel->canonicalLink); - - $mainRequest = $this->requestStack->getMainRequest(); - - // Ensure absolute links - if (!\preg_match('@^https?://@', $url)) { - if (!$mainRequest) { - throw new \RuntimeException('The request stack did not contain a request'); - } - - $url = $request->getSchemeAndHttpHost() . $request->getBasePath() . '/' . $url; - } - - $this->headTagManager->setCanonical($url); - - return; - } - - $keepParams = []; - - if ($pageModel->canonicalKeepParams) { - $keepParams = array_map('trim', explode(',', $pageModel->canonicalKeepParams)); - } - - $params = []; - - foreach ($request->query->all() as $originalParam => $value) { - foreach ($keepParams as $param) { - $regex = sprintf('/^%s$/', str_replace('\*', '.*', preg_quote($param, '/'))); - - if (preg_match($regex, $originalParam)) { - $params[$originalParam] = $value; - } - } - } - - $request = Request::create( - $request->getSchemeAndHttpHost() . $request->getBaseUrl() . $request->getPathInfo(), - $request->getMethod(), - $params - ); - - $this->headTagManager->setCanonical($request->getUri()); - } -}