Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GetSelected.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
12 
15 {
21  const ADMIN_RESOURCE = 'Magento_Catalog::products';
22 
26  private $resultJsonFactory;
27 
31  private $productCollectionFactory;
32 
39  public function __construct(
40  \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
41  \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
42  \Magento\Backend\App\Action\Context $context
43  ) {
44  $this->resultJsonFactory = $jsonFactory;
45  $this->productCollectionFactory = $productCollectionFactory;
46  parent::__construct($context);
47  }
48 
52  public function execute() : \Magento\Framework\Controller\ResultInterface
53  {
54  $productId = $this->getRequest()->getParam('productId');
56  $productCollection = $this->productCollectionFactory->create();
57  $productCollection->addAttributeToSelect(ProductInterface::NAME);
58  $productCollection->addIdFilter($productId);
59  $option = [];
61  if (!empty($productCollection->getFirstItem()->getData())) {
62  $product = $productCollection->getFirstItem();
63  $option = [
64  'value' => $productId,
65  'label' => $product->getName(),
66  'is_active' => $product->getStatus(),
67  'path' => $product->getSku(),
68  ];
69  }
71  $resultJson = $this->resultJsonFactory->create();
72  return $resultJson->setData($option);
73  }
74 }
__construct(\Magento\Framework\Controller\Result\JsonFactory $jsonFactory, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Backend\App\Action\Context $context)
Definition: GetSelected.php:39