Skip to content

Commit

Permalink
#6 Allow different behavior for homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
nenes25 committed Apr 4, 2022
1 parent 170cd9b commit 3c49df8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Block/Page/Canonical.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getCanonicalPageUrl()
{
if (null !== $this->getPage() && $this->_helper->isCanonicalUrlEnable()) {
if ($this->isHomePage()) {
if ($this->_helper->useTrailingSlash()) {
if ($this->_helper->useTrailingSlashForHomepage()) {
$url = $this->getUrl('');
} else {
$url = rtrim($this->getUrl(''), '/');
Expand Down
17 changes: 17 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
*/
const XML_PATH_CANONICAL_USE_TRAILING_SLASH = 'cms/canonical/use_trailing_slash';

/**
* Cms canonical use trailing slash for homepage config path
*/
const XML_PATH_CANONICAL_USE_TRAILING_SLASH_HOMEPAGE = 'cms/canonical/use_trailing_slash_homepage';

/**
* Return if canonical url is enabled
* @return bool
Expand All @@ -38,4 +43,16 @@ public function useTrailingSlash()
ScopeInterface::SCOPE_STORE
);
}

/**
* Return if we use the trailing slash for the homepage
* @return bool
*/
public function useTrailingSlashForHomepage()
{
return $this->scopeConfig->isSetFlag(
self::XML_PATH_CANONICAL_USE_TRAILING_SLASH_HOMEPAGE,
ScopeInterface::SCOPE_STORE
);
}
}
50 changes: 27 additions & 23 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<!-- Ajout d'un nouveau groupe dans la section cms existante -->
<section id="cms">
<group id="canonical" translate="label" type="text" sortOrder="200" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Canonical urls</label>
<field id="enable_canonical" translate="label" type="select" sortOrder="100" showInDefault="1" showInWebsite="1"
showInStore="1" canRestore="1">
<label>Enable Canonical for cms pages</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="use_trailing_slash" translate="label" type="select" sortOrder="200" showInDefault="1" showInWebsite="1"
showInStore="1" canRestore="1">
<label>Use trailing slash</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</section>
</system>
</config>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="cms">
<group id="canonical" translate="label" type="text" sortOrder="200" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Canonical urls</label>
<field id="enable_canonical" translate="label" type="select" sortOrder="100" showInDefault="1" showInWebsite="1"
showInStore="1" canRestore="1">
<label>Enable Canonical for cms pages</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="use_trailing_slash" translate="label" type="select" sortOrder="200" showInDefault="1" showInWebsite="1"
showInStore="1" canRestore="1">
<label>Use trailing slash</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="use_trailing_slash_homepage" translate="label" type="select" sortOrder="200" showInDefault="1" showInWebsite="1"
showInStore="1" canRestore="1">
<label>Use trailing slash for homepage</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</section>
</system>
</config>
23 changes: 12 additions & 11 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<cms>
<canonical>
<enable_canonical>1</enable_canonical>
<use_trailing_slash>0</use_trailing_slash>
</canonical>
</cms>
</default>
</config>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<cms>
<canonical>
<enable_canonical>1</enable_canonical>
<use_trailing_slash>0</use_trailing_slash>
<use_trailing_slash_homepage>0</use_trailing_slash_homepage>
</canonical>
</cms>
</default>
</config>

0 comments on commit 3c49df8

Please sign in to comment.