Skip to content

Commit 07b90cd

Browse files
committed
New templates crudEAVWithMultipleFiles and fileEAVMultiple
1 parent 0898b28 commit 07b90cd

File tree

18 files changed

+757
-40
lines changed

18 files changed

+757
-40
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
1. Add your STATIC fields on:
2+
3+
${Vendorname}/${Modulename}/Setup/InstallSchema.php
4+
5+
2. Add ENTITY EAV Attributes on:
6+
7+
${Vendorname}/${Modulename}/Setup/${Entityname}Setup.php
8+
9+
3. Add your custom columns to the grid:
10+
11+
${Vendorname}/${Modulename}/view/adminhtml/ui_component/${vendorname}_${modulename}_${entityname}_listing.xml
12+
13+
4. Add your custom fields to the form:
14+
15+
${Vendorname}/${Modulename}/view/adminhtml/ui_component/${vendorname}_${modulename}_${entityname}_form.xml
16+
17+
5. Add Attributes to display in Grid on the Listing DataProvider:
18+
19+
${Vendorname}\${Modulename}\Ui\Component\Listing\DataProvider
20+
21+
/**
22+
* @param SearchResultInterface $searchResult
23+
* @return array
24+
*/
25+
protected function searchResultToOutput(SearchResultInterface $searchResult)
26+
{
27+
$searchResult->setStoreId($this->request->getParam('store', 0))
28+
->addAttributeToSelect([]); // Add here needed EAV attributes to display on grid
29+
30+
return parent::searchResultToOutput($searchResult);
31+
}
32+
33+
6. Set the Admin Menu tab where you want your Module can be found:
34+
35+
${Vendorname}/${Modulename}/etc/adminhtml/menu.xml
36+
37+
7. Set From server side Validations:
38+
39+
${Vendorname}\${Modulename}\Controller\Adminhtml\${Entityname}\Validate:
40+
41+
/**
42+
* Check if required fields is not empty
43+
*
44+
* @param array $data
45+
*/
46+
public function validateRequireEntries(array $data)
47+
{
48+
$requiredFields = [
49+
'identifier' => __('${Entityname} Identifier'),
50+
];
51+
52+
//...
53+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dependencies:
2+
0: crudEAV
3+
1: model
4+
2: fileModel
5+
3: fileEAVMultiple
6+
4: fileProcessor
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This Template creates an EAV crud module using uiComponents for Grid and Form. It includes Dynamic Multiple Files
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dependencies:
2+
0: model
3+
1: fileModel
4+
2: fileProcessor
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This Template creates the attribute Backend, Ajax and Helpers to have a OneToMany relation between EAV entity and files
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
/**
4+
* ${Entityname}.php
5+
*
6+
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
7+
* @author ${commentsUserEmail}
8+
*/
9+
10+
namespace ${Vendorname}\${Modulename}\Model;
11+
12+
use Magento\Framework\DataObject\IdentityInterface;
13+
use Magento\Framework\Model\AbstractModel;
14+
use Magento\Framework\Model\Context;
15+
use Magento\Framework\Model\ResourceModel\AbstractResource;
16+
use Magento\Framework\Data\Collection\AbstractDb;
17+
use Magento\Framework\Registry;
18+
use ${Vendorname}\${Modulename}\Model\ResourceModel\${Simpleentityname}\Collection as ${Simpleentityname}Collection;
19+
20+
class ${Entityname} extends AbstractModel implements IdentityInterface
21+
{
22+
/**
23+
* CMS page cache tag
24+
*/
25+
const CACHE_TAG = '${vendorname}_${modulename}_${entityname}';
26+
27+
/**
28+
* @var string
29+
*/
30+
protected $_cacheTag = '${vendorname}_${modulename}_${entityname}';
31+
32+
/**
33+
* Prefix of model events names
34+
*
35+
* @var string
36+
*/
37+
protected $_eventPrefix = '${vendorname}_${modulename}_${entityname}';
38+
39+
/**
40+
* Initialize resource model
41+
*
42+
* @return void
43+
*/
44+
protected function _construct()
45+
{
46+
parent::_construct();
47+
$this->_init('${Vendorname}\${Modulename}\Model\ResourceModel\${Entityname}');
48+
}
49+
50+
/**
51+
* Reference constructor.
52+
* @param ${Simpleentityname}Collection $${simpleentityname}Collection,
53+
* @param ${Simpleentityname}Factory $${simpleentityname}Factory,
54+
* @param Context $context
55+
* @param Registry $registry
56+
* @param AbstractResource|null $resource
57+
* @param AbstractDb|null $resourceCollection
58+
* @param array $data
59+
*/
60+
public function __construct(
61+
${Simpleentityname}Collection $${simpleentityname}Collection,
62+
${Simpleentityname}Factory $${simpleentityname}Factory,
63+
Context $context,
64+
Registry $registry,
65+
AbstractResource $resource = null,
66+
AbstractDb $resourceCollection = null,
67+
array $data = []
68+
) {
69+
parent::__construct(
70+
$context,
71+
$registry,
72+
$resource,
73+
$resourceCollection,
74+
$data
75+
);
76+
$this->${simpleentityname}Collection = $${simpleentityname}Collection;
77+
$this->${simpleentityname}Factory = $${simpleentityname}Factory;
78+
}
79+
80+
/**
81+
* Get identities
82+
*
83+
* @return array
84+
*/
85+
public function getIdentities()
86+
{
87+
return [self::CACHE_TAG . '_' . $this->getId()];
88+
}
89+
90+
/**
91+
* Save from collection data
92+
*
93+
* @param array $data
94+
* @return $this|bool
95+
*/
96+
public function saveCollection(array $data)
97+
{
98+
if (isset($data[$this->getId()])) {
99+
$this->addData($data[$this->getId()]);
100+
$this->getResource()->save($this);
101+
}
102+
return $this;
103+
}
104+
105+
/**
106+
* Save images once the main reference data is saved
107+
*
108+
* @return $this
109+
*/
110+
public function afterSave() //@codingStandardsIgnoreLine
111+
{
112+
parent::afterSave();
113+
if (is_array($this->getData('dynamic_files'))) {
114+
$fileObject = $this->${simpleentityname}Factory->create();
115+
$fileObject->saveDynamicFieldFiles($this->getData('dynamic_files'), ['entity_id' => $this->getId()]);
116+
}
117+
118+
return $this;
119+
}
120+
121+
/**
122+
* @return Collection
123+
*/
124+
public function getDynamicFiles()
125+
{
126+
return $this->${simpleentityname}Collection
127+
->addFieldToFilter('entity_id', $this->getId())
128+
->addOrder('position', \Magento\Framework\Data\Collection::SORT_ORDER_ASC);
129+
}
130+
131+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
/**
3+
* installSchema.php
4+
*
5+
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
6+
* @author ${commentsUserEmail}
7+
*/
8+
namespace ${Vendorname}\${Modulename}\Setup;
9+
10+
use Magento\Framework\DB\Ddl\Table;
11+
use Magento\Framework\Setup\InstallSchemaInterface;
12+
use Magento\Framework\Setup\ModuleContextInterface;
13+
use Magento\Framework\Setup\SchemaSetupInterface;
14+
use ${Vendorname}\${Modulename}\Setup\EavTablesSetupFactory;
15+
16+
/**
17+
* @codeCoverageIgnore
18+
*/
19+
class InstallSchema implements InstallSchemaInterface
20+
{
21+
/**
22+
* @var EavTablesSetupFactory
23+
*/
24+
protected $eavTablesSetupFactory;
25+
26+
/**
27+
* Init
28+
*
29+
* @internal param EavTablesSetupFactory $EavTablesSetupFactory
30+
*/
31+
public function __construct(EavTablesSetupFactory $eavTablesSetupFactory)
32+
{
33+
$this->eavTablesSetupFactory = $eavTablesSetupFactory;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
39+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
40+
*/
41+
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) //@codingStandardsIgnoreLine
42+
{
43+
$setup->startSetup();
44+
45+
$eavTableName = ${Entityname}Setup::ENTITY_TYPE_CODE . '_entity';
46+
/**
47+
* Create entity Table
48+
*/
49+
$eavTable = $setup->getConnection()
50+
->newTable($setup->getTable($eavTableName))
51+
->addColumn(
52+
'entity_id',
53+
Table::TYPE_INTEGER,
54+
null,
55+
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
56+
'Entity ID'
57+
)->setComment('Entity Table');
58+
59+
$eavTable->addColumn(
60+
'identifier',
61+
Table::TYPE_TEXT,
62+
100,
63+
['nullable' => false],
64+
'Identifier'
65+
)->addIndex(
66+
$setup->getIdxName($eavTableName, ['identifier']),
67+
['identifier']
68+
);
69+
70+
// Add more static attributes here...
71+
72+
$eavTable->addColumn(
73+
'created_at',
74+
Table::TYPE_TIMESTAMP,
75+
null,
76+
['nullable' => false, 'default' => Table::TIMESTAMP_INIT],
77+
'Creation Time'
78+
)->addColumn(
79+
'updated_at',
80+
Table::TYPE_TIMESTAMP,
81+
null,
82+
['nullable' => false, 'default' => Table::TIMESTAMP_INIT_UPDATE],
83+
'Update Time'
84+
);
85+
$setup->getConnection()->createTable($eavTable);
86+
/** @var \${Vendorname}\${Modulename}\Setup\EavTablesSetup $eavTablesSetup */
87+
$eavTablesSetup = $this->eavTablesSetupFactory->create(['setup' => $setup]);
88+
$eavTablesSetup->createEavTables(${Entityname}Setup::ENTITY_TYPE_CODE);
89+
90+
/**
91+
* Create table '${vendorname}_${modulename}_${simpleentityname}'
92+
*/
93+
$fileTableName = '${vendorname}_${modulename}_${simpleentityname}';
94+
$fileTable = $setup->getConnection()->newTable(
95+
$setup->getTable($fileTableName)
96+
)->addColumn(
97+
'id',
98+
Table::TYPE_SMALLINT,
99+
null,
100+
['identity' => true, 'nullable' => false, 'primary' => true],
101+
'id'
102+
)->addIndex(
103+
$setup->getIdxName($fileTableName, ['id']),
104+
['id']
105+
)->setComment(
106+
'${Entityname} ${Simpleentityname}'
107+
);
108+
$fileTable->addColumn(
109+
'entity_id',
110+
Table::TYPE_INTEGER,
111+
null,
112+
['unsigned' => true, 'nullable' => false, 'primary' => true],
113+
'Foreign Key entity_id'
114+
)->addForeignKey(
115+
$setup->getFkName($fileTableName, 'entity_id', $eavTableName, 'entity_id'),
116+
'entity_id',
117+
$setup->getTable($eavTableName),
118+
'entity_id',
119+
Table::ACTION_CASCADE
120+
);
121+
$fileTable->addColumn(
122+
'filename',
123+
Table::TYPE_TEXT,
124+
255,
125+
['nullable' => false],
126+
'filename'
127+
);
128+
$fileTable->addColumn(
129+
'link',
130+
Table::TYPE_TEXT,
131+
255,
132+
['nullable' => true],
133+
'link'
134+
);
135+
$fileTable->addColumn(
136+
'position',
137+
Table::TYPE_SMALLINT,
138+
null,
139+
['nullable' => false, 'default' => '0'],
140+
'position'
141+
);
142+
$setup->getConnection()->createTable($fileTable);
143+
144+
$setup->endSetup();
145+
}
146+
}

0 commit comments

Comments
 (0)