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?

asked Dec 29, 2016 at 8:28

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();
answered Dec 29, 2016 at 8:34
2
  • How can I call this in form once I injected the constructor.Thanks in advance. Commented Dec 29, 2016 at 8:47
  • See the update..you can understand Commented Dec 29, 2016 at 8:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.