Wie programmiere ich ein Magento-Modul? Erste Schritte. David Fuhr, Flagbit GmbH & Co. KG Leipzig, 05.11.2010 Agenda • • • • Darstellung: Blocks, Templates und Layout XML Internationalisierung / Lokalisierung Datenhaltung: Models und Resource-Models Konfiguration: Widgets Verzeichnisse • app/code/<codepool>/<namespace>/ <modulname> – z.B app/code/community/Flagbit/FeedReader • app/etc/modules – Zentrale Stelle um Module zu registrieren app/etc/modules/ Flagbit_FeedReader.xml <?xml version="1.0" encoding="UTF-8"?> <config> <modules> <Flagbit_FeedReader> <active>true</active> <codePool>community</codePool> </Flagbit_FeedReader> </modules> </config> app/code/community/Flagbit/ FeedReader/etc/config.xml <?xml version="1.0" encoding="UTF-8"?> <config> <modules> <Flagbit_FeedReader> <version>0.0.1</version> </Flagbit_FeedReader> </modules> </config> Was bisher geschah... Blocks... .../Flagbit/FeedReader/etc/ config.xml <?xml version="1.0" encoding="UTF-8"?> <config> <!-- ... --> <global> <blocks> <flagbit_feedreader> <class>Flagbit_FeedReader_Block</class> </flagbit_feedreader> </blocks> </global> </config> .../Flagbit/FeedReader/Block/ View.php <?php /** * php doc here */ class Flagbit_FeedReader_Block_View extends Mage_Core_Block_Template { public function getFeed() { /* ... */ } } Was bisher geschah... ... und Layout XML • Erstellen • Einfügen • Entfernen • Verschieben von Blöcken im Layout .../Flagbit/FeedReader/etc/ config.xml <?xml version="1.0" encoding="UTF-8"?> <config> <!-- ... --> <frontend> <layout> <updates> <flagbit_feedreader> <file>flagbit_feedreader.xml</file> </flagbit_feedreader> </updates> </layout> </frontend> </config> .../design/frontend/base/default/ layout/flagbit_feedreader.xml <?xml version="1.0" encoding="UTF-8"?> <layout version="0.1.0"> <default> <reference name=“right"> <block type="flagbit_feedreader/view" name=“right.flagbit_feedreader.rightcol" template="flagbit_feedreader/right_col.phtml“ /> </reference> </default> </layout> .../base/default/template/ flagbit_feedreader/right_col.phtml <?php /* @var $this Flagbit_FeedReader_Block_View */ ?> <div class="block"> <div class="block-title“><strong><span> <?php echo $this->escapeHtml($this->__(‘News from out Feed')); ?> </span></strong></div> <div class="block-content"> <p><?php echo $this->escapeHtml($this->__(‘Hm... what Feed?')); ?></p> </div> </div> <?php endif; ?> Was bisher geschah... Awsum! .../design/frontend/base/default/ layout/flagbit_feedreader.xml <?xml version="1.0" encoding="UTF-8"?> <layout version="0.1.0"> <default> <reference name=“right"> <block type="flagbit_feedreader/view" name=“right.flagbit_feedreader.rightcol" template="flagbit_feedreader/right_col.phtml“ /> </reference> </default> </layout> Mehr mit Layout XML • Layout Handles (Beispiele) – catalog_product_view – checkout_cart_index – customer_account – customer_logged_in – PRODUCT_TYPE_configurable – ... i18n / l10n • Internationalisation – Schaffen der Vorraussetzungen für die... • Localisation – Die Umsetzung für eine Ziel-Locale .../base/default/template/ flagbit_feedreader/right_col.phtml <?php /* @var $this Flagbit_FeedReader_Block_View */ ?> <div class="block"> <div class="block-title“><strong><span> <?php echo $this->escapeHtml($this->__(‘News from out Feed')); ?> </span></strong></div> <div class="block-content"> <p><?php echo $this->escapeHtml($this->__(‘Hm... what Feed?')); ?></p> </div> </div> <?php endif; ?> .../Flagbit/FeedReader/etc/ config.xml (schon wieder) <?xml version="1.0" encoding="UTF-8"?> <config> <!-- ... --> <frontend> <!-- ... --> <translate> <modules> <Flagbit_FeedReader> <files> <default>Flagbit_FeedReader.csv</default> </files> </Flagbit_FeedReader> </modules> </translate> </frontend> </config> app/locale/de_DE/ Flagbit_FeedReader.csv "News from our Feed","Letzte Neuigkeiten" "Hm... what Feed?","Hm... welche Neuigkeiten?" Was bisher geschah... Awsum! Models... • Datenhaltung /-speicherung • Enthalten Berechnungen, interne Logik, Geschäftslogik, etc. • z.B. Mage_Catalog_Model_Product, Mage_Sales_Model_Order .../Flagbit/Faq/etc/config.xml (wieder mal) <?xml version="1.0" encoding="UTF-8"?> <config> <!-- ... --> <global> <!-- ... --> <models> <flagbit_feedreader> <class>Flagbit_FeedReader_Model</class> </flagbit_feedreader> </models> </global> <!-- ... --> </config> .../Flagbit/FeedReader/Model/ Feed.php <?php class Flagbit_FeedReader_Model_Feed extends Mage_Core_Model_Abstract { public function getFeed() { return Zend_Feed::import($this->getUri()); } } Was bisher geschah... Ein bisschen Demo-Voodoo... • Block/View.php public function getTitle() { return Mage::getModel('flagbit_feedreader/feed') ->setUri('http://feeds.feedburner.com/MageBlog') ->title(); } • template/flagbit_feedreader/right_col.phtml <?php echo $this->escapeHtml($this->__('News from ') . $this->getTitle()); ?> Awsum! Resource-Models... • Persistenz-Schicht zwischen Models und Datenbank • DBMS-spezifisch (Oracle und MSSQL-Support sind geplant): Enthalten SQL Widgets .../Flagbit/FeedReader/etc/ widget.xml <?xml version="1.0" encoding="UTF-8"?> <widgets> <flagbit_feedreader_sidebar type="flagbit_feedreader/view" translate="name description"> <name>FeedReader: Sidebar</name> <description type="desc">Adds a FeedReader block</description> <parameters> <uri translate="label"> <required>1</required> <visible>1</visible> <label>Feed URI</label> <type>text</type> </uri> </parameters> </flagbit_feedreader_sidebar> </widgets> Insert Widget... Was bisher geschah... Awsum! https://github.com/Flagbit/Magento-FeedReader http://www.mageblog.de/ http://www.flagbit.de/ Fragen?