Drupal 8: PluginsA visual and technical deep dive.
!Twitter session tag: #d8plugins
Kris Vanderwater‣ Developer Evangelist, Acquia!‣ Drupal 8: Scotch Initiative Owner!‣ Twitter: @eclipsegc!‣ Co-Maintainer: Chaos Tools!‣ Maintainer: Contextual Administration
Drupal 8 Plugins: Topics
• What is a Plugin?
• Why Plugins?
• Benefits of Plugins
• Foundational Concepts
• Implementing Plugins
• Creating your own Plugin Type
Thank YouHelior Colorado
What is a Plugin?
What is a Plugin?Helior’s Definition: A discreet class that
executes an operation within the context of a given scope, as a means
to extend Drupal’s functionality. !!!!!
What is a Plugin?Helior’s Definition: A discreet class that
executes an operation within the context of a given scope, as a means
to extend Drupal’s functionality. !!
Kris’ Definition: A discoverable class that implements a particular interface which adds or extends functionality to
a pluggable subsystem.
What is a Plugin?
• Discoverable
What is a Plugin?
• Discoverable!• Object Oriented
What is a Plugin?
• Discoverable!• Object Oriented!• Interface Driven
What is a Plugin?
• Discoverable!• Object Oriented!• Interface Driven!• Swappable
What is a Plugin?
• Discoverable!• Object Oriented!• Interface Driven!• Swappable!• Drupal not required
Why Plugins?
Why Plugins?
• Drupal actually is kinda unique
Why Plugins?
• Drupal actually is kinda unique!• Unprecedented Configurability
Why Plugins?
• Drupal actually is kinda unique!• Unprecedented Configurability!• Absence of similar code
Why Plugins?
• Drupal actually is kinda unique!• Unprecedented Configurability!• Absence of similar code!• Other CMS don’t expose as much
Why Plugins?
• Drupal actually is kinda unique!• Unprecedented Configurability!• Absence of similar code!• Other CMS don’t expose as much!• Frameworks expect it to be hard coded
Benefits of Plugins:
Benefits of Plugins:
• Definition & Implementation are co-located
Drupal 7 hook_block_info()
Drupal 7 hook_block_info()
Drupal 7 hook_block_view()
Drupal 7 hook_block_view()
Drupal 8 SystemPoweredByBlock
Benefits of Plugins:
• Definition & Implementation are co-located!• Plugins are Object Oriented
Drupal 7 Block Hooks
• hook_block_info()!• hook_block_info_alter()!• hook_block_view()!• hook_block_view_alter()!• hook_block_view_MODULE_DELTA_alter()!• hook_block_configure()!• hook_block_save()
Drupal 8 BlockPluginInterface
• label()!• access()!• build()!• blockForm()!• blockValidate()!• blockSubmit()!• getMachineNameSuggestion()!• getVisibilityConditions()!• getVisibilityCondition()!• setVisibilityConfig()
Methods not implemented by BlockBase
• label()!• access()!• build()!• blockForm()!• blockValidate()!• blockSubmit()!• getMachineNameSuggestion()!• getVisibilityConditions()!• getVisibilityCondition()!• setVisibilityConfig()
Plugins are Extensible
• Inherit from base class!• Inherit from other plugins
Drupal 8 SystemPoweredByBlock
Benefits of Plugins:
• Definition & Implementation are co-located!• Plugins are Object Oriented!• Plugins are Lazy Loaded by default
Drupal 7 Block Hooks
• hook_block_info()!• hook_block_info_alter()!• hook_block_view()!• hook_block_view_alter()!• hook_block_view_MODULE_DELTA_alter()!• hook_block_configure()!• hook_block_save()
Drupal 8 System Blocks!Contents of core/modules/system/src/Plugin/Block
• SystemBrandingBlock.php!• SystemBreadcrumbBlock.php!• SystemHelpBlock.php!• SystemMainBlock.php!• SystemMenuBlock.php!• SystemPoweredByBlock.php
Benefits of Plugins:
• Definition & Implementation are co-located!• Plugins are Object Oriented!• Plugins are Lazy Loaded by default!• Common Pattern (Learn once, use every where)
Drupal 8 Core Plugin Types
• Actions
• Archivers
• Blocks
• CKEditor Plugins
• Conditions
• Editors
• Entity Reference Selectors
• Field Types
• Tool Tips
• Views Access
• Views Area
• Views Argument Defaults
• Views Argument Validators
• Views Cache
• Views Displays
• Views Display Extenders
• Views Exposed Forms
• Field Formatters
• Field Widgets
• Filters
• Image Effects
• Contextual Links
• Local Actions
• Local Tasks
• Quickedit Editors
• Search
• Views Fields
• Views Filters
• Views Joins
• Views Pagers
• Views Queries
• Views Relationships
• Views Rows
• Views Sorts
• Views Styles
• Views Wizards
Foundational Concepts:
Foundational Concepts:
• Autoloading
Foundational Concepts:
• Autoloading!• PSR-0 & PSR-4
Autoloading Concepts:
Autoloading Concepts:
• \Drupal\Core => “core/lib/Drupal/Core”,!• \Drupal\Component => “core/lib/Drupal/Component”,
PSR-0
Autoloading Concepts:
• \Drupal\Core => “core/lib/Drupal/Core”,!• \Drupal\Component => “core/lib/Drupal/Component”,
PSR-0
• \Drupal\block => “core/modules/block/src”,
PSR-4
Foundational Concepts:
• Autoloading!• PSR-0 & PSR-4!
• Dependency Injection
Dependency Injection:
Foundational Concepts:
• Autoloading!• PSR-0 & PSR-4!
• Dependency Injection!• Service Containers
Service Containers:
Service Containers:
Service Containers:
Service Containers:
Service Containers:
Service Containers:
Service Containers:
Service Containers:
Service Containers: services.core.yml
Foundational Concepts:
• Autoloading!• PSR-0 & PSR-4!
• Dependency Injection!• Service Containers!• Annotations
Annotations: @Block()
Annotations:
• Annotations are NOT CODE!
Annotations:
• Annotations are NOT CODE!!• yaml, json, {insert_serializer} are NOT CODE!
Annotations:
• Annotations are NOT CODE!!• yaml, json, {insert_serializer} are NOT CODE!!• Annotations have more in common serialization.
Annotations:
• Annotations are NOT CODE!!• yaml, json, {insert_serializer} are NOT CODE!!• Annotations have more in common serialization.!• Data not behavior.
To the Code!