blackbird / hreflang-tag
1.0.5
2024-03-28 15:54 UTC
Requires
- magento/module-deploy: >=100.0.0
README
- 在“店铺” -> “系统配置” -> “Blackbird 扩展” -> “Href Lang”中填写所有配置
- 请注意,一些配置仅通过“店铺视图”可用
启用 Href Lang 块
启用块以注入布局
仅显示来自同一网站的店铺
定义是否在 hreflang 标签中显示所有店铺视图,或仅显示与当前爬取网站相同的店铺视图。
替换原生态店铺切换器 URL
原生态店铺切换器块将使用 hreflang 系统的 URL 而不是丑陋的默认 Magento URL,更有利于 SEO。如果通过 hreflang 系统不可用,则回退到默认系统。
从 URL 中移除 "___store" 参数
如果 URL 键中有 ___store,请移除它
默认语言
定义哪个语言是默认的。根据在“店铺视图”范围中为“HrefLang 的语言代码”字段定义的设置,定义哪个店铺视图将附加一个“x-default”标签。
店铺视图的附加配置
为此店铺使用 Href Lang 块
可以排除目标店铺视图从 hreflang 系统中
为 HrefLang 标签定义语言代码
(例如:“fr”、“en-us”、“es-us”)。默认情况下将使用全局语言配置
在提供者中添加自己的 hreflang 系统
可以轻松地根据不同的模块逻辑添加基于不同模块的逻辑的附加 hreflang URL。
- 在独立的 Magento 模块(例如 HrefLangContentManager)中的 di.xml 文件中声明您的提供者
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Blackbird\HrefLang\Api\HrefLangProvidersInterface"> <arguments> <argument name="providers" xsi:type="array"> <item name="contentmanager_content" xsi:type="array"> <item name="class" xsi:type="object">Blackbird\HrefLangContentManager\Model\Provider\Content</item> <item name="sortOrder" xsi:type="number">8</item> <item name="enabled" xsi:type="boolean">true</item> </item> </argument> </arguments> </type> </config>
- 定义您的 PHP 类并实现 ProviderInterface:例如,对于高级内容管理器模块的示例
<?php namespace Blackbird\HrefLangContentManager\Model\Provider; use Blackbird\ContentManager\Api\Data\ContentInterface; use Blackbird\HrefLang\Api\ProviderInterface; use Blackbird\HrefLang\Model\Provider\AbstractProvider; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\RequestInterface; use Magento\Framework\Registry; use Magento\Framework\View\LayoutInterface; use Magento\Store\Api\Data\StoreInterface; use Magento\Store\Model\App\Emulation; use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollectionFactory; class Content extends AbstractProvider implements ProviderInterface { protected Registry $registry; protected UrlRewriteCollectionFactory $urlRewriteCollectionFactory; protected Emulation $emulation; public function __construct( UrlRewriteCollectionFactory $urlRewriteCollectionFactory, Registry $registry, ScopeConfigInterface $scopeConfig, LayoutInterface $layout, Emulation $emulation, RequestInterface $request ) { parent::__construct( $scopeConfig, $layout, $request); $this->registry = $registry; $this->urlRewriteCollectionFactory = $urlRewriteCollectionFactory; $this->emulation = $emulation; } public function getAlternativeUrlForStore(StoreInterface $store): ?string { if ($content = $this->getCurrentContent()) { return $this->getContentUrl( $content, $store); } return null; } protected function getContentUrl(ContentInterface $content, $store): string { $url = ''; $urlRewriteCollection = $this->urlRewriteCollectionFactory->create(); $urlRewriteCollection->addStoreFilter($store); $urlRewriteCollection->addFieldToFilter( 'entity_id', $content->getId()); $urlRewriteCollection->addFieldToFilter( 'entity_type', 'contenttype_content'); $urlRewriteCollection->addFieldToSelect(['request_path']); $urlRewrite = $urlRewriteCollection->getFirstItem(); if ($urlRewrite && $urlRewrite->getRequestPath()) { if ($this->getRemoveStoreTag()) { $this->emulation->startEnvironmentEmulation($store->getId()); $url = $store->getUrl('/') . $urlRewrite['request_path']; $this->emulation->stopEnvironmentEmulation(); } else { $url = $store->getUrl($urlRewrite['request_path']); } } return $url; } protected function getCurrentContent(): ?ContentInterface { //Check we are on right router if (!in_array( 'contentmanager_index_content', $this->layout->getUpdate()->getHandles())) { return null; } //Check current content exists and return it return $this->registry->registry('current_content'); } }