Content.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Copyright © 2015 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Block\Adminhtml\Post\Edit\Tab;
  9. /**
  10. * Admin blog post edit form content tab
  11. */
  12. class Content extends \Magento\Backend\Block\Widget\Form\Generic implements
  13. \Magento\Backend\Block\Widget\Tab\TabInterface
  14. {
  15. /**
  16. * @var \Magento\Cms\Model\Wysiwyg\Config
  17. */
  18. protected $_wysiwygConfig;
  19. /**
  20. * @param \Magento\Backend\Block\Template\Context $context
  21. * @param \Magento\Framework\Registry $registry
  22. * @param \Magento\Framework\Data\FormFactory $formFactory
  23. * @param \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig
  24. * @param array $data
  25. */
  26. public function __construct(
  27. \Magento\Backend\Block\Template\Context $context,
  28. \Magento\Framework\Registry $registry,
  29. \Magento\Framework\Data\FormFactory $formFactory,
  30. \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
  31. array $data = []
  32. ) {
  33. $this->_wysiwygConfig = $wysiwygConfig;
  34. parent::__construct($context, $registry, $formFactory, $data);
  35. }
  36. /**
  37. * Prepare form
  38. *
  39. * @return $this
  40. */
  41. protected function _prepareForm()
  42. {
  43. /** @var $model \Magefan\Blog\Model\Post */
  44. $model = $this->_coreRegistry->registry('current_model');
  45. /*
  46. * Checking if user have permissions to save information
  47. */
  48. $isElementDisabled = !$this->_isAllowedAction('Magefan_Blog::post');
  49. /** @var \Magento\Framework\Data\Form $form */
  50. $form = $this->_formFactory->create();
  51. $form->setHtmlIdPrefix('post_');
  52. $fieldset = $form->addFieldset(
  53. 'content_fieldset',
  54. ['legend' => __('Content'), 'class' => 'fieldset-wide']
  55. );
  56. $wysiwygConfig = $this->_wysiwygConfig->getConfig(['tab_id' => $this->getTabId()]);
  57. $fieldset->addField(
  58. 'content_heading',
  59. 'text',
  60. [
  61. 'name' => 'post[content_heading]',
  62. 'label' => __('Content Heading'),
  63. 'title' => __('Content Heading'),
  64. 'disabled' => $isElementDisabled
  65. ]
  66. );
  67. $contentField = $fieldset->addField(
  68. 'content',
  69. 'editor',
  70. [
  71. 'name' => 'post[content]',
  72. 'style' => 'height:36em;',
  73. 'required' => true,
  74. 'disabled' => $isElementDisabled,
  75. 'config' => $wysiwygConfig
  76. ]
  77. );
  78. // Setting custom renderer for content field to remove label column
  79. $renderer = $this->getLayout()->createBlock(
  80. 'Magento\Backend\Block\Widget\Form\Renderer\Fieldset\Element'
  81. )->setTemplate(
  82. 'Magento_Cms::page/edit/form/renderer/content.phtml'
  83. );
  84. $contentField->setRenderer($renderer);
  85. $this->_eventManager->dispatch('magefan_blog_post_edit_tab_content_prepare_form', ['form' => $form]);
  86. $form->setValues($model->getData());
  87. $this->setForm($form);
  88. return parent::_prepareForm();
  89. }
  90. /**
  91. * Prepare label for tab
  92. *
  93. * @return \Magento\Framework\Phrase
  94. */
  95. public function getTabLabel()
  96. {
  97. return __('Content');
  98. }
  99. /**
  100. * Prepare title for tab
  101. *
  102. * @return \Magento\Framework\Phrase
  103. */
  104. public function getTabTitle()
  105. {
  106. return __('Content');
  107. }
  108. /**
  109. * Returns status flag about this tab can be shown or not
  110. *
  111. * @return bool
  112. */
  113. public function canShowTab()
  114. {
  115. return true;
  116. }
  117. /**
  118. * Returns status flag about this tab hidden or not
  119. *
  120. * @return bool
  121. */
  122. public function isHidden()
  123. {
  124. return false;
  125. }
  126. /**
  127. * Check permission for passed action
  128. *
  129. * @param string $resourceId
  130. * @return bool
  131. */
  132. protected function _isAllowedAction($resourceId)
  133. {
  134. return $this->_authorization->isAllowed($resourceId);
  135. }
  136. }