Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Messages.php
Go to the documentation of this file.
1 <?php
7 
9 {
15  protected $_messages;
16 
21  protected $jsonHelper;
22 
26  private $serializer;
27 
35  public function __construct(
36  \Magento\Backend\Block\Template\Context $context,
37  \Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized $messages,
38  \Magento\Framework\Json\Helper\Data $jsonHelper,
39  array $data = [],
40  \Magento\Framework\Serialize\Serializer\Json $serializer = null
41  ) {
42  $this->jsonHelper = $jsonHelper;
43  parent::__construct($context, $data);
44  $this->_messages = $messages;
45  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
46  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
47  }
48 
54  protected function _toHtml()
55  {
56  if (count($this->_messages->getItems())) {
57  return parent::_toHtml();
58  }
59  return '';
60  }
61 
67  public function getLastCritical()
68  {
69  $items = array_values($this->_messages->getItems());
70  if (isset(
71  $items[0]
73  ) {
74  return $items[0];
75  }
76  return null;
77  }
78 
84  public function getCriticalCount()
85  {
86  return $this->_messages->getCountBySeverity(
87  \Magento\Framework\Notification\MessageInterface::SEVERITY_CRITICAL
88  );
89  }
90 
96  public function getMajorCount()
97  {
98  return $this->_messages->getCountBySeverity(
99  \Magento\Framework\Notification\MessageInterface::SEVERITY_MAJOR
100  );
101  }
102 
108  public function hasMessages()
109  {
110  return (bool)count($this->_messages->getItems());
111  }
112 
118  protected function _getMessagesUrl()
119  {
120  return $this->getUrl('adminhtml/system_message/list');
121  }
122 
128  public function getSystemMessageDialogJson()
129  {
130  return $this->serializer->serialize(
131  [
132  'systemMessageDialog' => [
133  'buttons' => [],
134  'modalClass' => 'ui-dialog-active ui-popup-message modal-system-messages',
135  'ajaxUrl' => $this->_getMessagesUrl()
136  ],
137  ]
138  );
139  }
140 }
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized $messages, \Magento\Framework\Json\Helper\Data $jsonHelper, array $data=[], \Magento\Framework\Serialize\Serializer\Json $serializer=null)
Definition: Messages.php:35
$items