Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Product.php
Go to the documentation of this file.
1 <?php
13 
18 class Product extends \Magento\Backend\Block\Widget\Container
19 {
23  protected $_typeFactory;
24 
28  protected $_productFactory;
29 
36  public function __construct(
37  \Magento\Backend\Block\Widget\Context $context,
38  \Magento\Catalog\Model\Product\TypeFactory $typeFactory,
39  \Magento\Catalog\Model\ProductFactory $productFactory,
40  array $data = []
41  ) {
42  $this->_productFactory = $productFactory;
43  $this->_typeFactory = $typeFactory;
44 
45  parent::__construct($context, $data);
46  }
47 
53  protected function _prepareLayout()
54  {
55  $addButtonProps = [
56  'id' => 'add_new_product',
57  'label' => __('Add Product'),
58  'class' => 'add',
59  'button_class' => '',
60  'class_name' => \Magento\Backend\Block\Widget\Button\SplitButton::class,
61  'options' => $this->_getAddProductButtonOptions(),
62  ];
63  $this->buttonList->add('add_new', $addButtonProps);
64 
65  return parent::_prepareLayout();
66  }
67 
73  protected function _getAddProductButtonOptions()
74  {
75  $splitButtonOptions = [];
76  $types = $this->_typeFactory->create()->getTypes();
77  uasort(
78  $types,
79  function ($elementOne, $elementTwo) {
80  return ($elementOne['sort_order'] < $elementTwo['sort_order']) ? -1 : 1;
81  }
82  );
83 
84  foreach ($types as $typeId => $type) {
85  $splitButtonOptions[$typeId] = [
86  'label' => __($type['label']),
87  'onclick' => "setLocation('" . $this->_getProductCreateUrl($typeId) . "')",
88  'default' => \Magento\Catalog\Model\Product\Type::DEFAULT_TYPE == $typeId,
89  ];
90  }
91 
92  return $splitButtonOptions;
93  }
94 
101  protected function _getProductCreateUrl($type)
102  {
103  return $this->getUrl(
104  'catalog/*/new',
105  ['set' => $this->_productFactory->create()->getDefaultAttributeSetId(), 'type' => $type]
106  );
107  }
108 
114  public function isSingleStoreMode()
115  {
116  return $this->_storeManager->isSingleStoreMode();
117  }
118 }
__()
Definition: __.php:13
__construct(\Magento\Backend\Block\Widget\Context $context, \Magento\Catalog\Model\Product\TypeFactory $typeFactory, \Magento\Catalog\Model\ProductFactory $productFactory, array $data=[])
Definition: Product.php:36
$type
Definition: item.phtml:13