0

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.

  • You want to set value in $filter object? – Rohan Hapani Nov 14 '19 at 6:23
  • No... a different variable... i just named the getter and setter that way as it was related to filter – summu Nov 14 '19 at 6:27
  • @RohanHapani Edited – summu Nov 14 '19 at 6:28
  • But, in $filter object you can pass array/Object which you want. Then, you want to add in custom function? – Rohan Hapani Nov 14 '19 at 6:31
  • @RohanHapani you mean in $filter object I can set my custom variable and retrieve it in the child phtml ? – summu Nov 14 '19 at 6:34
1

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.

| improve this answer | |
  • Can you tell me what does render($filter) function does on getChildBlock.... – summu Nov 14 '19 at 7:35
  • 1
    Just give me few minute. – Rohan Hapani Nov 14 '19 at 7:40
  • 1
    Btw. Thanks @sumit. It's typo :D – Rohan Hapani Nov 14 '19 at 7:41
  • 1
    @summu You can check here in this file : vendor/magento/module-layered-navigation/Block/Navigation/FilterRenderer.php – Rohan Hapani Nov 14 '19 at 8:30

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.