Skip to content

Commit 06ceb70

Browse files
committed
Code improvements for crudEAV templates generation
1 parent aa727bf commit 06ceb70

File tree

14 files changed

+317
-206
lines changed

14 files changed

+317
-206
lines changed

templates/crudEAV/Block/Adminhtml/${Entityname}/Edit/DeleteButton.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public function getButtonData()
5656
'data_attribute' => [
5757
'url' => $this->getDeleteUrl()
5858
],
59-
'on_click' => 'deleteConfirm(\'' . __("Are you sure you want to do this?") . '\', \'' . $this->getDeleteUrl() . '\')',
59+
'on_click' =>
60+
'deleteConfirm(\'' . __("Are you sure you want to do this?") . '\', \'' . $this->getDeleteUrl() . '\')',
6061
'sort_order' => 20,
6162
];
6263
return $data;

templates/crudEAV/Controller/Adminhtml/${Entityname}/Delete.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class Delete extends Action
2121
* @param ${Entityname}Factory $objectFactory
2222
*/
2323
public function __construct(
24-
Context $context,
25-
${Entityname}Factory $objectFactory
24+
Context $context,
25+
${Entityname}Factory $objectFactory
2626
) {
2727
$this->objectFactory = $objectFactory;
2828
parent::__construct($context);

templates/crudEAV/Controller/Adminhtml/${Entityname}/InlineEdit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public function execute()
6464
try {
6565
$this->objectCollection
6666
->setStoreId($this->getRequest()->getParam('store', 0))
67-
->addFieldToFilter('entity_id', array('in' => array_keys($postItems)))
68-
->walk('saveCollection', array($postItems));
67+
->addFieldToFilter('entity_id', ['in' => array_keys($postItems)])
68+
->walk('saveCollection', [$postItems]);
6969
} catch (\Exception $e) {
7070
$messages[] = __('There was an error saving the data: ') . $e->getMessage();
7171
$error = true;

templates/crudEAV/Model/ResourceModel/${Entityname}.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ public function getStoreId()
8484
return $this->_storeId;
8585
}
8686

87-
8887
/**
8988
* Set Attribute values to be saved
9089
*

templates/crudEAV/Model/ResourceModel/${Entityname}/Collection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
use Magento\Store\Model\StoreManagerInterface;
2323
use Psr\Log\LoggerInterface;
2424

25+
/**
26+
* Class Collection
27+
* @package ${Vendorname}\${Modulename}\Model\ResourceModel\${Entityname}
28+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
29+
*/
2530
class Collection extends AbstractCollection
2631
{
2732
/**

templates/crudEAV/Model/ResourceModel/${Entityname}/Grid/Collection.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626
/**
2727
* Class Collection
28-
* Collection for displaying grid
28+
* @package ${Vendorname}\${Modulename}\Model\ResourceModel\${Entityname}\Grid
29+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2930
*/
3031
class Collection extends ${Entityname}Collection implements SearchResultInterface
3132
{
@@ -69,9 +70,21 @@ public function __construct(
6970
$eventObject,
7071
$resourceModel,
7172
$model = '${Vendorname}\${Modulename}\Ui\Component\Listing\DataProvider\Document',
72-
AdapterInterface $connection = null)
73-
{
74-
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $eavConfig, $resource, $eavEntityFactory, $resourceHelper, $universalFactory, $storeManager, $connection);
73+
AdapterInterface $connection = null
74+
) {
75+
parent::__construct(
76+
$entityFactory,
77+
$logger,
78+
$fetchStrategy,
79+
$eventManager,
80+
$eavConfig,
81+
$resource,
82+
$eavEntityFactory,
83+
$resourceHelper,
84+
$universalFactory,
85+
$storeManager,
86+
$connection
87+
);
7588
$this->_eventPrefix = $eventPrefix;
7689
$this->_eventObject = $eventObject;
7790
$this->_init($model, $resourceModel);

templates/fileEAVAttribute/Controller/Adminhtml/${Entityname}/Save.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Backend\App\Action;
1111
use Magento\Backend\App\Action\Context;
12+
use ${Vendorname}\${Modulename}\Model\${Entityname}\Attribute\Backend\ImageFactory;
1213
use ${Vendorname}\${Modulename}\Model\${Entityname}Factory;
1314

1415
class Save extends Action
@@ -58,7 +59,8 @@ public function execute()
5859
$objectInstance->load($data['entity_id']);
5960
$params['entity_id'] = $data['entity_id'];
6061
}
61-
$this->${fileattributename}Preprocessing($data);
62+
$imageData = $this->preparedImagesData($data);
63+
$data = array_merge($data, $imageData);
6264
$objectInstance->addData($data);
6365

6466
$this->_eventManager->dispatch(
@@ -87,19 +89,16 @@ public function execute()
8789
return $resultRedirect->setPath('*/*/');
8890
}
8991

90-
/**
91-
* ${Fileattributename} data preprocessing
92-
*
93-
* @param array $data
94-
*
95-
* @return array
96-
*/
97-
protected function ${fileattributename}Preprocessing(&$data)
92+
protected function preparedImagesData(array $data): array
9893
{
99-
if (empty($data['${fileattributename}'])) {
100-
unset($data['${fileattributename}']);
101-
$data['${fileattributename}']['delete'] = true;
94+
$imagesData = [];
95+
$imageAttributeCodes = array_keys(ImageFactory::IMAGE_ATTRIBUTE_CODES);
96+
foreach ($imageAttributeCodes as $imageAttrCode) {
97+
if (empty($data[$imageAttrCode])) {
98+
$imagesData[$imageAttrCode]['delete'] = true;
99+
}
102100
}
101+
return $imagesData;
103102
}
104103

105104
}

templates/fileEAVAttribute/Model/${Entityname}.php

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,29 @@
1515
use Magento\Framework\Model\ResourceModel\AbstractResource;
1616
use Magento\Framework\Data\Collection\AbstractDb;
1717
use Magento\Framework\Registry;
18-
use ${Vendorname}\${Modulename}\Model\${Entityname}\Attribute\Backend\${Fileattributename}Factory;
18+
use ${Vendorname}\${Modulename}\Model\${Entityname}\Attribute\Backend\ImageAbstract;
19+
use ${Vendorname}\${Modulename}\Model\${Entityname}\Attribute\Backend\ImageFactory;
1920

2021
class ${Entityname} extends AbstractModel implements IdentityInterface
2122
{
2223
/**
2324
* CMS page cache tag
2425
*/
2526
const CACHE_TAG = '${vendorname}_${modulename}_${entityname}';
26-
2727
/**
2828
* @var string
2929
*/
3030
protected $_cacheTag = '${vendorname}_${modulename}_${entityname}';
31-
3231
/**
3332
* Prefix of model events names
3433
*
3534
* @var string
3635
*/
3736
protected $_eventPrefix = '${vendorname}_${modulename}_${entityname}';
38-
3937
/**
40-
* @var ${Fileattributename}Factory
38+
* @var ImageFactory
4139
*/
42-
private $${fileattributename}Factory;
40+
private $imageFactory;
4341

4442
/**
4543
* Initialize resource model
@@ -52,17 +50,8 @@ protected function _construct()
5250
$this->_init('${Vendorname}\${Modulename}\Model\ResourceModel\${Entityname}');
5351
}
5452

55-
/**
56-
* Reference constructor.
57-
* @param ${Fileattributename}Factory $${fileattributename}Factory
58-
* @param Context $context
59-
* @param Registry $registry
60-
* @param AbstractResource|null $resource
61-
* @param AbstractDb|null $resourceCollection
62-
* @param array $data
63-
*/
6453
public function __construct(
65-
${Fileattributename}Factory $${fileattributename}Factory,
54+
ImageFactory $imageFactory,
6655
Context $context,
6756
Registry $registry,
6857
AbstractResource $resource = null,
@@ -76,7 +65,7 @@ public function __construct(
7665
$resourceCollection,
7766
$data
7867
);
79-
$this->${fileattributename}Factory = $${fileattributename}Factory;
68+
$this->imageFactory = $imageFactory;
8069
}
8170

8271
/**
@@ -104,26 +93,22 @@ public function saveCollection(array $data)
10493
return $this;
10594
}
10695

107-
/**
108-
* Get ${Fileattributename} in right format to edit in admin form
109-
*
110-
* @return array
111-
*/
112-
public function get${Fileattributename}ValueForForm()
96+
public function getImageValueForForm(string $imageAttrCode): array
11397
{
114-
$${fileattributename} = $this->${fileattributename}Factory->create();
115-
return $${fileattributename}->getFileValueForForm($this);
98+
/** @var ImageAbstract $image */
99+
$image = $this->imageFactory->create($imageAttrCode);
100+
return $image->getFileValueForForm($this);
116101
}
117102

118103
/**
119-
* Get ${Fileattributename} Src to display in frontend
120-
*
104+
* @param string $imageAttrCode
121105
* @return mixed
122106
*/
123-
public function get${Fileattributename}Src()
107+
public function getMainImageSrc(string $imageAttrCode)
124108
{
125-
$${fileattributename} = $this->${fileattributename}Factory->create();
126-
return $${fileattributename}->getFileInfo($this)->getUrl();
109+
/** @var ImageAbstract $image */
110+
$image = $this->imageFactory->create($imageAttrCode);
111+
return $image->getFileInfo($this)->getUrl();
127112
}
128113

129114
}

templates/fileEAVAttribute/Model/${Entityname}/Attribute/Backend/${Fileattributename}.php

Lines changed: 10 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -8,150 +8,21 @@
88

99
namespace ${Vendorname}\${Modulename}\Model\${Entityname}\Attribute\Backend;
1010

11-
use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend;
12-
use Magento\Framework\DataObject;
13-
use ${Vendorname}\${Modulename}\Helper\FileProcessor;
14-
15-
class ${Fileattributename} extends AbstractBackend
11+
class ${Fileattributename} extends ImageAbstract
1612
{
1713
/**
1814
* @var string
1915
*/
20-
const FILES_SUBDIR = '${fileattributename}';
21-
22-
/**
23-
* @var FileProcessor
24-
*/
25-
protected $fileProcessor;
26-
27-
/**
28-
* @param FileProcessor $fileProcessor
29-
*/
30-
public function __construct(FileProcessor $fileProcessor)
31-
{
32-
$this->fileProcessor = $fileProcessor;
33-
}
34-
35-
/**
36-
* Prepare File data before saving object
37-
*
38-
* @param \Magento\Framework\DataObject $object
39-
*
40-
* @return $this
41-
* @throws \Exception
42-
*/
43-
public function beforeSave($object) //@codingStandardsIgnoreLine
44-
{
45-
parent::beforeSave($object);
46-
$file = $object->get${Fileattributename}();
47-
if (!is_array($file)) {
48-
$object->setSkipSave${Fileattributename}(true);
49-
return $this;
50-
}
51-
52-
if (isset($file['delete'])) {
53-
$object->set${Fileattributename}(null);
54-
return $this;
55-
}
56-
57-
$file = reset($file) ?: [];
58-
if (!isset($file['file'])) {
59-
throw new LocalizedException(
60-
__('${Fileattributename} does not contain field \'file\'')
61-
);
62-
}
63-
// Add file related data to object
64-
$object->set${Fileattributename}($file['file']);
65-
$object->setFileExists(isset($file['exists']));
66-
67-
return $this;
68-
}
69-
70-
/**
71-
* Save uploaded file and remove temporary file after saving object
72-
*
73-
* @param \Magento\Framework\DataObject $object
74-
*
75-
* @return $this
76-
* @throws \Exception
77-
*/
78-
public function afterSave($object) //@codingStandardsIgnoreLine
79-
{
80-
parent::afterSave($object);
81-
// if file already exists we do not need to save any new file
82-
if ($object->getFileExists() || $object->getSkipSave${Fileattributename}()) {
83-
return $this;
84-
}
16+
const ATTRIBUTE_CODE = '${fileattributename}';
8517

86-
// Delete old file if new one has changed
87-
if ($object->getOrigData('${fileattributename}') && $object->get${Fileattributename}() != $object->getOrigData('${fileattributename}')) {
88-
$this->fileProcessor->delete($this->getFileSubDir($object), $object->getOrigData('${fileattributename}'));
89-
}
90-
91-
if ($object->get${Fileattributename}()) {
92-
if (!$this->fileProcessor->saveFileFromTmp($object->get${Fileattributename}(), $this->getFileSubDir($object))) {
93-
throw new \Exception('There was an error saving the file');
94-
}
95-
}
96-
}
97-
98-
/**
99-
* Subdir where files are stored
100-
*
101-
* @param \Magento\Framework\DataObject $object
102-
* @return string
103-
*/
104-
protected function getFileSubDir($object)
105-
{
106-
return self::FILES_SUBDIR . '/' . $object->getId();
107-
}
108-
109-
/**
110-
* Delete media file before an ${fileattributename} row in database is removed
111-
* @param \Magento\Framework\DataObject $object
112-
* @return $this
113-
*/
114-
public function beforeDelete($object) //@codingStandardsIgnoreLine
115-
{
116-
parent::beforeDelete($object);
117-
// Delete file from disk before the object is deleted from database
118-
if ($object->get${Fileattributename}()) {
119-
$this->fileProcessor->delete($this->getFileSubDir($object), $object->get${Fileattributename}());
120-
}
121-
return $this;
122-
}
123-
124-
/**
125-
* Get full info from file
126-
*
127-
* @param \Magento\Framework\DataObject $object
128-
* @return DataObject
129-
*/
130-
public function getFileInfo($object)
131-
{
132-
if (!$object->getData('file_info') && $object->get${Fileattributename}()) {
133-
$fileInfoObject = new DataObject();
134-
$fileInfo = $this->fileProcessor->getFileInfo($object->get${Fileattributename}(), $this->getFileSubDir($object));
135-
if ($fileInfo) {
136-
$fileInfoObject->setData($fileInfo);
137-
}
138-
$object->setFileInfo($fileInfoObject);
139-
}
18+
protected function subdirName(): string
19+
{
20+
return self::ATTRIBUTE_CODE;
21+
}
14022

141-
return $object->getData('file_info');
142-
}
23+
protected function attributeCode(): string
24+
{
25+
return self::ATTRIBUTE_CODE;
26+
}
14327

144-
/**
145-
* Return file info in a format valid for ui form fields
146-
*
147-
* @param \Magento\Framework\DataObject $object
148-
* @return array
149-
*/
150-
public function getFileValueForForm($object)
151-
{
152-
if ($this->getFileInfo($object)->getFile()) {
153-
return [$this->getFileInfo($object)->getData()];
154-
}
155-
return [];
156-
}
15728
}

0 commit comments

Comments
 (0)