2

I want to display CMS block names (for example, footer links, promotional banners, etc.) and display modes (for example, products only, static block only, static block and products) in the admin form dropdown.

In Magento 1, I have displayed it using the following code:

Display mode:

'values'=>Mage::getModel('catalog/category_attribute_source_mode')->getAllOptions() display mode

CMS block

'values' => Mage::getModel('catalog/category_attribute_source_page')->getAllOptions()

What is the equivalent code for the above in Magento 2 for getting the CMS block name and display modes in the admin form dropdown?

1 Answer 1

2

Magento2, also have the source Model for an eav attribute concept like Magento 1.X.

So, you can get display mod and static block using respective source model

Display Mode, you can use Magento\Catalog\Model\Category\Attribute\Source\Mode

Cms static block, you can use

and Magento\Catalog\Model\Category\Attribute\Source\Page

On __construct() function you need to inject this two classes

protected $mode; 
    protected $Staticblocks; 

    public function __construct(
        \Magento\Catalog\Model\Category\Attribute\Source\Mode $mode,
        \Magento\Catalog\Model\Category\Attribute\Source\Page $Staticblocks,

    ) {
    ...
        $this->mode = $mode;
        $this->Staticblocks = $Staticblocks;

        .....
    }

Then at your code, you can get option values & label by

  • $this->mode->getAllOptions();
  • $this->Staticblocks->getAllOptions();
2
  • How can I call this in form once I injected the constructor.Thanks in advance.
    – Vindhuja
    Commented Dec 29, 2016 at 8:47
  • See the update..you can understand
    – Amit Bera
    Commented Dec 29, 2016 at 8:48

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.