Magento 2.0: Prepare yourself for a new way of module development

44
Join us in Ibiza September 24 th 27 th 2012

Transcript of Magento 2.0: Prepare yourself for a new way of module development

Page 1: Magento 2.0: Prepare yourself for a new way of module development

Join us in Ibiza September 24th – 27th 2012

Page 2: Magento 2.0: Prepare yourself for a new way of module development

Prepare yourself for a new way of module development

Magento 2.0:

Ivan Chepurnyi

Page 3: Magento 2.0: Prepare yourself for a new way of module development

About Me

• Devoted to Magento Platform, since May 2007

• Former Magento Core Team member

• More than 5 Years of Magento Development Experience

• Technical Director at EcomDev

• Magento Coach for European developers

Page 4: Magento 2.0: Prepare yourself for a new way of module development

Magento 1.x Issues

Page 5: Magento 2.0: Prepare yourself for a new way of module development

Module Functionality

Non-transparent Module Structure

LayoutPHP Classes

Static DataTranslations

Emails

Definition

Configuration

Templates

app/design

app/locale

app/etc/modulesskin

app/code

Page 6: Magento 2.0: Prepare yourself for a new way of module development

Excessive Configurations

• Info for building classes names of

• Models

• Blocks

• Helpers

• Info about file path

• Layout

• Translate

Page 7: Magento 2.0: Prepare yourself for a new way of module development

Performance

• Timings for app initialization

• Excessive memory usage for building of page layout

• Loading of redundant XML configurations for each request

Page 8: Magento 2.0: Prepare yourself for a new way of module development

Magento 2.0 Module Changes

Page 9: Magento 2.0: Prepare yourself for a new way of module development

Module Structure in Magento 2.0

app/code/<codePool>/<Namespace>/<Module>

Model

Helper

Block

controllers

etc

sql

data

view

locale

Classes that are used in MVC application

Configuration files

Setup Scripts

Layouts, Templates, Static Data

Translations

Page 10: Magento 2.0: Prepare yourself for a new way of module development

Refactored Configuration

Page 11: Magento 2.0: Prepare yourself for a new way of module development

Changes in Main Configuration

• Definition of the module in app/etc/modules/<Module_Name>.xml moved to its etc/config.xml file

• Added option to specify dependency type

• Removed class aliases

• Fieldsets copy rules moved to a separate file

• Simplified rewrite system

Page 12: Magento 2.0: Prepare yourself for a new way of module development

New Modules Bootstrap Logic

1. Merging only <modules /> nodes from the following file paths:

1. app/code/pool/Mage/<Module>/etc/config.xml

2. app/code/pool/<Namespace>/<Module>/etc/config.xml

3. app/etc/modules/<Namespace_ModuleName>.xml

Page 13: Magento 2.0: Prepare yourself for a new way of module development

New Modules Bootstrap Logic

2. Sorting of modules by dependency and checking module activity

3. Merging of the config.xml file from sorted and active modules

Page 14: Magento 2.0: Prepare yourself for a new way of module development

Dependency Types

• Hard Dependency (By Default)• Soft Dependency

Snippet:<Namespace_Module>

<depends>

<Mage_Category type=“soft”/>

<Mage_Core /> <!– This one is hard dependency

</depends>

</Namespace_Module>

Page 15: Magento 2.0: Prepare yourself for a new way of module development

No More Class Aliases

• A full class name specified in all factory calls• Mage::getModel(‘Namespace_Module_Model_Name’);

• Mage::helper(‘Namespace_Module_Helper_Name’);

• etc…

• Now all the factories use the same service locator

Page 16: Magento 2.0: Prepare yourself for a new way of module development

Rewrite Is Simplified

Rewrite is specified for class name instead of <models />, <helpers /> and <blocks /> nodes:<global> <rewrites> <ClassName_To_Rewrite>Class_That_Sustitutes</ClassName_To_Rewrite> </rewrites></global>

Page 17: Magento 2.0: Prepare yourself for a new way of module development

Configuration Changes In Admin Panel

1. New ACL and authorization system•Acl resources now placed at <Module>/etc/adminhtml/acl.xml

•It is even possible to connect own authentication model

2. Introduced Menu Builder•A separate xml file at <Module>/etc/adminhtml/menu.xml

•Menu is build by XML instructions: <add />, <update /> and <remove />

3. Added schema for these XML files validation

Page 18: Magento 2.0: Prepare yourself for a new way of module development

View Layer Changes

Page 19: Magento 2.0: Prepare yourself for a new way of module development

View Structure in Module

• Layout, templates, module CSS and JS files moved from <area>/base/default theme and skin to the module directory

• There is no more template and layout directories on view level

• Module has a view configuration file for defining own variables

Page 20: Magento 2.0: Prepare yourself for a new way of module development

View Directory

view

<area>

layout.xml

template.phtml

css/file.css

file.js

image.jpg

Magento Application Area (frontend, adminhtml, install)

Layout File that is defined in module config.xml

Template that is specified via layout or block construct

Static files that can be included into HTML markup via layout or template

Page 21: Magento 2.0: Prepare yourself for a new way of module development

View Configuration

• File is merged from all modules and current theme:

• <Module>/etc/view.xml

• <theme>/view.xml

• It has XML scheme for the validation of its content

• Can be used in feature for Design Editor

Page 22: Magento 2.0: Prepare yourself for a new way of module development

View Configuration Example

In module config or theme:<?xml version=“1.0”?><view> <vars module=”Namespace_Module”> <var name=“items_count”>10</var> </vars></view>

In template or block:$this->getVar(‘items_count’, ‘Namespace_Module’);

Page 23: Magento 2.0: Prepare yourself for a new way of module development

Changes in Layout

• Changes in layout building behavior• Hierarchical Layout Handles• Containers instead of structural blocks• New <move /> layout element

Page 24: Magento 2.0: Prepare yourself for a new way of module development

Layout building behavior

1. Adding layout handles updates

2. Extracting current handles and processing <update handle=“<name>”/> node

3. Transforming XML structure into array tree and sorting blocks within that tree without creating the block

4. Applying scheduled remove and move operations

5. Building blocks and containers from array tree

Page 25: Magento 2.0: Prepare yourself for a new way of module development

Hierarchical Page Handles

• Realized via attributes for layout handle:• type=“page”

• parent=“handle_name”

• Helps getting rid of layout duplicates

• Used to specify which layout handles are pages in Design Editor functionality

Page 26: Magento 2.0: Prepare yourself for a new way of module development

Example of Page Handle

<catalog_category_view translate="label” type="page” parent="default”> <!– some structure --><catalog_category_view>

<catalog_category_view_type_layered translate="label” type="page" parent="catalog_category_view”> <!– some structure --><catalog_category_view_type_layered>

Page 27: Magento 2.0: Prepare yourself for a new way of module development

No more structural blocks

• Blocks will be refactored to be a final unit of view

• Containers will replace structural blocks

• Containers are not objects, they are rendered and managed by layout model

Page 28: Magento 2.0: Prepare yourself for a new way of module development

Container Element

<container name=“unique_name”as=“alias_in_parent”before=“sibling_name”after=“sibling_name”

htmlTag=“div” htmlClass=“css-class” htmlId=“id-in-html”

label=“Container Name in Design Editor”><container /><block />

</container>

Same as for block

Container HTML properties(optional)

Container Name for Design Editor functionality

Page 29: Magento 2.0: Prepare yourself for a new way of module development

Move Statement

<move element=“name”

destination=“destination.element”as=“new_alias”

after=”sibling_name”before="sibling_name” />

The element that should be moved

Destination element in layout

Same as for block

Page 30: Magento 2.0: Prepare yourself for a new way of module development

Themes

Page 31: Magento 2.0: Prepare yourself for a new way of module development

Simplified Themes

• Themes become more simple and flexible• Only one configuration field in the admin panel

• It is possible to create as many inherited themes as you need

• Skin become a style/locale variation on theme level

• Strict files relation in theme to the module

Page 32: Magento 2.0: Prepare yourself for a new way of module development

Theme Definition

• Every theme is defined by theme.xml in its directory

• app/design/<area>/<package>/<theme>/theme.xml

• It contains:

• Requirements for Magento version

• Fallback information

• Name of the theme for admin user

Page 33: Magento 2.0: Prepare yourself for a new way of module development

Theme Definition

<design><package code=”package_code”>

<title>Default</title><theme version="2.0.0.0"

code=”theme_code” parent=“theme_code”>

<title>Default</title><requirements>

<magento_version from=”1.0.0.0”to=“1.0.0.0|*"/>

</requirements></theme>

</package></design>

Page 34: Magento 2.0: Prepare yourself for a new way of module development

Theme Definition

• package/title – package name, that is visible to admin user

• theme/title – theme name, that is visible to admin user

• package/@code – unique identifier of a package

• theme/@code – unique identifier of a theme within the package

Page 35: Magento 2.0: Prepare yourself for a new way of module development

Theme Definition

• theme/@version – internal version of theme

• theme/@parent – theme name that the current one is inherited

• magento_version/@from – minimal required Magento version for theme

• magento_version/@to - maximum compatible version of Magento for theme (can be a wildcard)

Page 36: Magento 2.0: Prepare yourself for a new way of module development

Theme Fallbacks

Fallback structure for dynamic files looks quite simple, but you should consider theme inheritance:

1. <theme>/<Namespace_Module>/layout.xml

2. <parent_theme>/<Namespace_Module>/layout.xml

3. <Module>/view/layout.xml

Page 37: Magento 2.0: Prepare yourself for a new way of module development

Skin Fallbacks

• Static files (JS, CSS, Images) should be placed in theme skin directory

• Theme can have multiple skins, the default skin is “default”

• Skin directory allows fallbacks on locale level

• <theme>/skin/<skin_code>/<locale_code>/file.js

• <theme>/skin/<skin_code>/file.js

• <theme>/skin/<skin_code>/<locale_code>/<Namespace_Module>/file.js

• <theme>/skin/<skin_code>/<Namespace_Module>/file.js

Page 38: Magento 2.0: Prepare yourself for a new way of module development

Localization Inheritance

Page 39: Magento 2.0: Prepare yourself for a new way of module development

Localization Inheritance

It is possible to define inheritance between locales in any xml file that is merged for global configuration:

<global><locale>

<inheritance><!-- Inheritance of UK Locale from US

one --> <en_GB>en_US</en_GB></inheritance>

</locale></global>

Page 40: Magento 2.0: Prepare yourself for a new way of module development

Developer Stuff

Page 41: Magento 2.0: Prepare yourself for a new way of module development

Developer Stuff

• dev/shell – same as Magento 1 shell directory• dev/tests – set of different test suites:

• integration – tests that require Magento initialization

• js – Java Script UnitTests

• unit – test that can be run without Magento

• performance – load tests

• static – code analysis tools

Page 42: Magento 2.0: Prepare yourself for a new way of module development

Developer Stuff

• dev/tools – tools for developer• migration – a set of tools for migration of Magento 1.x

module to 2.0

• classmap – generator of the class map

• batch_tests – batch test runner

Page 43: Magento 2.0: Prepare yourself for a new way of module development

Thank You

Page 44: Magento 2.0: Prepare yourself for a new way of module development

Your Questions

E-mail: [email protected]