diff --git a/templates/crudEAVWithFile/.no-copied-config/after-generate-info.txt b/templates/crudEAVWithFile/.no-copied-config/after-generate-info.txt new file mode 100644 index 0000000..e7ae95c --- /dev/null +++ b/templates/crudEAVWithFile/.no-copied-config/after-generate-info.txt @@ -0,0 +1,53 @@ +1. Add your STATIC fields on: + + ${Vendorname}/${Modulename}/Setup/InstallSchema.php + +2. Add ENTITY EAV Attributes on: + + ${Vendorname}/${Modulename}/Setup/${Entityname}Setup.php + +3. Add your custom columns to the grid: + + ${Vendorname}/${Modulename}/view/adminhtml/ui_component/${vendorname}_${modulename}_${entityname}_listing.xml + +4. Add your custom fields to the form: + + ${Vendorname}/${Modulename}/view/adminhtml/ui_component/${vendorname}_${modulename}_${entityname}_form.xml + +5. Add Attributes to display in Grid on the Listing DataProvider: + + ${Vendorname}\${Modulename}\Ui\Component\Listing\DataProvider + + /** + * @param SearchResultInterface $searchResult + * @return array + */ + protected function searchResultToOutput(SearchResultInterface $searchResult) + { + $searchResult->setStoreId($this->request->getParam('store', 0)) + ->addAttributeToSelect([]); // Add here needed EAV attributes to display on grid + + return parent::searchResultToOutput($searchResult); + } + +6. Set the Admin Menu tab where you want your Module can be found: + + ${Vendorname}/${Modulename}/etc/adminhtml/menu.xml + +7. Set From server side Validations: + + ${Vendorname}\${Modulename}\Controller\Adminhtml\${Entityname}\Validate: + + /** + * Check if required fields is not empty + * + * @param array $data + */ + public function validateRequireEntries(array $data) + { + $requiredFields = [ + 'identifier' => __('${Entityname} Identifier'), + ]; + + //... + } \ No newline at end of file diff --git a/templates/crudEAVWithFile/.no-copied-config/config.yml b/templates/crudEAVWithFile/.no-copied-config/config.yml new file mode 100644 index 0000000..dc17d4d --- /dev/null +++ b/templates/crudEAVWithFile/.no-copied-config/config.yml @@ -0,0 +1,4 @@ +dependencies: + 0: crudEAV + 1: fileEAVAttribute + 2: fileProcessor \ No newline at end of file diff --git a/templates/crudEAVWithFile/.no-copied-config/description.txt b/templates/crudEAVWithFile/.no-copied-config/description.txt new file mode 100644 index 0000000..d23d697 --- /dev/null +++ b/templates/crudEAVWithFile/.no-copied-config/description.txt @@ -0,0 +1 @@ +This Template creates an EAV crud module using uiComponents for Grid and Form. It includes a file attribute \ No newline at end of file diff --git a/templates/crudEAVWithMultipleFiles/.no-copied-config/after-generate-info.txt b/templates/crudEAVWithMultipleFiles/.no-copied-config/after-generate-info.txt new file mode 100644 index 0000000..e7ae95c --- /dev/null +++ b/templates/crudEAVWithMultipleFiles/.no-copied-config/after-generate-info.txt @@ -0,0 +1,53 @@ +1. Add your STATIC fields on: + + ${Vendorname}/${Modulename}/Setup/InstallSchema.php + +2. Add ENTITY EAV Attributes on: + + ${Vendorname}/${Modulename}/Setup/${Entityname}Setup.php + +3. Add your custom columns to the grid: + + ${Vendorname}/${Modulename}/view/adminhtml/ui_component/${vendorname}_${modulename}_${entityname}_listing.xml + +4. Add your custom fields to the form: + + ${Vendorname}/${Modulename}/view/adminhtml/ui_component/${vendorname}_${modulename}_${entityname}_form.xml + +5. Add Attributes to display in Grid on the Listing DataProvider: + + ${Vendorname}\${Modulename}\Ui\Component\Listing\DataProvider + + /** + * @param SearchResultInterface $searchResult + * @return array + */ + protected function searchResultToOutput(SearchResultInterface $searchResult) + { + $searchResult->setStoreId($this->request->getParam('store', 0)) + ->addAttributeToSelect([]); // Add here needed EAV attributes to display on grid + + return parent::searchResultToOutput($searchResult); + } + +6. Set the Admin Menu tab where you want your Module can be found: + + ${Vendorname}/${Modulename}/etc/adminhtml/menu.xml + +7. Set From server side Validations: + + ${Vendorname}\${Modulename}\Controller\Adminhtml\${Entityname}\Validate: + + /** + * Check if required fields is not empty + * + * @param array $data + */ + public function validateRequireEntries(array $data) + { + $requiredFields = [ + 'identifier' => __('${Entityname} Identifier'), + ]; + + //... + } \ No newline at end of file diff --git a/templates/crudEAVWithMultipleFiles/.no-copied-config/config.yml b/templates/crudEAVWithMultipleFiles/.no-copied-config/config.yml new file mode 100644 index 0000000..c30aabf --- /dev/null +++ b/templates/crudEAVWithMultipleFiles/.no-copied-config/config.yml @@ -0,0 +1,6 @@ +dependencies: + 0: crudEAV + 1: model + 2: fileModel + 3: fileEAVMultiple + 4: fileProcessor diff --git a/templates/crudEAVWithMultipleFiles/.no-copied-config/description.txt b/templates/crudEAVWithMultipleFiles/.no-copied-config/description.txt new file mode 100644 index 0000000..fd42684 --- /dev/null +++ b/templates/crudEAVWithMultipleFiles/.no-copied-config/description.txt @@ -0,0 +1 @@ +This Template creates an EAV crud module using uiComponents for Grid and Form. It includes Dynamic Multiple Files \ No newline at end of file diff --git a/templates/eavFileAttribute/.no-copied-config/after-generate-info.txt b/templates/eavFileAttribute/.no-copied-config/after-generate-info.txt deleted file mode 100644 index b328822..0000000 --- a/templates/eavFileAttribute/.no-copied-config/after-generate-info.txt +++ /dev/null @@ -1,115 +0,0 @@ -1.- Create Image attribute in your Setup Script - - ${Vendorname}/${Modulename}/Setup/${Entityname}Setup.php - - - $attributes['${attributename}'] = [ - 'type' => 'varchar', - 'label' => '${Attributename}', - 'input' => 'image', - 'backend' => '${Vendorname}\${Modulename}\Model\${Entityname}\Attribute\Backend\${Attributename}', - 'required' => false, - 'sort_order' => 99, - 'global' => ScopedAttributeInterface::SCOPE_GLOBAL, - 'group' => 'General', - ]; - - -2.- Save image on your Save action controller - - ${Vendorname}/${Modulename}/Controller/Adminhtml/${Entityname}/Save.php: - - /** - * ${Attributename} data preprocessing - * - * @param array $data - * - * @return array - */ - protected function ${attributename}Preprocessing($data) - { - if (empty($data['${attributename}'])) { - unset($data['${attributename}']); - $data['${attributename}']['delete'] = true; - } - return $data; - } - - -3.- Add methods to handle files on the main entity Model - - - ${Vendorname}/${Modulename}/Model/${Entityname}.php - - - use ${Vendorname}\${Modulename}\Model\${Entityname}\Attribute\Backend\${Attributename}Factory; - - /** - * Get ${Attributename} in right format to edit in admin form - * - * @return array - */ - public function get${Attributename}ValueForForm() - { - $${attributename} = $this->${attributename}Factory->create(); - return $${attributename}->getFileValueForForm($this); - } - - /** - * Get ${Attributename} Src to display in frontend - * - * @return mixed - */ - public function get${Attributename}Src() - { - $${attributename} = $this->${attributename}Factory->create(); - return $${attributename}->getFileInfo($this)->getUrl(); - } - -4. Add the formElement into your uiComponent form: - - ${Vendorname}/${Modulename}/view/adminhtml/ui_component/${vendorname}_${modulename}_${entityname}_form.xml: - - - - - ${Attributename} - fileUploader - fileUploader - Allowed file types: jpeg, gif, png - 2097152 - jpg jpeg gif png - - ${vendorname}_${modulename}/file/uploader - - 99 - [GLOBAL] - - - - -5.- Get right File format for the form in your DataProvider - - ${Vendorname}/${Modulename}/Ui/Component/Form/${Entityname}/DataProvider.php - - /** - * Get data - * - * @return array - */ - public function getData() - { - // ... - foreach ($items as $item) { - if ($item->get${Attributename}()) { - $item->set${Attributename}($item->get${Attributename}ValueForForm()); - } - $item->setStoreId($storeId); - $this->loadedData[$item->getId()] = $item->getData(); - break; - } - } - return $this->loadedData; - } - - diff --git a/templates/eavFileAttribute/.no-copied-config/config.yml b/templates/fileEAVAttribute/.no-copied-config/config.yml similarity index 100% rename from templates/eavFileAttribute/.no-copied-config/config.yml rename to templates/fileEAVAttribute/.no-copied-config/config.yml diff --git a/templates/eavFileAttribute/.no-copied-config/description.txt b/templates/fileEAVAttribute/.no-copied-config/description.txt similarity index 100% rename from templates/eavFileAttribute/.no-copied-config/description.txt rename to templates/fileEAVAttribute/.no-copied-config/description.txt diff --git a/templates/fileEAVAttribute/Controller/Adminhtml/${Entityname}/Save.php b/templates/fileEAVAttribute/Controller/Adminhtml/${Entityname}/Save.php new file mode 100644 index 0000000..c08b634 --- /dev/null +++ b/templates/fileEAVAttribute/Controller/Adminhtml/${Entityname}/Save.php @@ -0,0 +1,105 @@ +objectFactory = $objectFactory; + parent::__construct($context); + } + + /** + * {@inheritdoc} + */ + protected function _isAllowed() + { + return $this->_authorization->isAllowed('${Vendorname}_${Modulename}::${entityname}'); + } + + /** + * Save action + * + * @return \Magento\Framework\Controller\ResultInterface + */ + public function execute() + { + $storeId = (int)$this->getRequest()->getParam('store_id'); + $data = $this->getRequest()->getParams(); + /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ + $resultRedirect = $this->resultRedirectFactory->create(); + if ($data) { + $params = []; + $objectInstance = $this->objectFactory->create(); + $objectInstance->setStoreId($storeId); + $params['store'] = $storeId; + if (empty($data['entity_id'])) { + $data['entity_id'] = null; + } else { + $objectInstance->load($data['entity_id']); + $params['entity_id'] = $data['entity_id']; + } + $this->${fileattributename}Preprocessing($data); + $objectInstance->addData($data); + + $this->_eventManager->dispatch( + '${vendorname}_${modulename}_${entityname}_prepare_save', + ['object' => $this->objectFactory, 'request' => $this->getRequest()] + ); + + try { + $objectInstance->save(); + $this->messageManager->addSuccessMessage(__('You saved this record.')); + $this->_getSession()->setFormData(false); + if ($this->getRequest()->getParam('back')) { + $params['entity_id'] = $objectInstance->getId(); + $params['_current'] = true; + return $resultRedirect->setPath('*/*/edit', $params); + } + return $resultRedirect->setPath('*/*/'); + } catch (\Exception $e) { + $this->messageManager->addErrorMessage($e->getMessage()); + $this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the record.')); + } + + $this->_getSession()->setFormData($this->getRequest()->getPostValue()); + return $resultRedirect->setPath('*/*/edit', $params); + } + return $resultRedirect->setPath('*/*/'); + } + + /** + * ${Fileattributename} data preprocessing + * + * @param array $data + * + * @return array + */ + protected function ${fileattributename}Preprocessing(&$data) + { + if (empty($data['${fileattributename}'])) { + unset($data['${fileattributename}']); + $data['${fileattributename}']['delete'] = true; + } + } + +} diff --git a/templates/fileEAVAttribute/Model/${Entityname}.php b/templates/fileEAVAttribute/Model/${Entityname}.php new file mode 100644 index 0000000..b292c6c --- /dev/null +++ b/templates/fileEAVAttribute/Model/${Entityname}.php @@ -0,0 +1,124 @@ +_init('${Vendorname}\${Modulename}\Model\ResourceModel\${Entityname}'); + } + + /** + * Reference constructor. + * @param ${Fileattributename}Factory $${fileattributename}Factory + * @param Context $context + * @param Registry $registry + * @param AbstractResource|null $resource + * @param AbstractDb|null $resourceCollection + * @param array $data + */ + public function __construct( + ${Fileattributename}Factory $${fileattributename}Factory, + Context $context, + Registry $registry, + AbstractResource $resource = null, + AbstractDb $resourceCollection = null, + array $data = [] + ) { + parent::__construct( + $context, + $registry, + $resource, + $resourceCollection, + $data + ); + $this->${fileattributename}Factory = $${fileattributename}Factory; + } + + /** + * Get identities + * + * @return array + */ + public function getIdentities() + { + return [self::CACHE_TAG . '_' . $this->getId()]; + } + + /** + * Save from collection data + * + * @param array $data + * @return $this|bool + */ + public function saveCollection(array $data) + { + if (isset($data[$this->getId()])) { + $this->addData($data[$this->getId()]); + $this->getResource()->save($this); + } + return $this; + } + + /** + * Get ${Fileattributename} in right format to edit in admin form + * + * @return array + */ + public function get${Fileattributename}ValueForForm() + { + $${fileattributename} = $this->${fileattributename}Factory->create(); + return $${fileattributename}->getFileValueForForm($this); + } + + /** + * Get ${Fileattributename} Src to display in frontend + * + * @return mixed + */ + public function get${Fileattributename}Src() + { + $${fileattributename} = $this->${fileattributename}Factory->create(); + return $${fileattributename}->getFileInfo($this)->getUrl(); + } + +} diff --git a/templates/eavFileAttribute/Model/${Entityname}/Attribute/Backend/${Attributename}.php b/templates/fileEAVAttribute/Model/${Entityname}/Attribute/Backend/${Fileattributename}.php similarity index 75% rename from templates/eavFileAttribute/Model/${Entityname}/Attribute/Backend/${Attributename}.php rename to templates/fileEAVAttribute/Model/${Entityname}/Attribute/Backend/${Fileattributename}.php index d88ad45..475b074 100644 --- a/templates/eavFileAttribute/Model/${Entityname}/Attribute/Backend/${Attributename}.php +++ b/templates/fileEAVAttribute/Model/${Entityname}/Attribute/Backend/${Fileattributename}.php @@ -1,6 +1,6 @@ get${Attributename}(); + $file = $object->get${Fileattributename}(); if (!is_array($file)) { - $object->setSkipSave${Attributename}(true); + $object->setSkipSave${Fileattributename}(true); return $this; } if (isset($file['delete'])) { - $object->set${Attributename}(null); + $object->set${Fileattributename}(null); return $this; } $file = reset($file) ?: []; if (!isset($file['file'])) { throw new LocalizedException( - __('${Attributename} does not contain field \'file\'') + __('${Fileattributename} does not contain field \'file\'') ); } // Add file related data to object - $object->set${Attributename}($file['file']); + $object->set${Fileattributename}($file['file']); $object->setFileExists(isset($file['exists'])); return $this; @@ -79,17 +79,17 @@ public function afterSave($object) //@codingStandardsIgnoreLine { parent::afterSave($object); // if file already exists we do not need to save any new file - if ($object->getFileExists() || $object->getSkipSave${Attributename}()) { + if ($object->getFileExists() || $object->getSkipSave${Fileattributename}()) { return $this; } // Delete old file if new one has changed - if ($object->getOrigData('${attributename}') && $object->get${Attributename}() != $object->getOrigData('${attributename}')) { - $this->fileProcessor->delete($this->getFileSubDir($object), $object->getOrigData('${attributename}')); + if ($object->getOrigData('${fileattributename}') && $object->get${Fileattributename}() != $object->getOrigData('${fileattributename}')) { + $this->fileProcessor->delete($this->getFileSubDir($object), $object->getOrigData('${fileattributename}')); } - if ($object->get${Attributename}()) { - if (!$this->fileProcessor->saveFileFromTmp($object->get${Attributename}(), $this->getFileSubDir($object))) { + if ($object->get${Fileattributename}()) { + if (!$this->fileProcessor->saveFileFromTmp($object->get${Fileattributename}(), $this->getFileSubDir($object))) { throw new \Exception('There was an error saving the file'); } } @@ -107,7 +107,7 @@ protected function getFileSubDir($object) } /** - * Delete media file before an ${attributename} row in database is removed + * Delete media file before an ${fileattributename} row in database is removed * @param \Magento\Framework\DataObject $object * @return $this */ @@ -115,8 +115,8 @@ public function beforeDelete($object) //@codingStandardsIgnoreLine { parent::beforeDelete($object); // Delete file from disk before the object is deleted from database - if ($object->get${Attributename}()) { - $this->fileProcessor->delete($this->getFileSubDir($object), $object->get${Attributename}()); + if ($object->get${Fileattributename}()) { + $this->fileProcessor->delete($this->getFileSubDir($object), $object->get${Fileattributename}()); } return $this; } @@ -129,9 +129,9 @@ public function beforeDelete($object) //@codingStandardsIgnoreLine */ public function getFileInfo($object) { - if (!$object->getData('file_info') && $object->get${Attributename}()) { + if (!$object->getData('file_info') && $object->get${Fileattributename}()) { $fileInfoObject = new DataObject(); - $fileInfo = $this->fileProcessor->getFileInfo($object->get${Attributename}(), $this->getFileSubDir($object)); + $fileInfo = $this->fileProcessor->getFileInfo($object->get${Fileattributename}(), $this->getFileSubDir($object)); if ($fileInfo) { $fileInfoObject->setData($fileInfo); } diff --git a/templates/fileEAVAttribute/Setup/${Entityname}Setup.php b/templates/fileEAVAttribute/Setup/${Entityname}Setup.php new file mode 100644 index 0000000..335e54a --- /dev/null +++ b/templates/fileEAVAttribute/Setup/${Entityname}Setup.php @@ -0,0 +1,91 @@ + 'static', + 'label' => 'identifier', + 'input' => 'text', + 'required' => true, + 'sort_order' => 10, + 'global' => ScopedAttributeInterface::SCOPE_GLOBAL, + 'group' => 'General', + 'validate_rules' => 'a:2:{s:15:"max_text_length";i:100;s:15:"min_text_length";i:1;}' + ]; + + $attributes['${fileattributename}'] = [ + 'type' => 'varchar', + 'label' => '${Fileattributename}', + 'input' => 'image', + 'backend' => '${Vendorname}\${Modulename}\Model\${Entityname}\Attribute\Backend\${Fileattributename}', + 'required' => false, + 'sort_order' => 99, + 'global' => ScopedAttributeInterface::SCOPE_GLOBAL, + 'group' => 'General', + ]; + + // Add your entity attributes here... For example: +// $attributes['is_active'] = [ +// 'type' => 'int', +// 'label' => 'Is Active', +// 'input' => 'select', +// 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', +// 'sort_order' => 10, +// 'global' => ScopedAttributeInterface::SCOPE_STORE, +// 'group' => 'General', +// ]; + + return $attributes; + } + + /** + * Retrieve default entities: ${entityname} + * + * @return array + */ + public function getDefaultEntities() + { + $entities = [ + self::ENTITY_TYPE_CODE => [ + 'entity_model' => '${Vendorname}\${Modulename}\Model\ResourceModel\${Entityname}', + 'attribute_model' => '${Vendorname}\${Modulename}\Model\ResourceModel\Eav\Attribute', + 'table' => self::ENTITY_TYPE_CODE . '_entity', + 'increment_model' => null, + 'additional_attribute_table' => self::ENTITY_TYPE_CODE . '_eav_attribute', + 'entity_attribute_collection' => '${Vendorname}\${Modulename}\Model\ResourceModel\Attribute\Collection', + 'attributes' => $this->getAttributes() + ] + ]; + + return $entities; + } +} diff --git a/templates/fileEAVAttribute/Ui/Component/Form/${Entityname}/DataProvider.php b/templates/fileEAVAttribute/Ui/Component/Form/${Entityname}/DataProvider.php new file mode 100644 index 0000000..8ed40e4 --- /dev/null +++ b/templates/fileEAVAttribute/Ui/Component/Form/${Entityname}/DataProvider.php @@ -0,0 +1,89 @@ +collection = $collection; + $this->filterPool = $filterPool; + $this->request = $request; + } + + /** + * Get data + * + * @return array + */ + public function getData() + { + if (!$this->loadedData) { + $storeId = (int)$this->request->getParam('store'); + $this->collection + ->setStoreId($storeId) + ->addAttributeToSelect('*'); + $items = $this->collection->getItems(); + foreach ($items as $item) { + if ($item->get${Fileattributename}()) { + $item->set${Fileattributename}($item->get${Fileattributename}ValueForForm()); + } + $item->setStoreId($storeId); + $this->loadedData[$item->getId()] = $item->getData(); + break; + } + } + return $this->loadedData; + } +} diff --git a/templates/fileEAVAttribute/view/adminhtml/ui_component/${vendorname}_${modulename}_${entityname}_form.xml b/templates/fileEAVAttribute/view/adminhtml/ui_component/${vendorname}_${modulename}_${entityname}_form.xml new file mode 100644 index 0000000..f99f4e3 --- /dev/null +++ b/templates/fileEAVAttribute/view/adminhtml/ui_component/${vendorname}_${modulename}_${entityname}_form.xml @@ -0,0 +1,109 @@ + + +
+ + + ${vendorname}_${modulename}_${entityname}_form.${vendorname}_${modulename}_${entityname}_form_data_source + ${vendorname}_${modulename}_${entityname}_form.${vendorname}_${modulename}_${entityname}_form_data_source + + + data + + templates/form/collapsible + ${Entityname} Form + + ${Vendorname}\${Modulename}\Block\Adminhtml\${Entityname}\Edit\BackButton + ${Vendorname}\${Modulename}\Block\Adminhtml\${Entityname}\Edit\DeleteButton + ${Vendorname}\${Modulename}\Block\Adminhtml\${Entityname}\Edit\SaveButton + ${Vendorname}\${Modulename}\Block\Adminhtml\${Entityname}\Edit\SaveAndContinueButton + + + + + ${Vendorname}\${Modulename}\Ui\Component\Form\${Entityname}\DataProvider + ${vendorname}_${modulename}_${entityname}_form_data_source + entity_id + entity_id + + + + + + + + + + Magento_Ui/js/form/provider + + + +
+ + + Main Information + + + + + + true + number + hidden + + 10 + main_fieldset + + + + + + + false + text + input + main_fieldset + + + + + + + true + text + Identifier + input + main_fieldset + + true + + 10 + [GLOBAL] + + + + + + + ${Fileattributename} + fileUploader + fileUploader + Allowed file types: jpeg, gif, png + 2097152 + jpg jpeg gif png + + ${vendorname}_${modulename}/file/uploader + + 99 + [GLOBAL] + + + + +
+
\ No newline at end of file diff --git a/templates/fileEAVMultiple/.no-copied-config/config.yml b/templates/fileEAVMultiple/.no-copied-config/config.yml new file mode 100644 index 0000000..fdbb924 --- /dev/null +++ b/templates/fileEAVMultiple/.no-copied-config/config.yml @@ -0,0 +1,4 @@ +dependencies: + 0: model + 1: fileModel + 2: fileProcessor \ No newline at end of file diff --git a/templates/fileEAVMultiple/.no-copied-config/description.txt b/templates/fileEAVMultiple/.no-copied-config/description.txt new file mode 100644 index 0000000..9ec1983 --- /dev/null +++ b/templates/fileEAVMultiple/.no-copied-config/description.txt @@ -0,0 +1 @@ +This Template creates the attribute Backend, Ajax and Helpers to have a OneToMany relation between EAV entity and files \ No newline at end of file diff --git a/templates/fileEAVMultiple/Model/${Entityname}.php b/templates/fileEAVMultiple/Model/${Entityname}.php new file mode 100644 index 0000000..339cdc3 --- /dev/null +++ b/templates/fileEAVMultiple/Model/${Entityname}.php @@ -0,0 +1,131 @@ +_init('${Vendorname}\${Modulename}\Model\ResourceModel\${Entityname}'); + } + + /** + * Reference constructor. + * @param ${Simpleentityname}Collection $${simpleentityname}Collection, + * @param ${Simpleentityname}Factory $${simpleentityname}Factory, + * @param Context $context + * @param Registry $registry + * @param AbstractResource|null $resource + * @param AbstractDb|null $resourceCollection + * @param array $data + */ + public function __construct( + ${Simpleentityname}Collection $${simpleentityname}Collection, + ${Simpleentityname}Factory $${simpleentityname}Factory, + Context $context, + Registry $registry, + AbstractResource $resource = null, + AbstractDb $resourceCollection = null, + array $data = [] + ) { + parent::__construct( + $context, + $registry, + $resource, + $resourceCollection, + $data + ); + $this->${simpleentityname}Collection = $${simpleentityname}Collection; + $this->${simpleentityname}Factory = $${simpleentityname}Factory; + } + + /** + * Get identities + * + * @return array + */ + public function getIdentities() + { + return [self::CACHE_TAG . '_' . $this->getId()]; + } + + /** + * Save from collection data + * + * @param array $data + * @return $this|bool + */ + public function saveCollection(array $data) + { + if (isset($data[$this->getId()])) { + $this->addData($data[$this->getId()]); + $this->getResource()->save($this); + } + return $this; + } + + /** + * Save images once the main reference data is saved + * + * @return $this + */ + public function afterSave() //@codingStandardsIgnoreLine + { + parent::afterSave(); + if (is_array($this->getData('dynamic_files'))) { + $fileObject = $this->${simpleentityname}Factory->create(); + $fileObject->saveDynamicFieldFiles($this->getData('dynamic_files'), ['entity_id' => $this->getId()]); + } + + return $this; + } + + /** + * @return Collection + */ + public function getDynamicFiles() + { + return $this->${simpleentityname}Collection + ->addFieldToFilter('entity_id', $this->getId()) + ->addOrder('position', \Magento\Framework\Data\Collection::SORT_ORDER_ASC); + } + +} diff --git a/templates/fileEAVMultiple/Setup/InstallSchema.php b/templates/fileEAVMultiple/Setup/InstallSchema.php new file mode 100644 index 0000000..6652759 --- /dev/null +++ b/templates/fileEAVMultiple/Setup/InstallSchema.php @@ -0,0 +1,146 @@ +eavTablesSetupFactory = $eavTablesSetupFactory; + } + + /** + * {@inheritdoc} + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) //@codingStandardsIgnoreLine + { + $setup->startSetup(); + + $eavTableName = ${Entityname}Setup::ENTITY_TYPE_CODE . '_entity'; + /** + * Create entity Table + */ + $eavTable = $setup->getConnection() + ->newTable($setup->getTable($eavTableName)) + ->addColumn( + 'entity_id', + Table::TYPE_INTEGER, + null, + ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], + 'Entity ID' + )->setComment('Entity Table'); + + $eavTable->addColumn( + 'identifier', + Table::TYPE_TEXT, + 100, + ['nullable' => false], + 'Identifier' + )->addIndex( + $setup->getIdxName($eavTableName, ['identifier']), + ['identifier'] + ); + + // Add more static attributes here... + + $eavTable->addColumn( + 'created_at', + Table::TYPE_TIMESTAMP, + null, + ['nullable' => false, 'default' => Table::TIMESTAMP_INIT], + 'Creation Time' + )->addColumn( + 'updated_at', + Table::TYPE_TIMESTAMP, + null, + ['nullable' => false, 'default' => Table::TIMESTAMP_INIT_UPDATE], + 'Update Time' + ); + $setup->getConnection()->createTable($eavTable); + /** @var \${Vendorname}\${Modulename}\Setup\EavTablesSetup $eavTablesSetup */ + $eavTablesSetup = $this->eavTablesSetupFactory->create(['setup' => $setup]); + $eavTablesSetup->createEavTables(${Entityname}Setup::ENTITY_TYPE_CODE); + + /** + * Create table '${vendorname}_${modulename}_${simpleentityname}' + */ + $fileTableName = '${vendorname}_${modulename}_${simpleentityname}'; + $fileTable = $setup->getConnection()->newTable( + $setup->getTable($fileTableName) + )->addColumn( + 'id', + Table::TYPE_SMALLINT, + null, + ['identity' => true, 'nullable' => false, 'primary' => true], + 'id' + )->addIndex( + $setup->getIdxName($fileTableName, ['id']), + ['id'] + )->setComment( + '${Entityname} ${Simpleentityname}' + ); + $fileTable->addColumn( + 'entity_id', + Table::TYPE_INTEGER, + null, + ['unsigned' => true, 'nullable' => false, 'primary' => true], + 'Foreign Key entity_id' + )->addForeignKey( + $setup->getFkName($fileTableName, 'entity_id', $eavTableName, 'entity_id'), + 'entity_id', + $setup->getTable($eavTableName), + 'entity_id', + Table::ACTION_CASCADE + ); + $fileTable->addColumn( + 'filename', + Table::TYPE_TEXT, + 255, + ['nullable' => false], + 'filename' + ); + $fileTable->addColumn( + 'link', + Table::TYPE_TEXT, + 255, + ['nullable' => true], + 'link' + ); + $fileTable->addColumn( + 'position', + Table::TYPE_SMALLINT, + null, + ['nullable' => false, 'default' => '0'], + 'position' + ); + $setup->getConnection()->createTable($fileTable); + + $setup->endSetup(); + } +} diff --git a/templates/fileEAVMultiple/Setup/Uninstall.php b/templates/fileEAVMultiple/Setup/Uninstall.php new file mode 100644 index 0000000..75abb68 --- /dev/null +++ b/templates/fileEAVMultiple/Setup/Uninstall.php @@ -0,0 +1,49 @@ +startSetup(); + + foreach ($this->tablesToUninstall as $table) { + if ($setup->tableExists($table)) { + $setup->getConnection()->dropTable($setup->getTable($table)); + } + } + if ($setup->tableExists('${vendorname}_${modulename}_${simpleentityname}')) { + $setup->getConnection()->dropTable($setup->getTable('${vendorname}_${modulename}_${simpleentityname}')); + } + + $setup->endSetup(); + } +} diff --git a/templates/fileEAVMultiple/Ui/Component/Form/${Entityname}/DataProvider.php b/templates/fileEAVMultiple/Ui/Component/Form/${Entityname}/DataProvider.php new file mode 100644 index 0000000..a08fc69 --- /dev/null +++ b/templates/fileEAVMultiple/Ui/Component/Form/${Entityname}/DataProvider.php @@ -0,0 +1,98 @@ +collection = $collection; + $this->filterPool = $filterPool; + $this->request = $request; + } + + /** + * Get data + * + * @return array + */ + public function getData() + { + if (!$this->loadedData) { + $storeId = (int)$this->request->getParam('store'); + $this->collection + ->setStoreId($storeId) + ->addAttributeToSelect('*'); + $items = $this->collection->getItems(); + foreach ($items as $item) { + $this->setDynamicFiles($item); + $item->setStoreId($storeId); + $this->loadedData[$item->getId()] = $item->getData(); + break; + } + } + return $this->loadedData; + } + + private function setDynamicFiles($item) + { + $files = []; + foreach ($item->getDynamicFiles() as $file) { + // Replace file data with file info needed for form input + $file->setFile($file->getFileValueForForm()); + $files[] = $file->getData(); + } + $item->setDynamicFiles($files); + } +} diff --git a/templates/fileEAVMultiple/view/adminhtml/ui_component/${vendorname}_${modulename}_${entityname}_form.xml b/templates/fileEAVMultiple/view/adminhtml/ui_component/${vendorname}_${modulename}_${entityname}_form.xml new file mode 100644 index 0000000..adcb10c --- /dev/null +++ b/templates/fileEAVMultiple/view/adminhtml/ui_component/${vendorname}_${modulename}_${entityname}_form.xml @@ -0,0 +1,171 @@ + + +
+ + + ${vendorname}_${modulename}_${entityname}_form.${vendorname}_${modulename}_${entityname}_form_data_source + ${vendorname}_${modulename}_${entityname}_form.${vendorname}_${modulename}_${entityname}_form_data_source + + + data + + templates/form/collapsible + ${Entityname} Form + + ${Vendorname}\${Modulename}\Block\Adminhtml\${Entityname}\Edit\BackButton + ${Vendorname}\${Modulename}\Block\Adminhtml\${Entityname}\Edit\DeleteButton + ${Vendorname}\${Modulename}\Block\Adminhtml\${Entityname}\Edit\SaveButton + ${Vendorname}\${Modulename}\Block\Adminhtml\${Entityname}\Edit\SaveAndContinueButton + + + + + ${Vendorname}\${Modulename}\Ui\Component\Form\${Entityname}\DataProvider + ${vendorname}_${modulename}_${entityname}_form_data_source + entity_id + entity_id + + + + + + + + + + Magento_Ui/js/form/provider + + + +
+ + + Main Information + + + + + + true + number + hidden + + 10 + main_fieldset + + + + + + + false + text + input + main_fieldset + + + + + + + true + text + Identifier + input + main_fieldset + + true + + 10 + [GLOBAL] + + + + + + + dynamicRows + record + 20 + true + ${Simpleentityname} + true + + true + + + + + + + true + true + Magento_Ui/js/dynamic-rows/record + container + false + + + + + + false + number + hidden + + + + + + + + fileUploader + fileUploader + Allowed file types: jpeg, gif, png + 2097152 + jpg jpeg gif png + + ${vendorname}_${modulename}/file/uploader + + + + + + + + true + text + Link + input + + + + + + + true + number + hidden + + + + + + + actionDelete + text + false + 50 + + + + + + +
+
diff --git a/templates/fileModel/.no-copied-config/after-generate-info.txt b/templates/fileModel/.no-copied-config/after-generate-info.txt index e0a53c3..d408c0e 100644 --- a/templates/fileModel/.no-copied-config/after-generate-info.txt +++ b/templates/fileModel/.no-copied-config/after-generate-info.txt @@ -13,12 +13,12 @@ 2. Add the formElement into your uiComponent form: - ${Vendorname}/${Modulename}/view/adminhtml/ui_component/${vendorname}_${modulename}_${entityname}_form.xml: + ${Vendorname}/${Modulename}/view/adminhtml/ui_component/${vendorname}_${modulename}_${simpleentityname}_form.xml: - ${Entityname} + ${Simpleentityname} fileUploader fileUploader Allowed file types: jpeg, gif, png @@ -36,7 +36,7 @@ 3.- Get right File format for the form in your DataProvider - ${Vendorname}/${Modulename}/Ui/Component/Form/${Entityname}/DataProvider.php + ${Vendorname}/${Modulename}/Ui/Component/Form/${Simpleentityname}/DataProvider.php /** * Get data @@ -48,7 +48,7 @@ if (!$this->loadedData) { $items = $this->collection->getItems(); foreach ($items as $item) { - $image = $item->get${Entityname}(); + $image = $item->get${Simpleentityname}(); if ($image->getId()) { $item->setFile($image->getFileValueForForm()); } @@ -61,7 +61,7 @@ 4. Save image on your Save action controller - ${Vendorname}/${Modulename}/Controller/Adminhtml/${Entityname}/Save.php: + ${Vendorname}/${Modulename}/Controller/Adminhtml/${Simpleentityname}/Save.php: /** * file data preprocessing diff --git a/templates/fileModel/Model/${Entityname}.php b/templates/fileModel/Model/${Simpleentityname}.php similarity index 87% rename from templates/fileModel/Model/${Entityname}.php rename to templates/fileModel/Model/${Simpleentityname}.php index f83d5e7..3523d59 100644 --- a/templates/fileModel/Model/${Entityname}.php +++ b/templates/fileModel/Model/${Simpleentityname}.php @@ -1,6 +1,6 @@ _init('${Vendorname}\${Modulename}\Model\ResourceModel\${Entityname}'); + $this->_init('${Vendorname}\${Modulename}\Model\ResourceModel\${Simpleentityname}'); } /** * @param FileProcessor $fileProcessor - * @param ${Entityname}Factory $${entityname}Factory + * @param ${Simpleentityname}Factory $${simpleentityname}Factory * @param Context $context * @param Registry $registry * @param AbstractResource|null $resource @@ -72,7 +72,7 @@ protected function _construct() */ public function __construct( FileProcessor $fileProcessor, - ${Entityname}Factory $${entityname}Factory, + ${Simpleentityname}Factory $${simpleentityname}Factory, Context $context, Registry $registry, AbstractResource $resource = null, @@ -88,7 +88,7 @@ public function __construct( ); $this->fileProcessor = $fileProcessor; - $this->${entityname}Factory = $${entityname}Factory; + $this->${simpleentityname}Factory = $${simpleentityname}Factory; } /** @@ -195,8 +195,8 @@ public function saveDynamicFieldFiles(array $files, array $data = []) if (isset($file[$idField]) && !$file[$idField]) { unset($file[$idField]); } - $fileObject = $this->${entityname}Factory->create()->addData($file); - if ($fileObject->getDelete() || !$fileObject->get${Form_field_name}()) { + $fileObject = $this->${simpleentityname}Factory->create()->addData($file); + if ($fileObject->getDelete() || !$fileObject->getFile()) { $fileObject->isDeleted(true); } if (isset($items[$fileObject->getId()])) { diff --git a/templates/fileModel/Setup/InstallSchema.php b/templates/fileModel/Setup/InstallSchema.php new file mode 100644 index 0000000..4411473 --- /dev/null +++ b/templates/fileModel/Setup/InstallSchema.php @@ -0,0 +1,57 @@ +startSetup(); + + /** + * Create table '${vendorname}_${modulename}_${simpleentityname}' + */ + $table = $setup->getConnection()->newTable( + $setup->getTable('${vendorname}_${modulename}_${simpleentityname}') + )->addColumn( + 'id', + Table::TYPE_SMALLINT, + null, + ['identity' => true, 'nullable' => false, 'primary' => true], + '${Simpleentityname} ID' + )->addColumn( + 'filename', + Table::TYPE_TEXT, + 255, + ['nullable' => false], + 'filename' + )->setComment( + '${Simpleentityname} Table' + ); + + // Add more columns here + + $setup->getConnection()->createTable($table); + + $setup->endSetup(); + } +} diff --git a/templates/model/Model/${Entityname}.php b/templates/model/Model/${Simpleentityname}.php similarity index 76% rename from templates/model/Model/${Entityname}.php rename to templates/model/Model/${Simpleentityname}.php index a535a35..fa40c5b 100644 --- a/templates/model/Model/${Entityname}.php +++ b/templates/model/Model/${Simpleentityname}.php @@ -1,7 +1,7 @@ _init('${Vendorname}\${Modulename}\Model\ResourceModel\${Entityname}'); + $this->_init('${Vendorname}\${Modulename}\Model\ResourceModel\${Simpleentityname}'); } /** diff --git a/templates/model/Model/ResourceModel/${Entityname}.php b/templates/model/Model/ResourceModel/${Simpleentityname}.php similarity index 71% rename from templates/model/Model/ResourceModel/${Entityname}.php rename to templates/model/Model/ResourceModel/${Simpleentityname}.php index f63390c..d373feb 100644 --- a/templates/model/Model/ResourceModel/${Entityname}.php +++ b/templates/model/Model/ResourceModel/${Simpleentityname}.php @@ -1,7 +1,7 @@ _init('${vendorname}_${modulename}_${entityname}', 'entity_id'); + $this->_init('${vendorname}_${modulename}_${simpleentityname}', 'id'); } } diff --git a/templates/model/Model/ResourceModel/${Entityname}/Collection.php b/templates/model/Model/ResourceModel/${Simpleentityname}/Collection.php similarity index 62% rename from templates/model/Model/ResourceModel/${Entityname}/Collection.php rename to templates/model/Model/ResourceModel/${Simpleentityname}/Collection.php index ddaec06..2befa25 100644 --- a/templates/model/Model/ResourceModel/${Entityname}/Collection.php +++ b/templates/model/Model/ResourceModel/${Simpleentityname}/Collection.php @@ -5,7 +5,7 @@ * @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved. * @author ${commentsUserEmail} */ -namespace ${Vendorname}\${Modulename}\Model\ResourceModel\${Entityname}; +namespace ${Vendorname}\${Modulename}\Model\ResourceModel\${Simpleentityname}; use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; @@ -14,7 +14,7 @@ class Collection extends AbstractCollection /** * @var string */ - protected $_idFieldName = 'entity_id'; + protected $_idFieldName = 'id'; /** * Define resource model @@ -23,6 +23,6 @@ class Collection extends AbstractCollection */ protected function _construct() { - $this->_init('${Vendorname}\${Modulename}\Model\${Entityname}', '${Vendorname}\${Modulename}\Model\ResourceModel\${Entityname}'); + $this->_init('${Vendorname}\${Modulename}\Model\${Simpleentityname}', '${Vendorname}\${Modulename}\Model\ResourceModel\${Simpleentityname}'); } } diff --git a/templates/model/Setup/InstallSchema.php b/templates/model/Setup/InstallSchema.php index e5561bf..fcbd31f 100644 --- a/templates/model/Setup/InstallSchema.php +++ b/templates/model/Setup/InstallSchema.php @@ -28,32 +28,32 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con $setup->startSetup(); /** - * Create table '${vendorname}_${modulename}_${entityname}' + * Create table '${vendorname}_${modulename}_${simpleentityname}' */ $table = $setup->getConnection()->newTable( - $setup->getTable('${vendorname}_${modulename}_${entityname}') + $setup->getTable('${vendorname}_${modulename}_${simpleentityname}') )->addColumn( - 'entity_id', + 'id', Table::TYPE_SMALLINT, null, ['identity' => true, 'nullable' => false, 'primary' => true], - '${Entityname} ID' + '${Simpleentityname} ID' )->addColumn( 'identifier', Table::TYPE_TEXT, 100, ['nullable' => false], - '${Entityname} Identifier' + '${Simpleentityname} Identifier' )->addIndex( $setup->getIdxName( - $setup->getTable('${vendorname}_${modulename}_${entityname}'), + $setup->getTable('${vendorname}_${modulename}_${simpleentityname}'), ['identifier'], AdapterInterface::INDEX_TYPE_FULLTEXT ), ['identifier'], ['type' => AdapterInterface::INDEX_TYPE_FULLTEXT] )->setComment( - '${Entityname} Table' + '${Simpleentityname} Table' ); // Add more columns here diff --git a/templates/model/Setup/Uninstall.php b/templates/model/Setup/Uninstall.php index 92faf56..236f654 100644 --- a/templates/model/Setup/Uninstall.php +++ b/templates/model/Setup/Uninstall.php @@ -22,8 +22,8 @@ public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $c { $setup->startSetup(); - if ($setup->tableExists('${vendorname}_${modulename}_${entityname}')) { - $setup->getConnection()->dropTable($setup->getTable('${vendorname}_${modulename}_${entityname}')); + if ($setup->tableExists('${vendorname}_${modulename}_${simpleentityname}')) { + $setup->getConnection()->dropTable($setup->getTable('${vendorname}_${modulename}_${simpleentityname}')); } $setup->endSetup();