Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Magento 2 module development
An introduction
Joke Puts - @jokeputs
PHP developer at PHPro
What is Magento?
Lets get that question out of the way
A commerce platform
Not just a framework
composer create-project symfony/framework-standard-edition
composer create-project magento/project-community-edition
It’s open source!
The community edition that is
https://github.com/magento/magento2
What's underneath the hood?
Well… a bit of everything
What’s this “module” thing?
An extension, a plugin, … a set of files that makes a feature
What can I do with it?
§Create a composer package
§Create customizations for your commerce platform
§Sell them on the Mage...
Edition 1-9 extensions
10-30
extensions
31-50
extensions
50+ extensions
Community 10% 53% 26% 11%
Enterprise 9% 32% 27% 32%
Setting it up
Not even a “Hello world”
Adding a customization to your installation?
Start in the app/code directory
Creating a reusable feature?
Create a compose...
§etc/module.xml
§registration.php
§composer.json
https://github.com/jokeputs/mage2-module-setup
registration.php
<?php
MagentoFrameworkComponentComponentRegistrar:
:register(
MagentoFrameworkComponentComponentRegistr
a...
etc/module.xml
<?xml version="1.0" ?>
<config>
<module
name="Vendor_Module"
setup_version="1.0.0" />
</config>
composer.json
{
"name": "vendor/module",
"type": "magento2-module",
"version": "1.0.0",
"require": {
"php": "~7.0.0|~7.1.0...
But wait... there’s more!
Or at least there should be
Make this guy your friend!
https://github.com/phpro/grumphp
grumphp.yml
phpcs:
standard: "ruleset.xml"
show_warnings: false
ignore_patterns:
- "vendor"
- "Test/Unit"
triggered_by: [p...
grumphp.yml
triggered_by: [php]
phpunit:
metadata:
priority: 100
grumphp.yml
git_blacklist:
keywords:
- "die("
- "var_dump("
- "exit;"
- "console.log("
triggered_by: [php,js]
Letting Magento know what’s up
Registering your module
bin/magento setup:upgrade
Lets go a little deeper
We only dipped our toe in the water
Model-View-Control pattern
§ Block
§ Controller
§ Model
§ Setup
§ etc
§ frontend
§ routes.xml
§ adminhtml
§ routes.xml
§ v...
… But it doesn’t do what I want
Change the “core”
Adapting the core
§Dependency injection
§Observers
§Plugins
Dependency injection
§DI via constructor
§Api: interfaces for a module
§etc/di.xml, etc/frontend/di.xml, etc/adminhtml/di....
DI: Class preferences
<preference
for="MagentoCustomerApiAddressRepositoryInter
face"
type="MagentoCustomerModelResourceMo...
DI: Constructor arguments
<type name="MagentoCustomerModelVisitor">
<arguments>
<argument name="ignoredUserAgents" xsi:typ...
Events
<event
name="admin_system_config_changed_section_catalo
g">
<observer
name="catalog_update_price_attribute"
instanc...
Plugins
• Modifies behavior of a public method of a class
• Before method
• Runs before the method
• Receives the argument...
di.xml
<type
name="MagentoEavModelEntityAttributeBackend
AbstractBackend">
<plugin name="attributeValidation"
type="Magent...
AttributeValidation.php
public function aroundValidate(AbstractBackend
$subject, Closure $proceed, DataObject $entity)
{
$...
The one tip…
You know, the one to rule them all
Look at the core
Know it and understand it
Can’t get enough?
I got just the thing for you
Thank you!
https://joind.in/talk/8a633
Introduction to Magento 2 module development - PHP Antwerp Meetup 2017
Upcoming SlideShare
Loading in …5
×

Introduction to Magento 2 module development - PHP Antwerp Meetup 2017

106 views

Published on

This presentation was given at a PHP Antwerp Meetup: https://www.meetup.com/phpantwerp/events/237632633/

Magento 2 is a commerce platform released since November 2015. In this introduction I’ll focus on the open source version, Magento Community and show you how easy it is to start creating modules for this platform. These modules can be used to customize your store or to publish on the Magento Marketplace.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Introduction to Magento 2 module development - PHP Antwerp Meetup 2017

  1. 1. Magento 2 module development An introduction Joke Puts - @jokeputs PHP developer at PHPro
  2. 2. What is Magento? Lets get that question out of the way
  3. 3. A commerce platform Not just a framework
  4. 4. composer create-project symfony/framework-standard-edition composer create-project magento/project-community-edition
  5. 5. It’s open source! The community edition that is https://github.com/magento/magento2
  6. 6. What's underneath the hood? Well… a bit of everything
  7. 7. What’s this “module” thing? An extension, a plugin, … a set of files that makes a feature
  8. 8. What can I do with it? §Create a composer package §Create customizations for your commerce platform §Sell them on the Magento Marketplace
  9. 9. Edition 1-9 extensions 10-30 extensions 31-50 extensions 50+ extensions Community 10% 53% 26% 11% Enterprise 9% 32% 27% 32%
  10. 10. Setting it up Not even a “Hello world”
  11. 11. Adding a customization to your installation? Start in the app/code directory Creating a reusable feature? Create a composer package
  12. 12. §etc/module.xml §registration.php §composer.json https://github.com/jokeputs/mage2-module-setup
  13. 13. registration.php <?php MagentoFrameworkComponentComponentRegistrar: :register( MagentoFrameworkComponentComponentRegistr ar::MODULE, 'Vendor_Module', __DIR__ );
  14. 14. etc/module.xml <?xml version="1.0" ?> <config> <module name="Vendor_Module" setup_version="1.0.0" /> </config>
  15. 15. composer.json { "name": "vendor/module", "type": "magento2-module", "version": "1.0.0", "require": { "php": "~7.0.0|~7.1.0", "magento/framework": "100.1.*" },
  16. 16. But wait... there’s more! Or at least there should be
  17. 17. Make this guy your friend! https://github.com/phpro/grumphp
  18. 18. grumphp.yml phpcs: standard: "ruleset.xml" show_warnings: false ignore_patterns: - "vendor" - "Test/Unit" triggered_by: [php]
  19. 19. grumphp.yml triggered_by: [php] phpunit: metadata: priority: 100
  20. 20. grumphp.yml git_blacklist: keywords: - "die(" - "var_dump(" - "exit;" - "console.log(" triggered_by: [php,js]
  21. 21. Letting Magento know what’s up Registering your module
  22. 22. bin/magento setup:upgrade
  23. 23. Lets go a little deeper We only dipped our toe in the water
  24. 24. Model-View-Control pattern § Block § Controller § Model § Setup § etc § frontend § routes.xml § adminhtml § routes.xml § view § frontend § adminhtml
  25. 25. … But it doesn’t do what I want Change the “core”
  26. 26. Adapting the core §Dependency injection §Observers §Plugins
  27. 27. Dependency injection §DI via constructor §Api: interfaces for a module §etc/di.xml, etc/frontend/di.xml, etc/adminhtml/di.xml §Class preferences §Constructor arguments
  28. 28. DI: Class preferences <preference for="MagentoCustomerApiAddressRepositoryInter face" type="MagentoCustomerModelResourceModelAddre ssRepository" />
  29. 29. DI: Constructor arguments <type name="MagentoCustomerModelVisitor"> <arguments> <argument name="ignoredUserAgents" xsi:type="array"> <item name="google1" xsi:type="string">Googlebot/1.0 (googlebot@googlebot.com http://googlebot.com/)</item> <item name="google2" xsi:type="string">Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)</item> <item name="google3" xsi:type="string">Googlebot/2.1 (+http://www.googlebot.com/bot.html)</item> </argument> </arguments> </type>
  30. 30. Events <event name="admin_system_config_changed_section_catalo g"> <observer name="catalog_update_price_attribute" instance="MagentoCatalogObserverSwitchPriceAt tributeScopeOnConfigChange" /> </event>
  31. 31. Plugins • Modifies behavior of a public method of a class • Before method • Runs before the method • Receives the arguments of the method • After method • Runs after completion of the method • Receives the output of the method • Around method • Runs both before and after the method • Receives the arguments of the method • Receives a callable, which will give the output of the method
  32. 32. di.xml <type name="MagentoEavModelEntityAttributeBackend AbstractBackend"> <plugin name="attributeValidation" type="MagentoCatalogPluginModelAttributeBac kendAttributeValidation"/> </type>
  33. 33. AttributeValidation.php public function aroundValidate(AbstractBackend $subject, Closure $proceed, DataObject $entity) { $isAllowedType = !empty(array_filter(array_map(function ($allowedEntity) use ($entity) { return $entity instanceof $allowedEntity; }, $this->allowedEntityTypes))); ... return $proceed($entity); }
  34. 34. The one tip… You know, the one to rule them all
  35. 35. Look at the core Know it and understand it
  36. 36. Can’t get enough? I got just the thing for you
  37. 37. Thank you! https://joind.in/talk/8a633

×