Template.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Backend\Block;
  8. /**
  9. * Standard admin block. Adds admin-specific behavior and event.
  10. * Should be used when you declare a block in admin layout handle.
  11. *
  12. * Avoid extending this class if possible.
  13. *
  14. * If you need custom presentation logic in your blocks, use this class as block, and declare
  15. * custom view models in block arguments in layout handle file.
  16. *
  17. * Example:
  18. * <block name="my.block" class="Magento\Backend\Block\Template" template="My_Module::template.phtml" >
  19. * <arguments>
  20. * <argument name="view_model" xsi:type="object">My\Module\ViewModel\Custom</argument>
  21. * </arguments>
  22. * </block>
  23. *
  24. * Your class object can then be accessed by doing $block->getViewModel()
  25. *
  26. * @api
  27. * @SuppressWarnings(PHPMD.NumberOfChildren)
  28. * @since 100.0.2
  29. */
  30. class Template extends \Magento\Framework\View\Element\Template
  31. {
  32. /**
  33. * @var \Magento\Framework\AuthorizationInterface
  34. */
  35. protected $_authorization;
  36. /**
  37. * @var \Magento\Framework\Math\Random
  38. */
  39. protected $mathRandom;
  40. /**
  41. * @var \Magento\Backend\Model\Session
  42. */
  43. protected $_backendSession;
  44. /**
  45. * @var \Magento\Framework\Data\Form\FormKey
  46. */
  47. protected $formKey;
  48. /**
  49. * @var \Magento\Framework\Code\NameBuilder
  50. */
  51. protected $nameBuilder;
  52. /**
  53. * @param \Magento\Backend\Block\Template\Context $context
  54. * @param array $data
  55. */
  56. public function __construct(\Magento\Backend\Block\Template\Context $context, array $data = [])
  57. {
  58. $this->_localeDate = $context->getLocaleDate();
  59. $this->_authorization = $context->getAuthorization();
  60. $this->mathRandom = $context->getMathRandom();
  61. $this->_backendSession = $context->getBackendSession();
  62. $this->formKey = $context->getFormKey();
  63. $this->nameBuilder = $context->getNameBuilder();
  64. parent::__construct($context, $data);
  65. }
  66. /**
  67. * Retrieve Session Form Key
  68. *
  69. * @return string
  70. */
  71. public function getFormKey()
  72. {
  73. return $this->formKey->getFormKey();
  74. }
  75. /**
  76. * Check whether or not the module output is enabled.
  77. *
  78. * Because many module blocks belong to Backend module,
  79. * the feature "Disable module output" doesn't cover Admin area.
  80. *
  81. * @param string $moduleName Full module name
  82. * @return boolean
  83. * @deprecated 100.2.0 Magento does not support disabling/enabling modules output from the Admin Panel since 2.2.0
  84. * version. Module output can still be enabled/disabled in configuration files. However, this functionality should
  85. * not be used in future development. Module design should explicitly state dependencies to avoid requiring output
  86. * disabling. This functionality will temporarily be kept in Magento core, as there are unresolved modularity
  87. * issues that will be addressed in future releases.
  88. */
  89. public function isOutputEnabled($moduleName = null)
  90. {
  91. if ($moduleName === null) {
  92. $moduleName = $this->getModuleName();
  93. }
  94. return !$this->_scopeConfig->isSetFlag(
  95. 'advanced/modules_disable_output/' . $moduleName,
  96. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  97. );
  98. }
  99. /**
  100. * Make this public so that templates can use it properly with template engine
  101. *
  102. * @return \Magento\Framework\AuthorizationInterface
  103. */
  104. public function getAuthorization()
  105. {
  106. return $this->_authorization;
  107. }
  108. /**
  109. * Prepare html output
  110. *
  111. * @return string
  112. */
  113. protected function _toHtml()
  114. {
  115. $this->_eventManager->dispatch('adminhtml_block_html_before', ['block' => $this]);
  116. return parent::_toHtml();
  117. }
  118. /**
  119. * Return toolbar block instance
  120. *
  121. * @return bool|\Magento\Framework\View\Element\BlockInterface
  122. */
  123. public function getToolbar()
  124. {
  125. return $this->getLayout()->getBlock('page.actions.toolbar');
  126. }
  127. }