Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NewCategory.php
Go to the documentation of this file.
1 <?php
13 
17 class NewCategory extends \Magento\Backend\Block\Widget\Form\Generic
18 {
22  protected $_jsonEncoder;
23 
27  protected $_categoryFactory;
28 
37  public function __construct(
38  \Magento\Backend\Block\Template\Context $context,
39  \Magento\Framework\Registry $registry,
40  \Magento\Framework\Data\FormFactory $formFactory,
41  \Magento\Framework\Json\EncoderInterface $jsonEncoder,
42  \Magento\Catalog\Model\CategoryFactory $categoryFactory,
43  array $data = []
44  ) {
45  $this->_jsonEncoder = $jsonEncoder;
46  $this->_categoryFactory = $categoryFactory;
47  parent::__construct($context, $registry, $formFactory, $data);
48  $this->setUseContainer(true);
49  }
50 
56  protected function _prepareForm()
57  {
59  $form = $this->_formFactory->create(['data' => ['id' => 'new_category_form', 'class' => 'admin__scope-old']]);
60  $form->setUseContainer($this->getUseContainer());
61 
62  $form->addField('new_category_messages', 'note', []);
63 
64  $fieldset = $form->addFieldset('new_category_form_fieldset', []);
65 
66  $fieldset->addField(
67  'new_category_name',
68  'text',
69  [
70  'label' => __('Category Name'),
71  'title' => __('Category Name'),
72  'required' => true,
73  'name' => 'new_category_name'
74  ]
75  );
76 
77  $fieldset->addField(
78  'new_category_parent',
79  'select',
80  [
81  'label' => __('Parent Category'),
82  'title' => __('Parent Category'),
83  'required' => true,
84  'options' => $this->_getParentCategoryOptions(),
85  'class' => 'validate-parent-category',
86  'name' => 'new_category_parent',
87  // @codingStandardsIgnoreStart
88  'note' => __(
89  'If there are no custom parent categories, please use the default parent category. ' .
90  'You can reassign the category at any time in ' .
91  '<a href="%1" target="_blank">Products > Categories</a>.',
92  $this->getUrl('catalog/category')
93  )
94  // @codingStandardsIgnoreEnd
95  ]
96  );
97 
98  $this->setForm($form);
99  }
100 
106  protected function _getParentCategoryOptions()
107  {
108  $items = $this->_categoryFactory->create()->getCollection()->addAttributeToSelect(
109  'name'
110  )->addAttributeToSort(
111  'entity_id',
112  'ASC'
113  )->setPageSize(
114  3
115  )->load()->getItems();
116 
117  $result = [];
118  if (count($items) === 2) {
119  $item = array_pop($items);
120  $result = [$item->getEntityId() => $item->getName()];
121  }
122 
123  return $result;
124  }
125 
131  public function getSaveCategoryUrl()
132  {
133  return $this->getUrl('catalog/category/save');
134  }
135 
141  public function getAfterElementHtml()
142  {
143  $widgetOptions = $this->_jsonEncoder->encode(
144  [
145  'suggestOptions' => [
146  'source' => $this->getUrl('catalog/category/suggestCategories'),
147  'valueField' => '#new_category_parent',
148  'className' => 'category-select',
149  'multiselect' => true,
150  'showAll' => true,
151  ],
152  'saveCategoryUrl' => $this->getUrl('catalog/category/save'),
153  ]
154  );
155  //TODO: JavaScript logic should be moved to separate file or reviewed
156  return <<<HTML
157 <script>
158 require(["jquery","mage/mage"],function($) { // waiting for dependencies at first
159  $(function(){ // waiting for page to load to have '#category_ids-template' available
160  $('#new-category').mage('newCategoryDialog', $widgetOptions);
161  });
162 });
163 </script>
164 HTML;
165  }
166 }
setForm(\Magento\Framework\Data\Form $form)
Definition: Form.php:112
if( $form)() ?>< script > require(['jquery' mage mage
Definition: save.phtml:15
__()
Definition: __.php:13
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Data\FormFactory $formFactory, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\Catalog\Model\CategoryFactory $categoryFactory, array $data=[])
Definition: NewCategory.php:37
$items