PHP code example of blackbird / hreflang-tag

1. Go to this page and download the library: Download blackbird/hreflang-tag library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

blackbird / hreflang-tag example snippets




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');
    }

}