Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Instance.php
Go to the documentation of this file.
1 <?php
11 
12 abstract class Instance extends \Magento\Backend\App\Action
13 {
19  const ADMIN_RESOURCE = 'Magento_Widget::widget_instance';
20 
26  protected $_coreRegistry;
27 
31  protected $_widgetFactory;
32 
36  protected $_logger;
37 
41  protected $mathRandom;
42 
46  protected $_translateInline;
47 
56  public function __construct(
57  \Magento\Backend\App\Action\Context $context,
58  \Magento\Framework\Registry $coreRegistry,
59  \Magento\Widget\Model\Widget\InstanceFactory $widgetFactory,
60  \Psr\Log\LoggerInterface $logger,
61  \Magento\Framework\Math\Random $mathRandom,
62  \Magento\Framework\Translate\InlineInterface $translateInline
63  ) {
64  $this->_translateInline = $translateInline;
65  $this->_coreRegistry = $coreRegistry;
66  $this->_widgetFactory = $widgetFactory;
67  $this->_logger = $logger;
68  $this->mathRandom = $mathRandom;
69  parent::__construct($context);
70  }
71 
77  protected function _initAction()
78  {
79  $this->_view->loadLayout();
80  $this->_setActiveMenu(
81  'Magento_Widget::cms_widget_instance'
82  )->_addBreadcrumb(
83  __('CMS'),
84  __('CMS')
85  )->_addBreadcrumb(
86  __('Manage Widget Instances'),
87  __('Manage Widget Instances')
88  );
89  return $this;
90  }
91 
97  protected function _initWidgetInstance()
98  {
100  $widgetInstance = $this->_widgetFactory->create();
101 
102  $code = $this->getRequest()->getParam('code', null);
103  $instanceId = $this->getRequest()->getParam('instance_id', null);
104  if ($instanceId) {
105  $widgetInstance->load($instanceId)->setCode($code);
106  if (!$widgetInstance->getId()) {
107  $this->messageManager->addError(__('Please specify a correct widget.'));
108  return false;
109  }
110  } else {
111  // Widget id was not provided on the query-string. Locate the widget instance
112  // type (namespace\classname) based upon the widget code (aka, widget id).
113  $themeId = $this->getRequest()->getParam('theme_id', null);
114  $type = $code != null ? $widgetInstance->getWidgetReference('code', $code, 'type') : null;
115  $widgetInstance->setType($type)->setCode($code)->setThemeId($themeId);
116  }
117  $this->_coreRegistry->register('current_widget_instance', $widgetInstance);
118  return $widgetInstance;
119  }
120 
127  protected function setBody($body)
128  {
129  $this->_translateInline->processResponseBody($body);
130 
131  $this->getResponse()->setBody($body);
132  }
133 }
__()
Definition: __.php:13
$logger
$type
Definition: item.phtml:13
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Framework\Registry $coreRegistry, \Magento\Widget\Model\Widget\InstanceFactory $widgetFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Math\Random $mathRandom, \Magento\Framework\Translate\InlineInterface $translateInline)
Definition: Instance.php:56
$code
Definition: info.phtml:12