SlideShare a Scribd company logo
1 of 19
Writing your first Drupal 8
module with Router and
Block plugin
Vinay Abhang
(vinay.abhang@iksulaops.com)
Vinay Abhang
•I am a Drupal developer at Iksula
Services Pvt Ltd, based in
Mumbai, India.
•I have 2 years of experience in
building websites in Drupal.
Router
• Websites => Webpages => Paths (Routes)
• Routes => Controller (to display content)
• Routes => Permission (to restrict access)
• Routes => Menu link (to reach the route)
Naming and placing your D8
module
• Name your module
• It must start with an alphabet.
• It must contain only lower-case alphabets and underscores.
• It must not contain any spaces.
• It must be unique.
• It may not be any of the reserved terms, for e.g.: src, lib, js, css,
templates, vendor, images, files, includes, misc, assets, fixtures, Drupal
• Create a folder for your module
• /modules
• /sites/all/modules
Let D8 know about your
module
• {module_machine_name}.info.yml file.
• Three keys — name, type and core are required in
this file.
• This file stores the metadata about the module.
• name, package, type, core, description
• Provides criteria to control module activation and
deactivation.
• Can specify dependencies (if any) of other
contributed modules and Drupal core modules.
Debugging .info.yml files
• Module is not listed on ‘Extend’ page
• Ensure the file is named correctly
{module_machine_name}.info.yml and is located in
module’s root.
• Ensure that file has the line: “type: module”
• Module is listed but its checkbox is disabled
• Ensure that core compatibility is set to “core: 8.x”
• Ensure that all module dependencies are available
Adding a basic Controller
• Within your module folder, you should have the PSR-4
standard folder structure /src/Controller and inside this
folder you should have your BasicController.php
controller file.
• /src/Controller/BasicController.php
• Adding the Controller first, is part of a general D8
philosophy of, “Build a tool first, then wire it up”
• For wiring it up, we need to add a routing.yml file in
our module.
Add routing file
• In module’s root folder, where .info.yml file is
located, add a new file with following naming
convention:
• {module_machine_name}.routing.yml
• Required properties in defining a route.
• path, defaults and requirements
Traits
• Traits help you embed a set of properties and
methods in several independent classes that
could be living in different hierarchies.
Defining configuration in custom
module
• Configurations are defined in a file residing in
config/install sub-directory of module
• {module_machine_name}.settings.yml
• Name of the config file (minus the .yml
extension) is called the ‘configuration name’ in
the system, using which the configuration can be
referred.
Using configuration in custom
module
• Simplest way to use is via Drupal::config() static
method
$config = Drupal::config(‘d8_example.settings');
// Will print ‘Vinay Abhang'.
print $config->get(‘d8_example.name’);
// Will print 'en'.
print $config->get(‘d8_example.langcode’);
• Editing and updating the config can be done
using Drupal::service(‘config.factory’)-
>getEditable() method
$config = Drupal::service(‘config.factory')->getEditable('d8_example.settings');
// Set and save new message value.
$config->set(‘d8_example.name’, 'Vinay')->save();
Add a menu link
• Menu links are used to reach to the routes
defined in the routing file.
• These links are defined in the file residing in the
module’s root folder with following naming
convention:
• {module_machine_name}.links.menu.yml
Creating menu tabs
• This is similar to the concept of D7’s
‘MENU_LOCAL_TASK’ menu type in
hook_menu.
• In D8, we need to create a file with following
naming convention:
• {module_machine_name}.links.task.yml
• It is advised to name the plugin ID same as the
route name.
Block Plugin
• In D8, blocks are instances of the Block Plugin
• Drupal’s block manager scans all the modules for any classes that contain
a @Block Annotation
• @Block Annotation has properties
• id : machine name for the block
• admin_label : block name to be listed on the administrative block
configuration page
• The block file resides in the src/Plugin/Block folder structure in your
module
• To add a block, go to Structure > Block Layout and click on ‘Place Block’
button associated with each available region.
Add a form to Block Configuration
• Provides an ability to enter a piece of
configuration for each instance of custom block
• This makes the configuration exportable using
Configuration Management
• The form is first defined in the blockForm()
method with a reference to its parent class using
• $form = parent::blockForm($form, $form_state)
Process the Block Config
Form
• Adding following method means that the form will
process and the input to the form will be saved in
the configuration for that instance of the block.
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['hello_name'] = $form_state->getValue('example_block_name');
}
Use Config in Block
Display
• To make use of the configuration of instances of
the block, the build() method of custom block
class is used
• $config = $this->getConfiguration()
Questions?
Thank You!

More Related Content

Similar to Writing your first Drupal 8 module with Router and Block plugin

Odoo (Build module, Security, ORM)
Odoo (Build module, Security, ORM)Odoo (Build module, Security, ORM)
Odoo (Build module, Security, ORM)sroo galal
 
Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2Anil Sagar
 
Drupal module development
Drupal module developmentDrupal module development
Drupal module developmentRachit Gupta
 
Intro to Drupal Module Developement
Intro to Drupal Module DevelopementIntro to Drupal Module Developement
Intro to Drupal Module DevelopementMatt Mendonca
 
Top 20 mistakes you will make on your 1st Drupal project
Top 20 mistakes you will make on your 1st Drupal projectTop 20 mistakes you will make on your 1st Drupal project
Top 20 mistakes you will make on your 1st Drupal projectIztok Smolic
 
Magento mega menu extension
Magento mega menu extensionMagento mega menu extension
Magento mega menu extensionBun Danny
 
Your first d8 module
Your first d8 moduleYour first d8 module
Your first d8 moduletedbow
 
Drupal: Reusing functionality
Drupal: Reusing functionalityDrupal: Reusing functionality
Drupal: Reusing functionalityRaymond Muilwijk
 
Oleksandr Medvediev - Content delivery tools in Drupal 8.
Oleksandr Medvediev - Content delivery tools in Drupal 8.Oleksandr Medvediev - Content delivery tools in Drupal 8.
Oleksandr Medvediev - Content delivery tools in Drupal 8.DrupalCamp Kyiv
 
Drupal Camp Porto - Developing with Drupal: First Steps
Drupal Camp Porto - Developing with Drupal: First StepsDrupal Camp Porto - Developing with Drupal: First Steps
Drupal Camp Porto - Developing with Drupal: First StepsLuís Carneiro
 
Magento 2.0: Prepare yourself for a new way of module development
Magento 2.0: Prepare yourself for a new way of module developmentMagento 2.0: Prepare yourself for a new way of module development
Magento 2.0: Prepare yourself for a new way of module developmentIvan Chepurnyi
 
13th Sep, Drupal 7 advanced training by TCS
13th Sep, Drupal 7 advanced training by TCS 13th Sep, Drupal 7 advanced training by TCS
13th Sep, Drupal 7 advanced training by TCS DrupalMumbai
 
Drupal 7 Features - Introduction - Basics
Drupal 7 Features - Introduction - BasicsDrupal 7 Features - Introduction - Basics
Drupal 7 Features - Introduction - BasicsDhinakaran Mani
 
13th Sep - Drupal Global Training Day by TCS - Drupal core advanced overview
13th Sep - Drupal Global Training Day by TCS - Drupal core advanced overview13th Sep - Drupal Global Training Day by TCS - Drupal core advanced overview
13th Sep - Drupal Global Training Day by TCS - Drupal core advanced overviewDrupalMumbai
 
Drupal Theming for Developers
Drupal Theming for DevelopersDrupal Theming for Developers
Drupal Theming for DevelopersIan Carnaghan
 
Building a Drupal Distribution using Features, Drush Make, Installation Profi...
Building a Drupal Distribution using Features, Drush Make, Installation Profi...Building a Drupal Distribution using Features, Drush Make, Installation Profi...
Building a Drupal Distribution using Features, Drush Make, Installation Profi...Ben Shell
 
SynapseIndia drupal presentation on drupal best practices
SynapseIndia drupal  presentation on drupal best practicesSynapseIndia drupal  presentation on drupal best practices
SynapseIndia drupal presentation on drupal best practicesSynapseindiappsdevelopment
 
Drupal8 corporate training in Hyderabad
Drupal8 corporate training in HyderabadDrupal8 corporate training in Hyderabad
Drupal8 corporate training in Hyderabadphp2ranjan
 
Modul-Entwicklung für Magento, OXID eShop und Shopware (2013)
Modul-Entwicklung für Magento, OXID eShop und Shopware (2013)Modul-Entwicklung für Magento, OXID eShop und Shopware (2013)
Modul-Entwicklung für Magento, OXID eShop und Shopware (2013)Roman Zenner
 
Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Drupalcon Paris
 

Similar to Writing your first Drupal 8 module with Router and Block plugin (20)

Odoo (Build module, Security, ORM)
Odoo (Build module, Security, ORM)Odoo (Build module, Security, ORM)
Odoo (Build module, Security, ORM)
 
Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2
 
Drupal module development
Drupal module developmentDrupal module development
Drupal module development
 
Intro to Drupal Module Developement
Intro to Drupal Module DevelopementIntro to Drupal Module Developement
Intro to Drupal Module Developement
 
Top 20 mistakes you will make on your 1st Drupal project
Top 20 mistakes you will make on your 1st Drupal projectTop 20 mistakes you will make on your 1st Drupal project
Top 20 mistakes you will make on your 1st Drupal project
 
Magento mega menu extension
Magento mega menu extensionMagento mega menu extension
Magento mega menu extension
 
Your first d8 module
Your first d8 moduleYour first d8 module
Your first d8 module
 
Drupal: Reusing functionality
Drupal: Reusing functionalityDrupal: Reusing functionality
Drupal: Reusing functionality
 
Oleksandr Medvediev - Content delivery tools in Drupal 8.
Oleksandr Medvediev - Content delivery tools in Drupal 8.Oleksandr Medvediev - Content delivery tools in Drupal 8.
Oleksandr Medvediev - Content delivery tools in Drupal 8.
 
Drupal Camp Porto - Developing with Drupal: First Steps
Drupal Camp Porto - Developing with Drupal: First StepsDrupal Camp Porto - Developing with Drupal: First Steps
Drupal Camp Porto - Developing with Drupal: First Steps
 
Magento 2.0: Prepare yourself for a new way of module development
Magento 2.0: Prepare yourself for a new way of module developmentMagento 2.0: Prepare yourself for a new way of module development
Magento 2.0: Prepare yourself for a new way of module development
 
13th Sep, Drupal 7 advanced training by TCS
13th Sep, Drupal 7 advanced training by TCS 13th Sep, Drupal 7 advanced training by TCS
13th Sep, Drupal 7 advanced training by TCS
 
Drupal 7 Features - Introduction - Basics
Drupal 7 Features - Introduction - BasicsDrupal 7 Features - Introduction - Basics
Drupal 7 Features - Introduction - Basics
 
13th Sep - Drupal Global Training Day by TCS - Drupal core advanced overview
13th Sep - Drupal Global Training Day by TCS - Drupal core advanced overview13th Sep - Drupal Global Training Day by TCS - Drupal core advanced overview
13th Sep - Drupal Global Training Day by TCS - Drupal core advanced overview
 
Drupal Theming for Developers
Drupal Theming for DevelopersDrupal Theming for Developers
Drupal Theming for Developers
 
Building a Drupal Distribution using Features, Drush Make, Installation Profi...
Building a Drupal Distribution using Features, Drush Make, Installation Profi...Building a Drupal Distribution using Features, Drush Make, Installation Profi...
Building a Drupal Distribution using Features, Drush Make, Installation Profi...
 
SynapseIndia drupal presentation on drupal best practices
SynapseIndia drupal  presentation on drupal best practicesSynapseIndia drupal  presentation on drupal best practices
SynapseIndia drupal presentation on drupal best practices
 
Drupal8 corporate training in Hyderabad
Drupal8 corporate training in HyderabadDrupal8 corporate training in Hyderabad
Drupal8 corporate training in Hyderabad
 
Modul-Entwicklung für Magento, OXID eShop und Shopware (2013)
Modul-Entwicklung für Magento, OXID eShop und Shopware (2013)Modul-Entwicklung für Magento, OXID eShop und Shopware (2013)
Modul-Entwicklung für Magento, OXID eShop und Shopware (2013)
 
Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3
 

Recently uploaded

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 

Recently uploaded (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 

Writing your first Drupal 8 module with Router and Block plugin

  • 1. Writing your first Drupal 8 module with Router and Block plugin Vinay Abhang (vinay.abhang@iksulaops.com)
  • 2. Vinay Abhang •I am a Drupal developer at Iksula Services Pvt Ltd, based in Mumbai, India. •I have 2 years of experience in building websites in Drupal.
  • 3. Router • Websites => Webpages => Paths (Routes) • Routes => Controller (to display content) • Routes => Permission (to restrict access) • Routes => Menu link (to reach the route)
  • 4. Naming and placing your D8 module • Name your module • It must start with an alphabet. • It must contain only lower-case alphabets and underscores. • It must not contain any spaces. • It must be unique. • It may not be any of the reserved terms, for e.g.: src, lib, js, css, templates, vendor, images, files, includes, misc, assets, fixtures, Drupal • Create a folder for your module • /modules • /sites/all/modules
  • 5. Let D8 know about your module • {module_machine_name}.info.yml file. • Three keys — name, type and core are required in this file. • This file stores the metadata about the module. • name, package, type, core, description • Provides criteria to control module activation and deactivation. • Can specify dependencies (if any) of other contributed modules and Drupal core modules.
  • 6. Debugging .info.yml files • Module is not listed on ‘Extend’ page • Ensure the file is named correctly {module_machine_name}.info.yml and is located in module’s root. • Ensure that file has the line: “type: module” • Module is listed but its checkbox is disabled • Ensure that core compatibility is set to “core: 8.x” • Ensure that all module dependencies are available
  • 7. Adding a basic Controller • Within your module folder, you should have the PSR-4 standard folder structure /src/Controller and inside this folder you should have your BasicController.php controller file. • /src/Controller/BasicController.php • Adding the Controller first, is part of a general D8 philosophy of, “Build a tool first, then wire it up” • For wiring it up, we need to add a routing.yml file in our module.
  • 8. Add routing file • In module’s root folder, where .info.yml file is located, add a new file with following naming convention: • {module_machine_name}.routing.yml • Required properties in defining a route. • path, defaults and requirements
  • 9. Traits • Traits help you embed a set of properties and methods in several independent classes that could be living in different hierarchies.
  • 10. Defining configuration in custom module • Configurations are defined in a file residing in config/install sub-directory of module • {module_machine_name}.settings.yml • Name of the config file (minus the .yml extension) is called the ‘configuration name’ in the system, using which the configuration can be referred.
  • 11. Using configuration in custom module • Simplest way to use is via Drupal::config() static method $config = Drupal::config(‘d8_example.settings'); // Will print ‘Vinay Abhang'. print $config->get(‘d8_example.name’); // Will print 'en'. print $config->get(‘d8_example.langcode’); • Editing and updating the config can be done using Drupal::service(‘config.factory’)- >getEditable() method $config = Drupal::service(‘config.factory')->getEditable('d8_example.settings'); // Set and save new message value. $config->set(‘d8_example.name’, 'Vinay')->save();
  • 12. Add a menu link • Menu links are used to reach to the routes defined in the routing file. • These links are defined in the file residing in the module’s root folder with following naming convention: • {module_machine_name}.links.menu.yml
  • 13. Creating menu tabs • This is similar to the concept of D7’s ‘MENU_LOCAL_TASK’ menu type in hook_menu. • In D8, we need to create a file with following naming convention: • {module_machine_name}.links.task.yml • It is advised to name the plugin ID same as the route name.
  • 14. Block Plugin • In D8, blocks are instances of the Block Plugin • Drupal’s block manager scans all the modules for any classes that contain a @Block Annotation • @Block Annotation has properties • id : machine name for the block • admin_label : block name to be listed on the administrative block configuration page • The block file resides in the src/Plugin/Block folder structure in your module • To add a block, go to Structure > Block Layout and click on ‘Place Block’ button associated with each available region.
  • 15. Add a form to Block Configuration • Provides an ability to enter a piece of configuration for each instance of custom block • This makes the configuration exportable using Configuration Management • The form is first defined in the blockForm() method with a reference to its parent class using • $form = parent::blockForm($form, $form_state)
  • 16. Process the Block Config Form • Adding following method means that the form will process and the input to the form will be saved in the configuration for that instance of the block. /** * {@inheritdoc} */ public function blockSubmit($form, FormStateInterface $form_state) { $this->configuration['hello_name'] = $form_state->getValue('example_block_name'); }
  • 17. Use Config in Block Display • To make use of the configuration of instances of the block, the build() method of custom block class is used • $config = $this->getConfiguration()