0

I'm trying to pass data to a block within a foreach loop.

I've created a new block search.phtml and added it to my layout.xml as follows:

       <block class="Magento\CatalogSearch\Block\Advanced\Form" name="catalogsearch_advanced_form" template="Amrita_CatalogSearch::advanced/form.phtml">
                    <block class="Magento\CatalogSearch\Block\Advanced\Form" name="catalogsearch_advanced_search" template="Amrita_CatalogSearch::advanced/search.phtml">
                        <arguments>
                            <argument name="my_arg" xsi:type="string">testvalue</argument>
                        </arguments>
                    </block>
</block>

I want to set the value of my_arg dynamically within a loop and render the search block, where i will retrieve the value of my_arg.

i've tried the following within the parent block:

$block->GetChild('search')->SetData('my_arg', $_code);
                            $block->getChildHtml('search');

and then to retrieve:

$_code = $block->getData('my_arg')

This throws an error:

Invalid method Magento\CatalogSearch\Block\Advanced\Form::GetChild(Array
(
    [0] => search
)
)

What's the correct way to achieve this?

  • In GetChild and SetData first letter must be in lowercase – Ігор Климчук Jun 17 '16 at 9:41
  • $block->getChild('search')->setData('my_arg', $_code); $block->getChildHtml('search'); isn't working either, getting the error Call to a member function setData() on null – Sophie Baxter Jun 17 '16 at 10:24
  • You dont have block with name "search". Try this $block->getChild('catalogsearch_advanced_search') – Ігор Климчук Jun 17 '16 at 10:36
  • I tried that too, same error. – Sophie Baxter Jun 17 '16 at 11:23
  • Maybe this answer will be usefull. – Zefiryn Jun 18 '16 at 11:00
3

I solved this as follows,

$block->getChildBlock("select")->setProductAttributes($_attribute, $_code);
                    echo $block->getChildHtml('select');


$_attributes = $block->getProductAttributes();

where getProductAttributes/setProductAttributes are declared in a custom class

    namespace Amrita\CatalogSearch\Block\Advanced;
use Magento\CatalogSearch\Block\Advanced;

class Form extends Advanced\Form{

    private $attribute;
    private $code;

    public function setProductAttributes($attribute, $code, $attributeTitle, $maxQueryLength, $isActive)
    {
        $this->attribute = $attribute;
        $this->code = $code;
        return $this;
    }

    public function getProductAttributes()
    {
        return array(
            'attribute' => $this->attribute,
            'code' => $this->code
    }
}

and in the layout.xml,

     <referenceBlock name="catalogsearch_advanced_form">
                    <action method="setTemplate">
                        <argument name="template" xsi:type="string">Amrita_CatalogSearch::advanced/form.phtml</argument>
                    </action>
                    <block class="Amrita\CatalogSearch\Block\Advanced\Form" name="catalogsearch_advanced_search" as="search" template="Amrita_CatalogSearch::advanced/search.phtml" />
</referenceBlock>
0

I used the following and it's now rendering the block:

$block->getChildHtml('catalogsearch_advanced_search')->setData('my_arg', $_attribute);

I still have an error

Call to a member function getAttributeCode() on null

within the search block

$_attribute = $block->getData('my_arg');
$_code = $_attribute->getAttributeCode()

i want to pass a single attribute object (rather than an attribute collection) but i can't get it to work. Is this the correct syntax?

 <arguments>
                        <argument name="attribute" xsi:type="array">
                            <item name="my_arg" xsi:type="object">Magento\Catalog\Model\ResourceModel\Eav\Attribute</item>
                        </argument>
                    </arguments>
0

getChild etc are not supported in Magento 2.

Whilst I haven't got that much experience with it yet, I have done something similar for a test module. It doesn't answer your question exactly but it may help you on the way:

$this->getLayout()->createBlock('Magento\CatalogSearch\Block\Advanced\Form')->setMyArg($_attribute)->toHtml();

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.