Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add canonical links to news and events #6215

Merged
merged 20 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion calendar-bundle/contao/dca/tl_calendar_events.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
'palettes' => array
(
'__selector__' => array('source', 'addTime', 'addImage', 'recurring', 'addEnclosure', 'overwriteMeta'),
'default' => '{title_legend},title,featured,alias,author;{date_legend},addTime,startDate,endDate;{source_legend},source,linkText;{meta_legend},pageTitle,robots,description,serpPreview;{details_legend},location,address,teaser;{image_legend},addImage;{recurring_legend},recurring;{enclosure_legend:hide},addEnclosure;{expert_legend:hide},cssClass;{publish_legend},published,start,stop',
'default' => '{title_legend},title,featured,alias,author;{date_legend},addTime,startDate,endDate;{source_legend},source,linkText,canonicalLink;{meta_legend},pageTitle,robots,description,serpPreview;{details_legend},location,address,teaser;{image_legend},addImage;{recurring_legend},recurring;{enclosure_legend:hide},addEnclosure;{expert_legend:hide},cssClass;{publish_legend},published,start,stop',
'internal' => '{title_legend},title,featured,alias,author;{date_legend},addTime,startDate,endDate;{source_legend},source,jumpTo,linkText;{details_legend},location,address,teaser;{image_legend},addImage;{recurring_legend},recurring;{enclosure_legend:hide},addEnclosure;{expert_legend:hide},cssClass;{publish_legend},published,start,stop',
'article' => '{title_legend},title,featured,alias,author;{date_legend},addTime,startDate,endDate;{source_legend},source,articleId,linkText;{details_legend},location,address,teaser;{image_legend},addImage;{recurring_legend},recurring;{enclosure_legend:hide},addEnclosure;{expert_legend:hide},cssClass;{publish_legend},published,start,stop',
'external' => '{title_legend},title,featured,alias,author;{date_legend},addTime,startDate,endDate;{source_legend},source,url,target,linkText;{details_legend},location,address,teaser;{image_legend},addImage;{recurring_legend},recurring;{enclosure_legend:hide},addEnclosure;{expert_legend:hide},cssClass;{publish_legend},published,start,stop'
Expand Down Expand Up @@ -258,6 +258,13 @@
'eval' => array('url_callback'=>array('tl_calendar_events', 'getSerpUrl'), 'title_tag_callback'=>array('tl_calendar_events', 'getTitleTag'), 'titleFields'=>array('pageTitle', 'title'), 'descriptionFields'=>array('description', 'teaser')),
'sql' => null
),
'canonicalLink' => array
(
'search' => true,
'inputType' => 'text',
'eval' => array('rgxp'=>'url', 'decodeEntities'=>true, 'maxlength'=>2048, 'dcaPicker'=>true, 'tl_class'=>'w50'),
'sql' => "varchar(2048) NOT NULL default ''"
),
'location' => array
(
'search' => true,
Expand Down
9 changes: 8 additions & 1 deletion calendar-bundle/contao/dca/tl_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// Add palettes to tl_module
$GLOBALS['TL_DCA']['tl_module']['palettes']['calendar'] = '{title_legend},name,headline,type;{config_legend},cal_calendar,cal_noSpan,cal_startDay,cal_featured;{redirect_legend},jumpTo;{template_legend:hide},cal_ctemplate,customTpl;{protected_legend:hide},protected;{expert_legend:hide},cssID';
$GLOBALS['TL_DCA']['tl_module']['palettes']['eventlist'] = '{title_legend},name,headline,type;{config_legend},cal_calendar,cal_noSpan,cal_format,cal_featured,cal_order,cal_readerModule,cal_limit,perPage,cal_ignoreDynamic,cal_hideRunning;{template_legend:hide},cal_template,customTpl;{image_legend:hide},imgSize;{protected_legend:hide},protected;{expert_legend:hide},cssID';
$GLOBALS['TL_DCA']['tl_module']['palettes']['eventreader'] = '{title_legend},name,headline,type;{config_legend},cal_calendar,cal_hideRunning,overviewPage,customLabel;{template_legend:hide},cal_template,customTpl;{image_legend},imgSize;{protected_legend:hide},protected;{expert_legend:hide},cssID';
$GLOBALS['TL_DCA']['tl_module']['palettes']['eventreader'] = '{title_legend},name,headline,type;{config_legend},cal_calendar,cal_hideRunning,cal_keepCanonical;{cal_overview_legend},overviewPage,customLabel;{template_legend:hide},cal_template,customTpl;{image_legend},imgSize;{protected_legend:hide},protected;{expert_legend:hide},cssID';
$GLOBALS['TL_DCA']['tl_module']['palettes']['eventmenu'] = '{title_legend},name,headline,type;{config_legend},cal_calendar,cal_noSpan,cal_format,cal_featured,cal_order,cal_showQuantity;{redirect_legend},jumpTo;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},cssID';
$GLOBALS['TL_DCA']['tl_module']['palettes']['eventmenucal_day'] = '{title_legend},name,headline,type;{config_legend},cal_calendar,cal_noSpan,cal_format,cal_featured,cal_startDay,cal_showQuantity;{redirect_legend},jumpTo;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},cssID';

Expand Down Expand Up @@ -135,6 +135,13 @@
'sql' => "varchar(16) COLLATE ascii_bin NOT NULL default 'all_items'"
);

$GLOBALS['TL_DCA']['tl_module']['fields']['cal_keepCanonical'] = array
(
'inputType' => 'checkbox',
'eval' => array('tl_class'=>'w50'),
'sql' => array('type' => 'boolean', 'default' => false)
);

/**
* Provide miscellaneous methods that are used by the data configuration array.
*
Expand Down
6 changes: 6 additions & 0 deletions calendar-bundle/contao/languages/en/tl_calendar_events.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
<trans-unit id="tl_calendar_events.description.1">
<source>Here you can add a custom meta description to overwrite the default page description.</source>
</trans-unit>
<trans-unit id="tl_calendar_events.canonicalLink.0">
<source>Canonical URL</source>
</trans-unit>
<trans-unit id="tl_calendar_events.canonicalLink.1">
<source>Here you can set a custom canonical URL like https://example.com/.</source>
</trans-unit>
<trans-unit id="tl_calendar_events.location.0">
<source>Location name</source>
</trans-unit>
Expand Down
9 changes: 9 additions & 0 deletions calendar-bundle/contao/languages/en/tl_module.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@
<trans-unit id="tl_module.cal_featured.1">
<source>Here you can choose how featured events are handled.</source>
</trans-unit>
<trans-unit id="tl_module.cal_keepCanonical.0">
<source>Use the current URL for canonical links</source>
</trans-unit>
<trans-unit id="tl_module.cal_keepCanonical.1">
<source>Keep the current URL instead of the configured reader page for canonical links.</source>
</trans-unit>
<trans-unit id="tl_module.cal_overview_legend">
<source>Overview page</source>
</trans-unit>
<trans-unit id="tl_module.cal_list">
<source>Event list</source>
</trans-unit>
Expand Down
4 changes: 4 additions & 0 deletions calendar-bundle/contao/models/CalendarEventsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @property string $pageTitle
* @property string $robots
* @property string|null $description
* @property string $canonicalLink
* @property string $location
* @property string $address
* @property string|null $teaser
Expand Down Expand Up @@ -79,6 +80,7 @@
* @method static CalendarEventsModel|null findOneByPageTitle($val, array $opt=array())
* @method static CalendarEventsModel|null findOneByRobots($val, array $opt=array())
* @method static CalendarEventsModel|null findOneByDescription($val, array $opt=array())
* @method static CalendarEventsModel|null findOneByCanonicalLink($val, array $opt=array())
* @method static CalendarEventsModel|null findOneByLocation($val, array $opt=array())
* @method static CalendarEventsModel|null findOneByAddress($val, array $opt=array())
* @method static CalendarEventsModel|null findOneByTeaser($val, array $opt=array())
Expand Down Expand Up @@ -122,6 +124,7 @@
* @method static Collection<CalendarEventsModel>|CalendarEventsModel[]|null findByPageTitle($val, array $opt=array())
* @method static Collection<CalendarEventsModel>|CalendarEventsModel[]|null findByRobots($val, array $opt=array())
* @method static Collection<CalendarEventsModel>|CalendarEventsModel[]|null findByDescription($val, array $opt=array())
* @method static Collection<CalendarEventsModel>|CalendarEventsModel[]|null findByCanonicalLink($val, array $opt=array())
* @method static Collection<CalendarEventsModel>|CalendarEventsModel[]|null findByLocation($val, array $opt=array())
* @method static Collection<CalendarEventsModel>|CalendarEventsModel[]|null findByAddress($val, array $opt=array())
* @method static Collection<CalendarEventsModel>|CalendarEventsModel[]|null findByTeaser($val, array $opt=array())
Expand Down Expand Up @@ -170,6 +173,7 @@
* @method static integer countByPageTitle($val, array $opt=array())
* @method static integer countByRobots($val, array $opt=array())
* @method static integer countByDescription($val, array $opt=array())
* @method static integer countByCanonicalLink($val, array $opt=array())
* @method static integer countByLocation($val, array $opt=array())
* @method static integer countByAddress($val, array $opt=array())
* @method static integer countByTeaser($val, array $opt=array())
Expand Down
22 changes: 22 additions & 0 deletions calendar-bundle/contao/modules/ModuleEventReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,28 @@ protected function compile()
{
$htmlHeadBag->setMetaRobots($objEvent->robots);
}

if ($objEvent->canonicalLink)
aschempp marked this conversation as resolved.
Show resolved Hide resolved
{
$url = System::getContainer()->get('contao.insert_tag.parser')->replaceInline($objEvent->canonicalLink);

// Ensure absolute links
if (!preg_match('#^https?://#', $url))
{
if (!$request = System::getContainer()->get('request_stack')->getCurrentRequest())
{
throw new \RuntimeException('The request stack did not contain a request');
}

$url = UrlUtil::makeAbsolute($url, $request->getUri());
}

$htmlHeadBag->setCanonicalUri($url);
}
elseif (!$this->cal_keepCanonical)
{
$htmlHeadBag->setCanonicalUri(Events::generateEventUrl($objEvent, true));
}
}

$intStartTime = $objEvent->startTime;
Expand Down
56 changes: 56 additions & 0 deletions calendar-bundle/src/Migration/KeepCanonicalMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CalendarBundle\Migration;

use Contao\CoreBundle\Migration\AbstractMigration;
use Contao\CoreBundle\Migration\MigrationResult;
use Doctrine\DBAL\Connection;

/**
* @internal
*/
class KeepCanonicalMigration extends AbstractMigration
{
public function __construct(private readonly Connection $connection)
{
}

public function shouldRun(): bool
{
$schemaManager = $this->connection->createSchemaManager();

if (!$schemaManager->tablesExist(['tl_module'])) {
return false;
}

$columns = $schemaManager->listTableColumns('tl_module');

return !isset($columns['cal_keepcanonical']);
}

public function run(): MigrationResult
{
$this->connection->executeStatement('
ALTER TABLE tl_module
ADD cal_keepCanonical tinyint(1) NOT NULL default 0
');

$this->connection->executeStatement("
UPDATE tl_module
SET cal_keepCanonical = 1
WHERE type = 'eventreader'
");

return $this->createResult(true);
}
}
9 changes: 8 additions & 1 deletion news-bundle/contao/dca/tl_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

// Add palettes to tl_module
$GLOBALS['TL_DCA']['tl_module']['palettes']['newslist'] = '{title_legend},name,headline,type;{config_legend},news_archives,news_readerModule,numberOfItems,news_featured,news_order,skipFirst,perPage;{template_legend:hide},news_template,customTpl;{image_legend:hide},imgSize;{protected_legend:hide},protected;{expert_legend:hide},cssID';
$GLOBALS['TL_DCA']['tl_module']['palettes']['newsreader'] = '{title_legend},name,headline,type;{config_legend},news_archives,overviewPage,customLabel;{template_legend:hide},news_template,customTpl;{image_legend:hide},imgSize;{protected_legend:hide},protected;{expert_legend:hide},cssID';
$GLOBALS['TL_DCA']['tl_module']['palettes']['newsreader'] = '{title_legend},name,headline,type;{config_legend},news_archives,news_keepCanonical;{news_overview_legend},overviewPage,customLabel;{template_legend:hide},news_template,customTpl;{image_legend:hide},imgSize;{protected_legend:hide},protected;{expert_legend:hide},cssID';
$GLOBALS['TL_DCA']['tl_module']['palettes']['newsarchive'] = '{title_legend},name,headline,type;{config_legend},news_archives,news_readerModule,news_format,news_order,news_jumpToCurrent,perPage;{template_legend:hide},news_template,customTpl;{image_legend:hide},imgSize;{protected_legend:hide},protected;{expert_legend:hide},cssID';
$GLOBALS['TL_DCA']['tl_module']['palettes']['newsmenu'] = '{title_legend},name,headline,type;{config_legend},news_archives,news_showQuantity,news_format,news_order;{redirect_legend},jumpTo;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},cssID';
$GLOBALS['TL_DCA']['tl_module']['palettes']['newsmenunews_day'] = '{title_legend},name,headline,type;{config_legend},news_archives,news_showQuantity,news_format,news_startDay;{redirect_legend},jumpTo;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},cssID';
Expand Down Expand Up @@ -105,6 +105,13 @@
'sql' => array('type' => 'boolean', 'default' => false)
);

$GLOBALS['TL_DCA']['tl_module']['fields']['news_keepCanonical'] = array
(
'inputType' => 'checkbox',
'eval' => array('tl_class'=>'w50'),
'sql' => array('type' => 'boolean', 'default' => false)
aschempp marked this conversation as resolved.
Show resolved Hide resolved
);

/**
* Provide miscellaneous methods that are used by the data configuration array.
*
Expand Down
9 changes: 8 additions & 1 deletion news-bundle/contao/dca/tl_news.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
'palettes' => array
(
'__selector__' => array('source', 'addImage', 'addEnclosure', 'overwriteMeta'),
'default' => '{title_legend},headline,featured,alias,author;{date_legend},date,time;{source_legend},source,linkText;{meta_legend},pageTitle,robots,description,serpPreview;{teaser_legend},subheadline,teaser;{image_legend},addImage;{enclosure_legend:hide},addEnclosure;{expert_legend:hide},cssClass;{publish_legend},published,start,stop',
'default' => '{title_legend},headline,featured,alias,author;{date_legend},date,time;{source_legend},source,linkText,canonicalLink;{meta_legend},pageTitle,robots,description,serpPreview;{teaser_legend},subheadline,teaser;{image_legend},addImage;{enclosure_legend:hide},addEnclosure;{expert_legend:hide},cssClass;{publish_legend},published,start,stop',
'internal' => '{title_legend},headline,featured,alias,author;{date_legend},date,time;{source_legend},source,jumpTo,linkText;{teaser_legend},subheadline,teaser;{image_legend},addImage;{enclosure_legend:hide},addEnclosure;{expert_legend:hide},cssClass;{publish_legend},published,start,stop',
'article' => '{title_legend},headline,featured,alias,author;{date_legend},date,time;{source_legend},source,articleId,linkText;{teaser_legend},subheadline,teaser;{image_legend},addImage;{enclosure_legend:hide},addEnclosure;{expert_legend:hide},cssClass;{publish_legend},published,start,stop',
'external' => '{title_legend},headline,featured,alias,author;{date_legend},date,time;{source_legend},source,url,target,linkText;{teaser_legend},subheadline,teaser;{image_legend},addImage;{enclosure_legend:hide},addEnclosure;{expert_legend:hide},cssClass;{publish_legend},published,start,stop'
Expand Down Expand Up @@ -222,6 +222,13 @@
'eval' => array('url_callback'=>array('tl_news', 'getSerpUrl'), 'title_tag_callback'=>array('tl_news', 'getTitleTag'), 'titleFields'=>array('pageTitle', 'headline'), 'descriptionFields'=>array('description', 'teaser')),
'sql' => null
),
'canonicalLink' => array
(
'search' => true,
'inputType' => 'text',
'eval' => array('rgxp'=>'url', 'decodeEntities'=>true, 'maxlength'=>2048, 'dcaPicker'=>true, 'tl_class'=>'w50'),
'sql' => "varchar(2048) NOT NULL default ''"
),
'subheadline' => array
(
'search' => true,
Expand Down
9 changes: 9 additions & 0 deletions news-bundle/contao/languages/en/tl_module.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@
<trans-unit id="tl_module.news_showQuantity.1">
<source>Show the number of news items of each month.</source>
</trans-unit>
<trans-unit id="tl_module.news_keepCanonical.0">
<source>Use the current URL for canonical links</source>
</trans-unit>
<trans-unit id="tl_module.news_keepCanonical.1">
<source>Keep the current URL instead of the configured reader page for canonical links.</source>
</trans-unit>
<trans-unit id="tl_module.news_overview_legend">
<source>Overview page</source>
</trans-unit>
<trans-unit id="tl_module.news_day">
<source>Day</source>
</trans-unit>
Expand Down
6 changes: 6 additions & 0 deletions news-bundle/contao/languages/en/tl_news.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
<trans-unit id="tl_news.description.1">
<source>Here you can add a custom meta description to overwrite the default page description.</source>
</trans-unit>
<trans-unit id="tl_news.canonicalLink.0">
<source>Canonical URL</source>
</trans-unit>
<trans-unit id="tl_news.canonicalLink.1">
<source>Here you can set a custom canonical URL like https://example.com/.</source>
</trans-unit>
<trans-unit id="tl_news.subheadline.0">
<source>Subheadline</source>
</trans-unit>
Expand Down
4 changes: 4 additions & 0 deletions news-bundle/contao/models/NewsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @property string $pageTitle
* @property string $robots
* @property string|null $description
* @property string $canonicalLink
* @property string $subheadline
* @property string|null $teaser
* @property boolean $addImage
Expand Down Expand Up @@ -67,6 +68,7 @@
* @method static NewsModel|null findOneByTime($val, array $opt=array())
* @method static NewsModel|null findOneByPageTitle($val, array $opt=array())
* @method static NewsModel|null findOneByDescription($val, array $opt=array())
* @method static NewsModel|null findOneByCanonicalLink($val, array $opt=array())
* @method static NewsModel|null findOneByRobots($val, array $opt=array())
* @method static NewsModel|null findOneBySubheadline($val, array $opt=array())
* @method static NewsModel|null findOneByTeaser($val, array $opt=array())
Expand Down Expand Up @@ -102,6 +104,7 @@
* @method static Collection<NewsModel>|NewsModel[]|null findByTime($val, array $opt=array())
* @method static Collection<NewsModel>|NewsModel[]|null findByPageTitle($val, array $opt=array())
* @method static Collection<NewsModel>|NewsModel[]|null findByDescription($val, array $opt=array())
* @method static Collection<NewsModel>|NewsModel[]|null findByCanonicalLink($val, array $opt=array())
* @method static Collection<NewsModel>|NewsModel[]|null findByRobots($val, array $opt=array())
* @method static Collection<NewsModel>|NewsModel[]|null findBySubheadline($val, array $opt=array())
* @method static Collection<NewsModel>|NewsModel[]|null findByTeaser($val, array $opt=array())
Expand Down Expand Up @@ -142,6 +145,7 @@
* @method static integer countByTime($val, array $opt=array())
* @method static integer countByPageTitle($val, array $opt=array())
* @method static integer countByDescription($val, array $opt=array())
* @method static integer countByCanonicalLink($val, array $opt=array())
* @method static integer countByRobots($val, array $opt=array())
* @method static integer countBySubheadline($val, array $opt=array())
* @method static integer countByTeaser($val, array $opt=array())
Expand Down
22 changes: 22 additions & 0 deletions news-bundle/contao/modules/ModuleNewsReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,28 @@ protected function compile()
{
$htmlHeadBag->setMetaRobots($objArticle->robots);
}

if ($objArticle->canonicalLink)
aschempp marked this conversation as resolved.
Show resolved Hide resolved
{
$url = System::getContainer()->get('contao.insert_tag.parser')->replaceInline($objArticle->canonicalLink);

// Ensure absolute links
if (!preg_match('#^https?://#', $url))
{
if (!$request = System::getContainer()->get('request_stack')->getCurrentRequest())
{
throw new \RuntimeException('The request stack did not contain a request');
}

$url = UrlUtil::makeAbsolute($url, $request->getUri());
}

$htmlHeadBag->setCanonicalUri($url);
}
elseif (!$this->news_keepCanonical)
{
$htmlHeadBag->setCanonicalUri(News::generateNewsUrl($objArticle, false, true));
}
}

$bundles = System::getContainer()->getParameter('kernel.bundles');
Expand Down