Skip to content

Commit

Permalink
New templates crudEAVWithMultipleFiles and fileEAVMultiple
Browse files Browse the repository at this point in the history
  • Loading branch information
jalogut committed Jul 28, 2017
1 parent 0898b28 commit 07b90cd
Show file tree
Hide file tree
Showing 18 changed files with 757 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -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'),
];

//...
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies:
0: crudEAV
1: model
2: fileModel
3: fileEAVMultiple
4: fileProcessor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This Template creates an EAV crud module using uiComponents for Grid and Form. It includes Dynamic Multiple Files
4 changes: 4 additions & 0 deletions templates/fileEAVMultiple/.no-copied-config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies:
0: model
1: fileModel
2: fileProcessor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This Template creates the attribute Backend, Ajax and Helpers to have a OneToMany relation between EAV entity and files
131 changes: 131 additions & 0 deletions templates/fileEAVMultiple/Model/${Entityname}.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/**
* ${Entityname}.php
*
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
* @author ${commentsUserEmail}
*/

namespace ${Vendorname}\${Modulename}\Model;

use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Registry;
use ${Vendorname}\${Modulename}\Model\ResourceModel\${Simpleentityname}\Collection as ${Simpleentityname}Collection;

class ${Entityname} extends AbstractModel implements IdentityInterface
{
/**
* CMS page cache tag
*/
const CACHE_TAG = '${vendorname}_${modulename}_${entityname}';

/**
* @var string
*/
protected $_cacheTag = '${vendorname}_${modulename}_${entityname}';

/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = '${vendorname}_${modulename}_${entityname}';

/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
parent::_construct();
$this->_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);
}

}
146 changes: 146 additions & 0 deletions templates/fileEAVMultiple/Setup/InstallSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* installSchema.php
*
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
* @author ${commentsUserEmail}
*/
namespace ${Vendorname}\${Modulename}\Setup;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use ${Vendorname}\${Modulename}\Setup\EavTablesSetupFactory;

/**
* @codeCoverageIgnore
*/
class InstallSchema implements InstallSchemaInterface
{
/**
* @var EavTablesSetupFactory
*/
protected $eavTablesSetupFactory;

/**
* Init
*
* @internal param EavTablesSetupFactory $EavTablesSetupFactory
*/
public function __construct(EavTablesSetupFactory $eavTablesSetupFactory)
{
$this->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();
}
}
Loading

0 comments on commit 07b90cd

Please sign in to comment.