2

I am working on a custom module. I am getting array of attribute values in my custom controller and I want to pass these values in my template to list in a select dropdown. Please provide a solution -

Controller -

<?php

namespace Vendor\Module\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_productCollectionFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
    ) {
        $this->_productCollectionFactory = $productCollectionFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $Urlparam =  $this->getRequest()->getParam('make');
        echo $Urlparam;
        $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('id');
        $collection->addAttributeToSelect('model');
        $collection->addAttributeToSelect('status');
        $collection->addAttributeToFilter('status',1);
        $collection->addAttributeToFilter('make',5711);
        //$count =0;
        $modelValues[] = "";
        foreach ($collection as $product) {
            $modelValue = $product->getAttributeText("model");;
            if ((in_array($modelValue,$modelValues)) == false) {
                $modelValues[] = $modelValue;
            }
        }
    }
}

Block file -

<?php

namespace Vendor\Module\Block;

class Vendor extends \Magento\Framework\View\Element\Template
{
    protected $_productCollectionFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        array $data = []
    ) {
        $this->_productCollectionFactory = $productCollectionFactory;
        parent::__construct($context, $data);
    }

    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('id');
        $collection->addAttributeToSelect('make');
        $collection->addAttributeToSelect('status');
        $collection->addAttributeToFilter('status',1);

        return $collection;
    }
}

Template file -

<select class="select-model">
    <option>Select Model</option> 
</select>

I am new to this concept. Please provide a solution for how to show the array from the controller in phtml select option.

1 Answer 1

0

You can use Registry \Magento\Framework\Registry you can pass the parameters from the controller to Block. At 2.3, Registry is deprecated but still, you can use.

Inject the registry class to your controller class __construct

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Framework\Registry $registry
) {
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->registry =  $registry;
    parent::__construct($context);
}

$modelValue to registry a variable. Add below code after for each loop of at your controller's execute() method.

if ($this->registry->registry('makes_info')) {
    $this->registry->unregister('makes_info');
}
// set value to registry variable  makes_info
$this->registry->register('makes_info', $modelValues);

After that Inject the registry class to your Block class __construct

At same way and

After that access that registry variable below way

if ($this->registry->registry('makes_info')) {
    $makes_info = $this->registry->registry('makes_info');
}
3
  • please provide detailed solution. As how to implement in controller and template.
    – chanchal
    Nov 7, 2019 at 7:59
  • and how to get that in tempalte file select option?
    – chanchal
    Nov 7, 2019 at 8:48
  • $block->getRequest()->getParam('make') is same as $Urlparam = $this->getRequest()->getParam('make'); at phtml
    – Amit Bera
    Nov 7, 2019 at 9:06

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.