Pregunta

I have to pass a variable in layer/view.phtml

/Magento_LayeredNavigation/templates/layer/view.phtml

to its getChildBlock which is renderer/labels.phtml

Amasty_Shopby/templates/layer/filter/category/items/renderer/labels.phtml

I am not able to send a variable, have added a setter & getter in its block file.

Following the layer/view.phtml file the block file I find is $block \Magento\LayeredNavigation\Block\Navigation

I have overridden the vendor file with the following code

class Navigation extends \Magento\LayeredNavigation\Block\Navigation
{

   private $_filter;

   public function setCustomVariable($filter){
   $this->_filter = $filter;
   }

   public function getCustomVariable(){
    return $this->_filter;
   }
 }

I want something like that

<div class="filter-options-content"><?php /* @escapeNotVerified */ echo $block->getChildBlock('renderer')->setCustomVariable($customData)->render($filter); ?></div>

and in the child HTML which is layer/filter/category/items/renderer/labels.phtml

$block->getCustomVariable()

but it doesn't set the variable in layer/view.phtml and not able to get the value in its child phtml renderer/labels.phtml

So upon digging I find the catalog_category_view_type_layered.xml in vendor which

vendor/magento/module-layered-navigation/view/frontend/layout/catalog_category_view_type_layered.xml

 <attribute name="class" value="page-with-filter"/>
    <referenceContainer name="sidebar.main">
        <block class="Magento\LayeredNavigation\Block\Navigation\Category" name="catalog.leftnav" before="-" template="Magento_LayeredNavigation::layer/view.phtml">
            <block class="Magento\LayeredNavigation\Block\Navigation\State" name="catalog.navigation.state" as="state" />
            <block class="Magento\LayeredNavigation\Block\Navigation\FilterRenderer" name="catalog.navigation.renderer" as="renderer" template="Magento_LayeredNavigation::layer/filter.phtml"/>
        </block>
    </referenceContainer>

This say Magento_LayeredNavigation::layer/view.phtml has block file Magento\LayeredNavigation\Block\Navigation\Category which doesnt even exist in my code base of vendor.

Magento\LayeredNavigation\Block\Navigation\State and Magento\LayeredNavigation\Block\Navigation\FilterRenderer is there in the vendor block folder but no Magento\LayeredNavigation\Block\Navigation\Category.

enter image description here

At first, I have overridden the Navigation.php which is under Block\Nagivation.

¿Fue útil?

Solución

You can set value in $filter object and access in your labels.phtml.

Add this code in your layer/view.phtml file :

<?= $filter->setData('custom_variable','custom_value');
<div class="filter-options-content"><?php /* @escapeNotVerified */ echo $block->getChildBlock('renderer')->setCustomVariable($customData)->render($filter); ?></div>

Now, access in your labels.phtml :

$filter->getCustomVariable();

or

$block->getFilter()->getCustomVariable();

Clean cache and check it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top