├── README.md
├── blank
└── app
│ └── code
│ └── Namespace
│ └── Module
│ ├── Block
│ ├── Adminhtml
│ │ ├── Form
│ │ │ └── Element
│ │ │ │ └── Image.php
│ │ ├── Module.php
│ │ └── Module
│ │ │ ├── Edit.php
│ │ │ ├── Edit
│ │ │ ├── Form.php
│ │ │ ├── Tab
│ │ │ │ ├── Content.php
│ │ │ │ ├── Image.php
│ │ │ │ └── Main.php
│ │ │ └── Tabs.php
│ │ │ └── Grid.php
│ ├── Module.php
│ └── View.php
│ ├── Controller
│ ├── AbstractController
│ │ ├── ModuleLoader.php
│ │ ├── ModuleLoaderInterface.php
│ │ └── View.php
│ ├── Adminhtml
│ │ └── Index
│ │ │ ├── Delete.php
│ │ │ ├── Edit.php
│ │ │ ├── Grid.php
│ │ │ ├── Index.php
│ │ │ ├── NewAction.php
│ │ │ ├── PostDataProcessor.php
│ │ │ └── Save.php
│ ├── Index
│ │ ├── Index.php
│ │ └── View.php
│ └── ModuleInterface.php
│ ├── Helper
│ └── Data.php
│ ├── Model
│ ├── Module.php
│ └── ResourceModel
│ │ ├── Module.php
│ │ └── Module
│ │ └── Collection.php
│ ├── Setup
│ └── InstallSchema.php
│ ├── etc
│ ├── acl.xml
│ ├── adminhtml
│ │ ├── menu.xml
│ │ ├── routes.xml
│ │ └── system.xml
│ ├── config.xml
│ ├── frontend
│ │ ├── di.xml
│ │ └── routes.xml
│ └── module.xml
│ ├── i18n
│ └── en_US.csv
│ ├── registration.php
│ └── view
│ ├── adminhtml
│ └── layout
│ │ ├── module_index_edit.xml
│ │ ├── module_index_grid.xml
│ │ ├── module_index_index.xml
│ │ └── module_index_new.xml
│ └── frontend
│ ├── layout
│ ├── default.xml
│ ├── module_index_index.xml
│ └── module_index_view.xml
│ └── templates
│ ├── module.phtml
│ └── view.phtml
├── form.php
├── index.php
├── js
├── jquery-1.11.1.js
├── jquery.js
└── jquery.validate.js
└── main.css
/README.md:
--------------------------------------------------------------------------------
1 | # magento2-module-creator
2 | Magento 2 Module Creator
3 |
4 |
5 | - This module creator script will save magento developers time which helps in providing estimation of custom module development.
6 | - To create a new module for Magento 2, insert Namespace and a Module name.
7 | - This script will create module which has basic folders, required PHP and XML files, frontend listing and view, backend listing and view according to Magento 2 structure and coding standards.
8 | - You can later start work by keeping/adding/removing/changing files according to your requirement.
9 |
10 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Block/Adminhtml/Form/Element/Image.php:
--------------------------------------------------------------------------------
1 | Form Image File Element Block
5 | *
6 | */
7 | namespace \\Block\Adminhtml\Form\Element;
8 |
9 | class Image extends Magento\Framework\Data\Form\Element\Image
10 | {
11 | /**
12 | * Get image preview url
13 | *
14 | * @return string
15 | */
16 | protected function _getUrl()
17 | {
18 | return $this->getValue();
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Block/Adminhtml/Module.php:
--------------------------------------------------------------------------------
1 | list block
4 | *
5 | */
6 | namespace \\Block\Adminhtml;
7 |
8 | class extends \Magento\Backend\Block\Widget\Grid\Container
9 | {
10 | /**
11 | * Constructor
12 | *
13 | * @return void
14 | */
15 | protected function _construct()
16 | {
17 | $this->_controller = 'adminhtml_';
18 | $this->_blockGroup = '_';
19 | $this->_headerText = __('');
20 | $this->_addButtonLabel = __('Add New ');
21 | parent::_construct();
22 | if ($this->_isAllowedAction('_::save')) {
23 | $this->buttonList->update('add', 'label', __('Add New '));
24 | } else {
25 | $this->buttonList->remove('add');
26 | }
27 | }
28 |
29 | /**
30 | * Check permission for passed action
31 | *
32 | * @param string $resourceId
33 | * @return bool
34 | */
35 | protected function _isAllowedAction($resourceId)
36 | {
37 | return $this->_authorization->isAllowed($resourceId);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Block/Adminhtml/Module/Edit.php:
--------------------------------------------------------------------------------
1 | \\Block\Adminhtml\;
3 |
4 | /**
5 | * Admin page
6 | *
7 | */
8 | class Edit extends \Magento\Backend\Block\Widget\Form\Container
9 | {
10 | /**
11 | * Core registry
12 | *
13 | * @var \Magento\Framework\Registry
14 | */
15 | protected $_coreRegistry = null;
16 |
17 | /**
18 | * @param \Magento\Backend\Block\Widget\Context $context
19 | * @param \Magento\Framework\Registry $registry
20 | * @param array $data
21 | */
22 | public function __construct(
23 | \Magento\Backend\Block\Widget\Context $context,
24 | \Magento\Framework\Registry $registry,
25 | array $data = []
26 | ) {
27 | $this->_coreRegistry = $registry;
28 | parent::__construct($context, $data);
29 | }
30 |
31 | /**
32 | * Initialize cms page edit block
33 | *
34 | * @return void
35 | */
36 | protected function _construct()
37 | {
38 | $this->_objectId = '_id';
39 | $this->_blockGroup = '_';
40 | $this->_controller = 'adminhtml_';
41 |
42 | parent::_construct();
43 |
44 | if ($this->_isAllowedAction('_::save')) {
45 | $this->buttonList->update('save', 'label', __('Save '));
46 | $this->buttonList->add(
47 | 'saveandcontinue',
48 | [
49 | 'label' => __('Save and Continue Edit'),
50 | 'class' => 'save',
51 | 'data_attribute' => [
52 | 'mage-init' => [
53 | 'button' => ['event' => 'saveAndContinueEdit', 'target' => '#edit_form'],
54 | ],
55 | ]
56 | ],
57 | -100
58 | );
59 | } else {
60 | $this->buttonList->remove('save');
61 | }
62 |
63 | if ($this->_isAllowedAction('_::_delete')) {
64 | $this->buttonList->update('delete', 'label', __('Delete '));
65 | } else {
66 | $this->buttonList->remove('delete');
67 | }
68 | }
69 |
70 | /**
71 | * Retrieve text for header element depending on loaded page
72 | *
73 | * @return string
74 | */
75 | public function getHeaderText()
76 | {
77 | if ($this->_coreRegistry->registry('')->getId()) {
78 | return __("Edit '%1'", $this->escapeHtml($this->_coreRegistry->registry('')->getTitle()));
79 | } else {
80 | return __('New ');
81 | }
82 | }
83 |
84 | /**
85 | * Check permission for passed action
86 | *
87 | * @param string $resourceId
88 | * @return bool
89 | */
90 | protected function _isAllowedAction($resourceId)
91 | {
92 | return $this->_authorization->isAllowed($resourceId);
93 | }
94 |
95 | /**
96 | * Getter of url for "Save and Continue" button
97 | * tab_id will be replaced by desired by JS later
98 | *
99 | * @return string
100 | */
101 | protected function _getSaveAndContinueUrl()
102 | {
103 | return $this->getUrl('/*/save', ['_current' => true, 'back' => 'edit', 'active_tab' => '{{tab_id}}']);
104 | }
105 |
106 | /**
107 | * Prepare layout
108 | *
109 | * @return \Magento\Framework\View\Element\AbstractBlock
110 | */
111 | protected function _prepareLayout()
112 | {
113 | $this->_formScripts[] = "
114 | function toggleEditor() {
115 | if (tinyMCE.getInstanceById('page_content') == null) {
116 | tinyMCE.execCommand('mceAddControl', false, 'page_content');
117 | } else {
118 | tinyMCE.execCommand('mceRemoveControl', false, 'page_content');
119 | }
120 | };
121 | ";
122 | return parent::_prepareLayout();
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Block/Adminhtml/Module/Edit/Form.php:
--------------------------------------------------------------------------------
1 | \\Block\Adminhtml\\Edit;
3 |
4 | /**
5 | * Adminhtml edit form block
6 | *
7 | */
8 | class Form extends \Magento\Backend\Block\Widget\Form\Generic
9 | {
10 | /**
11 | * Prepare form
12 | *
13 | * @return $this
14 | */
15 | protected function _prepareForm()
16 | {
17 | /** @var \Magento\Framework\Data\Form $form */
18 | $form = $this->_formFactory->create(
19 | ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post','enctype' => 'multipart/form-data']]
20 | );
21 | $form->setUseContainer(true);
22 | $this->setForm($form);
23 | return parent::_prepareForm();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Block/Adminhtml/Module/Edit/Tab/Content.php:
--------------------------------------------------------------------------------
1 | \\Block\Adminhtml\\Edit\Tab;
3 |
4 | /**
5 | * edit form main tab
6 | */
7 | class Content extends \Magento\Backend\Block\Widget\Form\Generic implements
8 | \Magento\Backend\Block\Widget\Tab\TabInterface
9 | {
10 | /**
11 | * @var \Magento\Cms\Model\Wysiwyg\Config
12 | */
13 | protected $_wysiwygConfig;
14 |
15 | /**
16 | * @param \Magento\Backend\Block\Template\Context $context
17 | * @param \Magento\Framework\Registry $registry
18 | * @param \Magento\Framework\Data\FormFactory $formFactory
19 | * @param \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig
20 | * @param array $data
21 | */
22 | public function __construct(
23 | \Magento\Backend\Block\Template\Context $context,
24 | \Magento\Framework\Registry $registry,
25 | \Magento\Framework\Data\FormFactory $formFactory,
26 | \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
27 | array $data = []
28 | ) {
29 | $this->_wysiwygConfig = $wysiwygConfig;
30 | parent::__construct($context, $registry, $formFactory, $data);
31 | }
32 |
33 | /**
34 | * Prepare form
35 | *
36 | * @return $this
37 | */
38 | protected function _prepareForm()
39 | {
40 | /** @var $model \Model\ */
41 | $model = $this->_coreRegistry->registry('');
42 |
43 | /*
44 | * Checking if user have permissions to save information
45 | */
46 | if ($this->_isAllowedAction('_::save')) {
47 | $isElementDisabled = false;
48 | } else {
49 | $isElementDisabled = true;
50 | }
51 |
52 | /** @var \Magento\Framework\Data\Form $form */
53 | $form = $this->_formFactory->create();
54 |
55 | $form->setHtmlIdPrefix('_content_');
56 |
57 | $fieldset = $form->addFieldset(
58 | 'content_fieldset',
59 | ['legend' => __('Content'), 'class' => 'fieldset-wide']
60 | );
61 |
62 | $wysiwygConfig = $this->_wysiwygConfig->getConfig(['tab_id' => $this->getTabId()]);
63 |
64 | $contentField = $fieldset->addField(
65 | 'content',
66 | 'editor',
67 | [
68 | 'name' => 'content',
69 | 'style' => 'height:36em;',
70 | 'required' => true,
71 | 'disabled' => $isElementDisabled,
72 | 'config' => $wysiwygConfig
73 | ]
74 | );
75 |
76 | // Setting custom renderer for content field to remove label column
77 | $renderer = $this->getLayout()->createBlock(
78 | 'Magento\Backend\Block\Widget\Form\Renderer\Fieldset\Element'
79 | )->setTemplate(
80 | 'Magento_Cms::page/edit/form/renderer/content.phtml'
81 | );
82 | $contentField->setRenderer($renderer);
83 |
84 | $this->_eventManager->dispatch('adminhtml__edit_tab_content_prepare_form', ['form' => $form]);
85 | $form->setValues($model->getData());
86 | $this->setForm($form);
87 |
88 | return parent::_prepareForm();
89 | }
90 |
91 | /**
92 | * Prepare label for tab
93 | *
94 | * @return string
95 | */
96 | public function getTabLabel()
97 | {
98 | return __('Content');
99 | }
100 |
101 | /**
102 | * Prepare title for tab
103 | *
104 | * @return string
105 | */
106 | public function getTabTitle()
107 | {
108 | return __('Content');
109 | }
110 |
111 | /**
112 | * Returns status flag about this tab can be shown or not
113 | *
114 | * @return bool
115 | */
116 | public function canShowTab()
117 | {
118 | return true;
119 | }
120 |
121 | /**
122 | * Returns status flag about this tab hidden or not
123 | *
124 | * @return bool
125 | */
126 | public function isHidden()
127 | {
128 | return false;
129 | }
130 |
131 | /**
132 | * Check permission for passed action
133 | *
134 | * @param string $resourceId
135 | * @return bool
136 | */
137 | protected function _isAllowedAction($resourceId)
138 | {
139 | return $this->_authorization->isAllowed($resourceId);
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Block/Adminhtml/Module/Edit/Tab/Image.php:
--------------------------------------------------------------------------------
1 | \\Block\Adminhtml\\Edit\Tab;
3 |
4 | /**
5 | * @SuppressWarnings(PHPMD.DepthOfInheritance)
6 | */
7 | class Image extends \Magento\Backend\Block\Widget\Form\Generic implements
8 | \Magento\Backend\Block\Widget\Tab\TabInterface
9 | {
10 |
11 | /**
12 | * @param \Magento\Backend\Block\Template\Context $context
13 | * @param \Magento\Framework\Registry $registry
14 | * @param \Magento\Framework\Data\FormFactory $formFactory
15 | * @param array $data
16 | */
17 | public function __construct(
18 | \Magento\Backend\Block\Template\Context $context,
19 | \Magento\Framework\Registry $registry,
20 | \Magento\Framework\Data\FormFactory $formFactory,
21 | array $data = []
22 | ) {
23 | parent::__construct($context, $registry, $formFactory, $data);
24 | }
25 |
26 | /**
27 | * Initialise form fields
28 | *
29 | * @return $this
30 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
31 | */
32 | protected function _prepareForm()
33 | {
34 | /** @var \Magento\Framework\Data\Form $form */
35 | $form = $this->_formFactory->create(['data' => ['html_id_prefix' => '_image_']]);
36 |
37 | $model = $this->_coreRegistry->registry('');
38 |
39 | /*
40 | * Checking if user have permissions to save information
41 | */
42 | if ($this->_isAllowedAction('_::save')) {
43 | $isElementDisabled = false;
44 | } else {
45 | $isElementDisabled = true;
46 | }
47 |
48 | $layoutFieldset = $form->addFieldset(
49 | 'image_fieldset',
50 | ['legend' => __('Image Thumbnail'), 'class' => 'fieldset-wide', 'disabled' => $isElementDisabled]
51 | );
52 |
53 | $layoutFieldset->addField(
54 | 'image',
55 | 'image',
56 | [
57 | 'name' => 'image',
58 | 'label' => __('Image'),
59 | 'title' => __('Image'),
60 | 'required' => false,
61 | 'disabled' => $isElementDisabled
62 | ]
63 | );
64 |
65 | $this->_eventManager->dispatch('adminhtml__edit_tab_image_prepare_form', ['form' => $form]);
66 |
67 | $form->setValues($model->getData());
68 |
69 | $this->setForm($form);
70 |
71 | return parent::_prepareForm();
72 | }
73 |
74 | /**
75 | * Prepare label for tab
76 | *
77 | * @return string
78 | */
79 | public function getTabLabel()
80 | {
81 | return __('Image Thumbnail');
82 | }
83 |
84 | /**
85 | * Prepare title for tab
86 | *
87 | * @return string
88 | */
89 | public function getTabTitle()
90 | {
91 | return __('Image Thumbnail');
92 | }
93 |
94 | /**
95 | * {@inheritdoc}
96 | */
97 | public function canShowTab()
98 | {
99 | return true;
100 | }
101 |
102 | /**
103 | * {@inheritdoc}
104 | */
105 | public function isHidden()
106 | {
107 | return false;
108 | }
109 |
110 | /**
111 | * Check permission for passed action
112 | *
113 | * @param string $resourceId
114 | * @return bool
115 | */
116 | protected function _isAllowedAction($resourceId)
117 | {
118 | return $this->_authorization->isAllowed($resourceId);
119 | }
120 |
121 | /**
122 | * Return predefined additional element types
123 | *
124 | * @return array
125 | */
126 | protected function _getAdditionalElementTypes()
127 | {
128 | return ['image' => '\\Block\Adminhtml\Form\Element\Image'];
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Block/Adminhtml/Module/Edit/Tab/Main.php:
--------------------------------------------------------------------------------
1 | \\Block\Adminhtml\\Edit\Tab;
3 |
4 | /**
5 | * Cms page edit form main tab
6 | */
7 | class Main extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
8 | {
9 | /**
10 | * @var \Magento\Store\Model\System\Store
11 | */
12 | protected $_systemStore;
13 |
14 | /**
15 | * @param \Magento\Backend\Block\Template\Context $context
16 | * @param \Magento\Framework\Registry $registry
17 | * @param \Magento\Framework\Data\FormFactory $formFactory
18 | * @param \Magento\Store\Model\System\Store $systemStore
19 | * @param array $data
20 | */
21 | public function __construct(
22 | \Magento\Backend\Block\Template\Context $context,
23 | \Magento\Framework\Registry $registry,
24 | \Magento\Framework\Data\FormFactory $formFactory,
25 | \Magento\Store\Model\System\Store $systemStore,
26 | array $data = []
27 | ) {
28 | $this->_systemStore = $systemStore;
29 | parent::__construct($context, $registry, $formFactory, $data);
30 | }
31 |
32 | /**
33 | * Prepare form
34 | *
35 | * @return $this
36 | */
37 | protected function _prepareForm()
38 | {
39 | /* @var $model \Magento\Cms\Model\Page */
40 | $model = $this->_coreRegistry->registry('');
41 |
42 | /*
43 | * Checking if user have permissions to save information
44 | */
45 | if ($this->_isAllowedAction('_::save')) {
46 | $isElementDisabled = false;
47 | } else {
48 | $isElementDisabled = true;
49 | }
50 |
51 | /** @var \Magento\Framework\Data\Form $form */
52 | $form = $this->_formFactory->create();
53 |
54 | $form->setHtmlIdPrefix('_main_');
55 |
56 | $fieldset = $form->addFieldset('base_fieldset', ['legend' => __(' Information')]);
57 |
58 | if ($model->getId()) {
59 | $fieldset->addField('_id', 'hidden', ['name' => '_id']);
60 | }
61 |
62 | $fieldset->addField(
63 | 'title',
64 | 'text',
65 | [
66 | 'name' => 'title',
67 | 'label' => __(' Title'),
68 | 'title' => __(' Title'),
69 | 'required' => true,
70 | 'disabled' => $isElementDisabled
71 | ]
72 | );
73 |
74 | $fieldset->addField(
75 | 'author',
76 | 'text',
77 | [
78 | 'name' => 'author',
79 | 'label' => __('Author'),
80 | 'title' => __('Author'),
81 | 'required' => true,
82 | 'disabled' => $isElementDisabled
83 | ]
84 | );
85 |
86 | $dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
87 | $fieldset->addField('published_at', 'date', [
88 | 'name' => 'published_at',
89 | 'date_format' => $dateFormat,
90 | 'image' => $this->getViewFileUrl('images/grid-cal.gif'),
91 | 'value' => $model->getPublishedAt(),
92 | 'label' => __('Publishing Date'),
93 | 'title' => __('Publishing Date'),
94 | 'required' => true
95 | ]);
96 |
97 | $this->_eventManager->dispatch('adminhtml__edit_tab_main_prepare_form', ['form' => $form]);
98 |
99 | $form->setValues($model->getData());
100 | $this->setForm($form);
101 |
102 | return parent::_prepareForm();
103 | }
104 |
105 | /**
106 | * Prepare label for tab
107 | *
108 | * @return string
109 | */
110 | public function getTabLabel()
111 | {
112 | return __(' Information');
113 | }
114 |
115 | /**
116 | * Prepare title for tab
117 | *
118 | * @return string
119 | */
120 | public function getTabTitle()
121 | {
122 | return __(' Information');
123 | }
124 |
125 | /**
126 | * {@inheritdoc}
127 | */
128 | public function canShowTab()
129 | {
130 | return true;
131 | }
132 |
133 | /**
134 | * {@inheritdoc}
135 | */
136 | public function isHidden()
137 | {
138 | return false;
139 | }
140 |
141 | /**
142 | * Check permission for passed action
143 | *
144 | * @param string $resourceId
145 | * @return bool
146 | */
147 | protected function _isAllowedAction($resourceId)
148 | {
149 | return $this->_authorization->isAllowed($resourceId);
150 | }
151 | }
152 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Block/Adminhtml/Module/Edit/Tabs.php:
--------------------------------------------------------------------------------
1 | \\Block\Adminhtml\\Edit;
3 |
4 | /**
5 | * Admin left menu
6 | */
7 | class Tabs extends \Magento\Backend\Block\Widget\Tabs
8 | {
9 | /**
10 | * @return void
11 | */
12 | protected function _construct()
13 | {
14 | parent::_construct();
15 | $this->setId('page_tabs');
16 | $this->setDestElementId('edit_form');
17 | $this->setTitle(__(' Information'));
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Block/Adminhtml/Module/Grid.php:
--------------------------------------------------------------------------------
1 | \\Block\Adminhtml\;
3 |
4 | /**
5 | * Adminhtml grid
6 | */
7 | class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
8 | {
9 | /**
10 | * @var \\\Model\ResourceModel\\CollectionFactory
11 | */
12 | protected $_collectionFactory;
13 |
14 | /**
15 | * @var \\\Model\
16 | */
17 | protected $_;
18 |
19 | /**
20 | * @param \Magento\Backend\Block\Template\Context $context
21 | * @param \Magento\Backend\Helper\Data $backendHelper
22 | * @param \\\Model\ $Page
23 | * @param \\\Model\ResourceModel\\CollectionFactory $collectionFactory
24 | * @param \Magento\Core\Model\PageLayout\Config\Builder $pageLayoutBuilder
25 | * @param array $data
26 | */
27 | public function __construct(
28 | \Magento\Backend\Block\Template\Context $context,
29 | \Magento\Backend\Helper\Data $backendHelper,
30 | \\\Model\ $,
31 | \\\Model\ResourceModel\\CollectionFactory $collectionFactory,
32 | array $data = []
33 | ) {
34 | $this->_collectionFactory = $collectionFactory;
35 | $this->_ = $;
36 | parent::__construct($context, $backendHelper, $data);
37 | }
38 |
39 | /**
40 | * @return void
41 | */
42 | protected function _construct()
43 | {
44 | parent::_construct();
45 | $this->setId('Grid');
46 | $this->setDefaultSort('_id');
47 | $this->setDefaultDir('DESC');
48 | $this->setUseAjax(true);
49 | $this->setSaveParametersInSession(true);
50 | }
51 |
52 | /**
53 | * Prepare collection
54 | *
55 | * @return \Magento\Backend\Block\Widget\Grid
56 | */
57 | protected function _prepareCollection()
58 | {
59 | $collection = $this->_collectionFactory->create();
60 | /* @var $collection \\\Model\ResourceModel\\Collection */
61 | $this->setCollection($collection);
62 |
63 | return parent::_prepareCollection();
64 | }
65 |
66 | /**
67 | * Prepare columns
68 | *
69 | * @return \Magento\Backend\Block\Widget\Grid\Extended
70 | */
71 | protected function _prepareColumns()
72 | {
73 | $this->addColumn('_id', [
74 | 'header' => __('ID'),
75 | 'index' => '_id',
76 | ]);
77 |
78 | $this->addColumn('title', ['header' => __('Title'), 'index' => 'title']);
79 | $this->addColumn('author', ['header' => __('Author'), 'index' => 'author']);
80 |
81 | $this->addColumn(
82 | 'published_at',
83 | [
84 | 'header' => __('Published On'),
85 | 'index' => 'published_at',
86 | 'type' => 'date',
87 | 'header_css_class' => 'col-date',
88 | 'column_css_class' => 'col-date'
89 | ]
90 | );
91 |
92 | $this->addColumn(
93 | 'created_at',
94 | [
95 | 'header' => __('Created'),
96 | 'index' => 'created_at',
97 | 'type' => 'datetime',
98 | 'header_css_class' => 'col-date',
99 | 'column_css_class' => 'col-date'
100 | ]
101 | );
102 |
103 | $this->addColumn(
104 | 'action',
105 | [
106 | 'header' => __('Edit'),
107 | 'type' => 'action',
108 | 'getter' => 'getId',
109 | 'actions' => [
110 | [
111 | 'caption' => __('Edit'),
112 | 'url' => [
113 | 'base' => '*/*/edit',
114 | 'params' => ['store' => $this->getRequest()->getParam('store')]
115 | ],
116 | 'field' => '_id'
117 | ]
118 | ],
119 | 'sortable' => false,
120 | 'filter' => false,
121 | 'header_css_class' => 'col-action',
122 | 'column_css_class' => 'col-action'
123 | ]
124 | );
125 |
126 | return parent::_prepareColumns();
127 | }
128 |
129 | /**
130 | * Row click url
131 | *
132 | * @param \Magento\Framework\Object $row
133 | * @return string
134 | */
135 | public function getRowUrl($row)
136 | {
137 | return $this->getUrl('*/*/edit', ['_id' => $row->getId()]);
138 | }
139 |
140 | /**
141 | * Get grid url
142 | *
143 | * @return string
144 | */
145 | public function getGridUrl()
146 | {
147 | return $this->getUrl('*/*/grid', ['_current' => true]);
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Block/Module.php:
--------------------------------------------------------------------------------
1 | \\Block;
4 |
5 | /**
6 | * content block
7 | */
8 | class extends \Magento\Framework\View\Element\Template
9 | {
10 | /**
11 | * collection
12 | *
13 | * @var \\Model\ResourceModel\\Collection
14 | */
15 | protected $_Collection = null;
16 |
17 | /**
18 | * factory
19 | *
20 | * @var \\\Model\Factory
21 | */
22 | protected $_CollectionFactory;
23 |
24 | /** @var \\\Helper\Data */
25 | protected $_dataHelper;
26 |
27 | /**
28 | * @param \Magento\Framework\View\Element\Template\Context $context
29 | * @param \\\Model\ResourceModel\\CollectionFactory $CollectionFactory
30 | * @param array $data
31 | */
32 | public function __construct(
33 | \Magento\Framework\View\Element\Template\Context $context,
34 | \\\Model\ResourceModel\\CollectionFactory $CollectionFactory,
35 | \\\Helper\Data $dataHelper,
36 | array $data = []
37 | ) {
38 | $this->_CollectionFactory = $CollectionFactory;
39 | $this->_dataHelper = $dataHelper;
40 | parent::__construct(
41 | $context,
42 | $data
43 | );
44 | }
45 |
46 | /**
47 | * Retrieve collection
48 | *
49 | * @return \\Model\ResourceModel\\Collection
50 | */
51 | protected function _getCollection()
52 | {
53 | $collection = $this->_CollectionFactory->create();
54 | return $collection;
55 | }
56 |
57 | /**
58 | * Retrieve prepared collection
59 | *
60 | * @return \\Model\ResourceModel\\Collection
61 | */
62 | public function getCollection()
63 | {
64 | if (is_null($this->_Collection)) {
65 | $this->_Collection = $this->_getCollection();
66 | $this->_Collection->setCurPage($this->getCurrentPage());
67 | $this->_Collection->setPageSize($this->_dataHelper->getPerPage());
68 | $this->_Collection->setOrder('published_at','asc');
69 | }
70 |
71 | return $this->_Collection;
72 | }
73 |
74 | /**
75 | * Fetch the current page for the list
76 | *
77 | * @return int
78 | */
79 | public function getCurrentPage()
80 | {
81 | return $this->getData('current_page') ? $this->getData('current_page') : 1;
82 | }
83 |
84 | /**
85 | * Return URL to item's view page
86 | *
87 | * @param \\Model\ $Item
88 | * @return string
89 | */
90 | public function getItemUrl($Item)
91 | {
92 | return $this->getUrl('*/*/view', array('id' => $Item->getId()));
93 | }
94 |
95 | /**
96 | * Return URL for resized Item image
97 | *
98 | * @param \\Model\ $item
99 | * @param integer $width
100 | * @return string|false
101 | */
102 | public function getImageUrl($item, $width)
103 | {
104 | return $this->_dataHelper->resize($item, $width);
105 | }
106 |
107 | /**
108 | * Get a pager
109 | *
110 | * @return string|null
111 | */
112 | public function getPager()
113 | {
114 | $pager = $this->getChildBlock('_list_pager');
115 | if ($pager instanceof \Magento\Framework\Object) {
116 | $PerPage = $this->_dataHelper->getPerPage();
117 |
118 | $pager->setAvailableLimit([$PerPage => $PerPage]);
119 | $pager->setTotalNum($this->getCollection()->getSize());
120 | $pager->setCollection($this->getCollection());
121 | $pager->setShowPerPage(TRUE);
122 | $pager->setFrameLength(
123 | $this->_scopeConfig->getValue(
124 | 'design/pagination/pagination_frame',
125 | \Magento\Store\Model\ScopeInterface::SCOPE_STORE
126 | )
127 | )->setJump(
128 | $this->_scopeConfig->getValue(
129 | 'design/pagination/pagination_frame_skip',
130 | \Magento\Store\Model\ScopeInterface::SCOPE_STORE
131 | )
132 | );
133 |
134 | return $pager->toHtml();
135 | }
136 |
137 | return NULL;
138 | }
139 | }
140 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Block/View.php:
--------------------------------------------------------------------------------
1 | \\Block;
4 |
5 | class View extends \Magento\Framework\View\Element\Template
6 | {
7 |
8 | /**
9 | * Core registry
10 | *
11 | * @var \Magento\Framework\Registry
12 | */
13 | protected $_coreRegistry = null;
14 |
15 | /** @var \\\Helper\Data */
16 | protected $_dataHelper;
17 |
18 | /**
19 | * @param \Magento\Framework\View\Element\Template\Context $context
20 | * @param \Magento\Framework\Registry $registry
21 | * @param \Magento\Framework\App\Http\Context $httpContext
22 | * @param \Magento\Payment\Helper\Data $paymentHelper
23 | * @param array $data
24 | */
25 | public function __construct(
26 | \Magento\Framework\View\Element\Template\Context $context,
27 | \Magento\Framework\Registry $registry,
28 | \Magento\Framework\App\Http\Context $httpContext,
29 | \\\Helper\Data $dataHelper,
30 | array $data = []
31 | ) {
32 | $this->_coreRegistry = $registry;
33 | $this->httpContext = $httpContext;
34 | $this->_dataHelper = $dataHelper;
35 | parent::__construct($context, $data);
36 | }
37 |
38 | /**
39 | * @return void
40 | */
41 | protected function _prepareLayout()
42 | {
43 | $this->pageConfig->getTitle()->set($this->get()->getTitle());
44 | }
45 |
46 | /**
47 | * Retrieve current order model instance
48 | *
49 | * @return \\\Model\
50 | */
51 | public function get()
52 | {
53 | return $this->_coreRegistry->registry('current_');
54 | }
55 |
56 | /**
57 | * Return back url for logged in and guest users
58 | *
59 | * @return string
60 | */
61 | public function getBackUrl()
62 | {
63 | return $this->getUrl('/index/index');
64 | }
65 |
66 | /**
67 | * Return back title for logged in and guest users
68 | *
69 | * @return string
70 | */
71 | public function getBackTitle()
72 | {
73 | if ($this->httpContext->getValue(Context::CONTEXT_AUTH)) {
74 | return __('Back to My Orders');
75 | }
76 | return __('View Another Order');
77 | }
78 |
79 | /**
80 | * Return URL for resized Item image
81 | *
82 | * @param \\Model\ $item
83 | * @param integer $width
84 | * @return string|false
85 | */
86 | public function getImageUrl($item, $width)
87 | {
88 | return $this->_dataHelper->resize($item, $width);
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Controller/AbstractController/ModuleLoader.php:
--------------------------------------------------------------------------------
1 | \\Controller\AbstractController;
4 |
5 | use Magento\Framework\App\RequestInterface;
6 | use Magento\Framework\App\ResponseInterface;
7 | use Magento\Framework\Registry;
8 |
9 | class Loader implements LoaderInterface
10 | {
11 | /**
12 | * @var \\\Model\Factory
13 | */
14 | protected $Factory;
15 |
16 | /**
17 | * @var \Magento\Framework\Registry
18 | */
19 | protected $registry;
20 |
21 | /**
22 | * @var \Magento\Framework\UrlInterface
23 | */
24 | protected $url;
25 |
26 | /**
27 | * @param \\\Model\Factory $Factory
28 | * @param OrderViewAuthorizationInterface $orderAuthorization
29 | * @param Registry $registry
30 | * @param \Magento\Framework\UrlInterface $url
31 | */
32 | public function __construct(
33 | \\\Model\Factory $Factory,
34 | Registry $registry,
35 | \Magento\Framework\UrlInterface $url
36 | ) {
37 | $this->Factory = $Factory;
38 | $this->registry = $registry;
39 | $this->url = $url;
40 | }
41 |
42 | /**
43 | * @param RequestInterface $request
44 | * @param ResponseInterface $response
45 | * @return bool
46 | */
47 | public function load(RequestInterface $request, ResponseInterface $response)
48 | {
49 | $id = (int)$request->getParam('id');
50 | if (!$id) {
51 | $request->initForward();
52 | $request->setActionName('noroute');
53 | $request->setDispatched(false);
54 | return false;
55 | }
56 |
57 | $ = $this->Factory->create()->load($id);
58 | $this->registry->register('current_', $);
59 | return true;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Controller/AbstractController/ModuleLoaderInterface.php:
--------------------------------------------------------------------------------
1 | \\Controller\AbstractController;
4 |
5 | use Magento\Framework\App\RequestInterface;
6 | use Magento\Framework\App\ResponseInterface;
7 |
8 | interface LoaderInterface
9 | {
10 | /**
11 | * @param RequestInterface $request
12 | * @param ResponseInterface $response
13 | * @return \\\Model\
14 | */
15 | public function load(RequestInterface $request, ResponseInterface $response);
16 | }
17 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Controller/AbstractController/View.php:
--------------------------------------------------------------------------------
1 | \\Controller\AbstractController;
4 |
5 | use Magento\Framework\App\Action;
6 | use Magento\Framework\View\Result\PageFactory;
7 |
8 | abstract class View extends Action\Action
9 | {
10 | /**
11 | * @var \\\Controller\AbstractController\LoaderInterface
12 | */
13 | protected $Loader;
14 |
15 | /**
16 | * @var PageFactory
17 | */
18 | protected $resultPageFactory;
19 |
20 | /**
21 | * @param Action\Context $context
22 | * @param OrderLoaderInterface $orderLoader
23 | * @param PageFactory $resultPageFactory
24 | */
25 | public function __construct(Action\Context $context, LoaderInterface $Loader, PageFactory $resultPageFactory)
26 | {
27 | $this->Loader = $Loader;
28 | $this->resultPageFactory = $resultPageFactory;
29 | parent::__construct($context);
30 | }
31 |
32 | /**
33 | * view page
34 | *
35 | * @return void
36 | */
37 | public function execute()
38 | {
39 | if (!$this->Loader->load($this->_request, $this->_response)) {
40 | return;
41 | }
42 |
43 | /** @var \Magento\Framework\View\Result\Page $resultPage */
44 | $resultPage = $this->resultPageFactory->create();
45 | return $resultPage;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Controller/Adminhtml/Index/Delete.php:
--------------------------------------------------------------------------------
1 | \\Controller\Adminhtml\Index;
4 |
5 | class Delete extends \Magento\Backend\App\Action
6 | {
7 | /**
8 | * {@inheritdoc}
9 | */
10 | protected function _isAllowed()
11 | {
12 | return $this->_authorization->isAllowed('_::_delete');
13 | }
14 |
15 | /**
16 | * Delete action
17 | *
18 | * @return void
19 | */
20 | public function execute()
21 | {
22 | // check if we know what should be deleted
23 | $id = $this->getRequest()->getParam('_id');
24 | /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
25 | $resultRedirect = $this->resultRedirectFactory->create();
26 | if ($id) {
27 | $title = "";
28 | try {
29 | // init model and delete
30 | $model = $this->_objectManager->create('\\Model\');
31 | $model->load($id);
32 | $title = $model->getTitle();
33 | $model->delete();
34 | // display success message
35 | $this->messageManager->addSuccess(__('The data has been deleted.'));
36 | // go to grid
37 | return $resultRedirect->setPath('*/*/');
38 | } catch (\Exception $e) {
39 | // display error message
40 | $this->messageManager->addError($e->getMessage());
41 | // go back to edit form
42 | return $resultRedirect->setPath('*/*/edit', ['page_id' => $id]);
43 | }
44 | }
45 | // display error message
46 | $this->messageManager->addError(__('We can\'t find a data to delete.'));
47 | // go to grid
48 | return $resultRedirect->setPath('*/*/');
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Controller/Adminhtml/Index/Edit.php:
--------------------------------------------------------------------------------
1 | \\Controller\Adminhtml\Index;
4 |
5 | use Magento\Backend\App\Action;
6 |
7 | class Edit extends \Magento\Backend\App\Action
8 | {
9 | /**
10 | * Core registry
11 | *
12 | * @var \Magento\Framework\Registry
13 | */
14 | protected $_coreRegistry = null;
15 |
16 | /**
17 | * @var \Magento\Framework\View\Result\PageFactory
18 | */
19 | protected $resultPageFactory;
20 |
21 | /**
22 | * @param Action\Context $context
23 | * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
24 | * @param \Magento\Framework\Registry $registry
25 | */
26 | public function __construct(Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Magento\Framework\Registry $registry)
27 | {
28 | $this->resultPageFactory = $resultPageFactory;
29 | $this->_coreRegistry = $registry;
30 | parent::__construct($context);
31 | }
32 |
33 | /**
34 | * {@inheritdoc}
35 | */
36 | protected function _isAllowed()
37 | {
38 | return $this->_authorization->isAllowed('_::save');
39 | }
40 |
41 | /**
42 | * Init actions
43 | *
44 | * @return $this
45 | */
46 | protected function _initAction()
47 | {
48 | // load layout, set active menu and breadcrumbs
49 | /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
50 | $resultPage = $this->resultPageFactory->create();
51 | $resultPage->setActiveMenu(
52 | '_::_manage'
53 | )->addBreadcrumb(
54 | __(''),
55 | __('')
56 | )->addBreadcrumb(
57 | __('Manage '),
58 | __('Manage ')
59 | );
60 | return $resultPage;
61 | }
62 |
63 | /**
64 | * Edit CMS page
65 | *
66 | * @return void
67 | */
68 | public function execute()
69 | {
70 | // 1. Get ID and create model
71 | $id = $this->getRequest()->getParam('_id');
72 | $model = $this->_objectManager->create('\\Model\');
73 |
74 | // 2. Initial checking
75 | if ($id) {
76 | $model->load($id);
77 | if (!$model->getId()) {
78 | $this->messageManager->addError(__('This no longer exists.'));
79 | /** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
80 | $resultRedirect = $this->resultRedirectFactory->create();
81 | return $resultRedirect->setPath('*/*/');
82 | }
83 | }
84 |
85 | // 3. Set entered data if was error when we do save
86 | $data = $this->_objectManager->get('Magento\Backend\Model\Session')->getFormData(true);
87 | if (!empty($data)) {
88 | $model->setData($data);
89 | }
90 |
91 | // 4. Register model to use later in blocks
92 | $this->_coreRegistry->register('', $model);
93 |
94 | // 5. Build edit form
95 | /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
96 | $resultPage = $this->_initAction();
97 | $resultPage->addBreadcrumb(
98 | $id ? __('Edit ') : __('New '),
99 | $id ? __('Edit ') : __('New ')
100 | );
101 | $resultPage->getConfig()->getTitle()->prepend(__(''));
102 | $resultPage->getConfig()->getTitle()
103 | ->prepend($model->getId() ? $model->getTitle() : __('New '));
104 | return $resultPage;
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Controller/Adminhtml/Index/Grid.php:
--------------------------------------------------------------------------------
1 | \\Controller\Adminhtml\Index;
4 |
5 | class Grid extends \Magento\Customer\Controller\Adminhtml\Index
6 | {
7 | /**
8 | * Customer grid action
9 | *
10 | * @return void
11 | */
12 | public function execute()
13 | {
14 | $this->_view->loadLayout(false);
15 | $this->_view->renderLayout();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/blank/app/code/Namespace/Module/Controller/Adminhtml/Index/Index.php:
--------------------------------------------------------------------------------
1 | \\Controller\Adminhtml\Index;
4 |
5 | use Magento\Backend\App\Action\Context;
6 | use Magento\Framework\View\Result\PageFactory;
7 |
8 | class Index extends \Magento\Backend\App\Action
9 | {
10 | /**
11 | * @var PageFactory
12 | */
13 | protected $resultPageFactory;
14 |
15 | /**
16 | * @param Context $context
17 | * @param PageFactory $resultPageFactory
18 | */
19 | public function __construct(
20 | Context $context,
21 | PageFactory $resultPageFactory
22 | ) {
23 | parent::__construct($context);
24 | $this->resultPageFactory = $resultPageFactory;
25 | }
26 |
27 | /**
28 | * Check the permission to run it
29 | *
30 | * @return bool
31 | */
32 | protected function _isAllowed()
33 | {
34 | return $this->_authorization->isAllowed('_