1

I want a new custom block to get all option values to a given filter product attribute in Magento_LayeredNavigation::layer/view.phtml.

We have our own design folder which overrides the default layout:

enter image description here

Now i want to modify this view.phtml with a new Block which we build in a custom module:

<?php

namespace MyVendor\MyNamespace\Block\Catalog;

class AttributeOptions extends \Magento\Framework\View\Element\Template
{
    public function __construct(\Magento\Framework\View\Element\Template\Context $context)
    {
        parent::__construct($context);
    }

    public function getOptions()
    {
        $aReturnValues = array('A', 'B');
        return $aReturnValues;
    }
}

In our module we add this:

app\code\MyVendor\MyNamespace\view\frontend\layout\catalog_category_layered.xml

With:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="sidebar.main">
        <block class="MyVendor\MyNamespace\Block\Catalog\AttributeOptions"/>
    </referenceContainer>
</page>

Than we edit the view.phtml and and this:

enter image description here

We activate the whole module but we get an

Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid method exception.

Can anyone tell us the correct way to modify a phtml with a new block which has no own template?

1 Answer 1

1

Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid method exception.

Already they declared Magento_LayeredNavigation::layer/view.phtml to this block app/code/Magento/LayeredNavigation/Block/Navigation.php.

You only override the phtml so it is still render the method from old block so it throws method not found error. If you want to customize the block you need to override the block also.

for override the block add the below content in your app/code/vendor/module_name/etc/di.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\LayeredNavigation\Block\Navigation" type="MyVendor\MyNamespace\Block\Catalog\AttributeOptions" />
</config>

after that your custom theme view.phtml render method from your MyVendor\MyNamespace\Block\Catalog\AttributeOptions.php

no need this declaration \app\code\MyVendor\MyNamespace\view\frontend\layout\catalog_category_layered.xml. this is not a way to override block

1
  • I only want to add a new block inside a given layout ( /app/design/... ) from a custom block. Do i realy have to rewrite the complete Navigation Block ? Commented Aug 27, 2016 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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