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">
                            <argument name="my_arg" xsi:type="string">testvalue</argument>

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);

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 Commented Jun 17, 2016 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 Commented Jun 17, 2016 at 10:24
  • You dont have block with name "search". Try this $block->getChild('catalogsearch_advanced_search') Commented Jun 17, 2016 at 10:36
  • I tried that too, same error. Commented Jun 17, 2016 at 11:23
  • Maybe this answer will be usefull.
    – Zefiryn
    Commented Jun 18, 2016 at 11:00

3 Answers 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>
                    <block class="Amrita\CatalogSearch\Block\Advanced\Form" name="catalogsearch_advanced_search" as="search" template="Amrita_CatalogSearch::advanced/search.phtml" />

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?

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

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:


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.