Module Development Part 2 Magento 1 Stenik Group Ltd. Martin Grozdanov <martin@stenik.bg> Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license. 1
Last time Convention URLs’ Structure Design elements & Fallback Module Development - Basic Components © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license. 2
Table of Contents Set, Get & Save data in model Translations Blocks & Cache Admin Menu Admin Grid + Model Collection Model Create & Update via Admin 3 © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license. 3
Varien’s Magical Getters & Setters All Blocks and Models are extending Varien_Object Varien_Object contains $_data property - associative array getData(‘key’) and setData(‘key’, ‘value’) are used to manage the $_data Varien_Object contains Magical Getters & Setters which translates setFooBar(‘value’) to setData(‘foo_bar’, ‘value’) and getFooBar() to getData(‘foo_bar’) 4
Set Model Data & Save <?php … $model->setFirstname(‘John’); Model without data change does not save itself <?php … $model->setFooBar(‘barfoo’); $model->setFirstname(‘John’); $model->save(); ... 5
Translations Magento uses English => Lang mapping translates <?php … echo Mage::helper(‘myhelper’)->__(‘Text to translate’); ... Magento uses English => Lang mapping translates Translate mappings are located in several places. 1. Magento first checks the database for the text 2. If not there – checks theme’s translate.csv file 3. If not there – checks modules translate file. 6
Translations The DB translation mapping is located in the core_translate table. Not recommended to be used. Theme’s translation files are located in theme’s …/locale/<store_lang_code>/translate.csv For example …/locale/bg_BG/translate.csv Module’s translate file is located under app/locale/<store_lang_code>/<defined_file.csv> 7
Module Translate Definition Definition - In module’s config.xml in frontend/adminhtml … <translate> <modules> <SoftUni_Submission> <files> <default>SoftUni_Submission.csv</default> </files> </SoftUni_Submission> </modules> </translate> 8
Block Files Extend Mage_Core_Block_Abstract or Mage_Core_Block_Template (or other) Add your own methods which can be used in the template (if there is any) 9
How block cache works + setup If block can be cached – Magento cache’s it’s HTML Uses Cache Key to identify create or fetch the cache. Uses Cache Tags to clear the cache when needed. Setup: 1. Set cache lifetime using cache_lifetime data Number – Seconds FALSE – Forever NULL – No cache. Default. 2. Set Tags if you want invalidation. (cache_tags data) 3. Provide Cache Key info creating getCacheKeyInfo method. The method’s result shall be array of strings. 10
@see Mage_Cms ‘s /etc/adminhtml.xml Admin Menu Define the menu in module’s config.xml Define ACL for that menu. @see Mage_Cms ‘s /etc/adminhtml.xml 11
Admin controllers Must use the adminhtml frontName. Should have unique controller prefix. Should have _isAllowed method in the class The class should extend Mage_Adminhtml_Controller_Action 12
Admin – Creating Grid and Collection Create controller action Create Grid Container Block Create layout and use this block in the content area Create Model Collection Create Grid Block with columns Create gridAction if you want AJAX Reference: Adminhtml_Cms 13
What is registry Magento’s registry is global container for data for the current session. Usage: <?php … Mage::register(‘my_unique_key’, ‘value’); $value = Mage::registry(‘my_unique_key’); ... Registering the same key twice may cause error. 14
Admin – Create & Edit entities Create new and edit controller action. Register the object, so it can be used from the blocks Create Form Container Block Create Form Block Define them in the layout. Create save & delete actions Reference: Adminhtml_Cms 15
Event Bus – things you can do Dispatch event Register for event and process it’s data. Magento Events Cheat Sheet (1.9) - Nick Jones https://www.nicksays.co.uk/magento-events-cheat-sheet-1-9/ 16
© Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license. 17
Thank you 18
License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International" license 19 © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license. 19
Trainings @ Software University (SoftUni) Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University Foundation softuni.org Software University @ Facebook facebook.com/SoftwareUniversity Software University Forums forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license. 20