2

I want to change the Meta Tag to NOINDEX, NOFOLLOW I am trying with setting an observer, but m having very hard time to find exact event.

My events.xml is

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch">
        <observer name="pinkblue_seo" instance="PinkBlue\SeoUrl\Observer\ChangeMetaTags" />
    </event>

</config>

The observer file is :

<?php

namespace PinkBlue\SeoUrl\Observer;

use Magento\Framework\Event\ObserverInterface;

class ChangeMetaTags implements ObserverInterface {


    public function execute(\Magento\Framework\Event\Observer $observer){
         rm_state()->controllerSet($observer['controller_action']);

        if($observer->getEvent()->getAction()->getFullActionName() == 'catalog_category_view'){
            $uri = $observer->getEvent()->getAction()->getRequest()->getRequestUri();
            if(stristr($uri,"?"))// looking for a ?
            {
                $layout       = $observer->getEvent()->getLayout();
                $product_info = $layout->getBlock('head');
                $layout->getUpdate()->addUpdate('<reference name="head"><action method="setRobots"><value>NOINDEX,NOFOLLOW</value></action></reference>');
                $layout->generateXml();
            }
        }

}

**Answer : when we want to do it only for category filter is applied. It worked **

 public function execute(\Magento\Framework\Event\Observer $observer)
    {

        $full_action_name = $observer->getFullActionName();

        $layout = $observer->getEvent()->getLayout();
        if($full_action_name == 'catalog_category_view'){
            $uri = $_SERVER['REQUEST_URI'];

            if(stristr($uri,"?"))
            {
                $this->_pageConfig->setRobots('NOINDEX,NOFOLLOW');
            }
            //print_r($layout->getBlock('head')->getRobots());
        }

    }
4

Your observer is not right,

You need need to use layout_generate_blocks_before event.

Then using

$this->_pageConfig->setRobots('NOINDEX,NOFOLLOW');

Here $this->_pageConfig is \Magento\Framework\View\Page\Config

Observer file ObserverChangeMetaTagForCategory.php is located app/code/Devamitbera/ChangeMetaTags/Observer/

<?php
namespace Devamitbera\ChangeMetaTags\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class ObserverChangeMetaTagForCategory implements ObserverInterface
{


    /**
     * @var \Magento\Framework\App\ActionFlag
     */
    protected $_actionFlag;

    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    protected $messageManager;

    /**
     * @var \Magento\Framework\App\Response\RedirectInterface
     */
    protected $redirect;


    private $_pageConfig;

    /**
     * @param \Magento\Framework\App\ActionFlag $actionFlag
     * @param \Magento\Framework\Message\ManagerInterface $messageManager
     * @param \Magento\Framework\App\Response\RedirectInterface $redirect
     */
    public function __construct(
        \Magento\Framework\App\ActionFlag $actionFlag,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Framework\View\Page\Config $pageConfig     
    ) {
        $this->_actionFlag = $actionFlag;
        $this->messageManager = $messageManager;
        $this->redirect = $redirect;
         $this->_pageConfig = $pageConfig; 
    }

    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /** @var \Magento\Framework\App\Action\Action $controller */
        $full_action_name = $observer->getFullActionName();

        $layout = $observer->getEvent()->getLayout();
        if($full_action_name == 'catalog_category_view'){
            //print_r($layout->getBlock('head')->getRobots());
            $this->_pageConfig->setRobots('NOINDEX,NOFOLLOW');
        }

    }

}

Full module available at github

3
  • Hey I tried but it is changing meta tags even after selecting categories, while i want only when filer is applied – supriya mishra Dec 31 '16 at 20:36
  • 1
    @amit Is it possible to change <meta property="og:title" content="Test"/> custom meta tag from controller? – Newbie Apr 6 '18 at 8:14
  • hi amit bera, any help appreciated thanks magento.stackexchange.com/a/330072/57334 – zus Jan 29 at 3:45
1

Try this

events.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="layout_generate_blocks_after">
        <observer name="result_robots" instance="Vendor\ResultRobots\Observer\ResultRobotsObserver" />
    </event>
</config>

Observer:

<?php

use Magento\Framework\App\Request\Http;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\View\Page\Config;

class ResultRobotsObserver implements ObserverInterface
{
    public $request;

    public $layoutFactory;

    public $layerResolver;

    public function __construct(
        Http $request,
        Config $layoutFactory,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver
    ) {
        $this->request = $request;
        $this->layoutFactory = $layoutFactory;
        $this->layerResolver = $layerResolver;
    }

    /**
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        $fullActionName = $this->request->getFullActionName();
        $filtersCount = sizeof($this->layerResolver->get()->getState()->getFilters());

        if ($fullActionName == "catalog_category_view" &&
            $filtersCount > 1) {
            $this->layoutFactory->setRobots('NOINDEX,NOFOLLOW');
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.