3

In magento 1.x you called the block template processor such as

$helper = Mage::helper('cms');
$processor = $helper->getBlockTemplateProcessor();

echo $processor->filter($somecontent)

This would allow you to take advantage of variables, widgets and WYSIWYG features etc. I'm trying to figure out where the processor now resides in Magento 2.x and the best practice for parsing content through it.

Built out a model Testing\Testmodule\Model\Processor.php using the below code by Amit,

namespace Testing\Testmodule\Model;

class Processor extends \Magento\Framework\View\Element\AbstractBlock implements \Magento\Framework\DataObject\IdentityInterface
{

public function __construct(
    \Magento\Framework\View\Element\Context $context,
    \Magento\Cms\Model\Template\FilterProvider $filterProvider,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Cms\Model\BlockFactory $blockFactory,
    array $data = []
) {
    parent::__construct($context, $data);
    $this->_filterProvider = $filterProvider;
    $this->_storeManager = $storeManager;
    $this->_blockFactory = $blockFactory;
}


protected function content()
{
    $blockId = 'testblock';
    $html = '';
    if ($blockId) {
        $storeId = $this->_storeManager->getStore()->getId();
        /** @var \Magento\Cms\Model\Block $block */
        $block = $this->_blockFactory->create();
        $block->setStoreId($storeId)->load($blockId);

            $html = $this->_filterProvider->getBlockFilter()->setStoreId($storeId)->filter($block->getContent());
    }
    echo  $html;
}

}

And then I am calling the model inside my controller with

$processor = $this->_objectManager->create('Testing\Testmodule\Model\Processor')->content();

It is returning

Fatal error:  Class Testing\Testmodule\Model\Processor contains 1 abstract 

method
 and must therefore be declared abstract or implement the remaining methods (Magento\Framework\DataObject
\IdentityInterface::getIdentities) in /croot/www/mag2rc1/app/code/Testing/Testmodule
/Model/Processor.php

I've even tried including the class directly in the index controller and the same error is returned. Any insight into why this would be happening.

  • 1
    try removing implements \Magento\Framework\DataObject\IdentityInterface and see what happens. – Marius Nov 16 '15 at 16:47
  • @Marius I used your idea and came up with what I think is probably a good final solution and posted it to the thread. – Eirik Nov 16 '15 at 23:19
3

It took a bit of work and combine the answers from @Amit, @Alex, and @Marius

To call the template processor I created a model processor.php

namespace Testing\Testmodule\Model;

class Processor 
{

    public function __construct(
        \Magento\Cms\Model\Template\FilterProvider $filterProvider,
        \Magento\Cms\Model\BlockFactory $blockFactory
    ) {
        $this->_filterProvider = $filterProvider;
        $this->_blockFactory = $blockFactory;
    }


    public function content($content)
    {

                return $this->_filterProvider->getBlockFilter()->filter($content);


    }

}

Now to pass the content from the Controller to Modal I call it like

$this->_objectManager->create('Testing\Testmodule\Model\Processor')->content($content);

I'm not sure if calling the objectManager in such a way is the best practice, but it seems to be working.

|improve this answer|||||
  • Object manager should never be used by your classes to instantiate objects directly, use construct dependency injection (like you did for filter provider in processor). There are several exceptions, but very rare. – Alex Paliarush Nov 17 '15 at 6:26
2
+50

You can try this:

  <?php 
namespace Your\Class\Name;
class Here  extends \Magento\Framework\View\Element\AbstractBlock implements \Magento\Framework\DataObject\IdentityInterface
{ 
    public function __construct(
        \Magento\Framework\View\Element\Context $context,
        \Magento\Cms\Model\Template\FilterProvider $filterProvider,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Cms\Model\BlockFactory $blockFactory,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_filterProvider = $filterProvider;
        $this->_storeManager = $storeManager;
        $this->_blockFactory = $blockFactory;
    }


    protected function content()
    {
        $blockId = 'YOurBlock_Id';
        $html = '';
        if ($blockId) {
            $storeId = $this->_storeManager->getStore()->getId();
            /** @var \Magento\Cms\Model\Block $block */
            $block = $this->_blockFactory->create();
            $block->setStoreId($storeId)->load($blockId);

                $html = $this->_filterProvider->getBlockFilter()->setStoreId($storeId)->filter($block->getContent());
        }
        echo  $html;
    }

}
|improve this answer|||||
  • been working on this for a bit but encountering an error. Edited original question to show what I have done and the error returned. – Eirik Nov 15 '15 at 2:29
1

Your class implements \Magento\Framework\DataObject\IdentityInterface so you must have getIdentities() interface method implemented in it. See sample implementation of this method here \Magento\Catalog\Model\Category::getIdentities().

Or just remove implements \Magento\Framework\DataObject\IdentityInterface. Not sure why your class needs to implement this interface. It is used by page cache module to extract a list of entities displayed on a page. Then it is possible to flush all pages containing let's say particular product information.

|improve this answer|||||

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.