tripal / tripal_doc

Official Documentation for the Tripal Platform
https://tripaldoc.readthedocs.io/en/latest/
GNU General Public License v3.0
2 stars 3 forks source link

How to generate a Drupal 9 module using Tripal Docker #26

Open laceysanderson opened 1 year ago

laceysanderson commented 1 year ago

These instructions were demonstrated in the Cofest in San Diego. They show how to use drush generate to create a templated Drupal module within Tripal Docker as the first step to upgrading an extension module.

The Example I'm going to use:

Make an empty directory for your extension module. Name it the machine name you intend to use for your module.

mkdir potato_module
cd potato_module

Then start up Tripal Docker in such a way to mount your local potato_module directory inside a newly created container:

docker run --publish=9000:80 --name=potatodev -tid --volume=`pwd`:/var/www/drupal9/web/modules/potato_module tripalproject/tripaldocker:latest

Now you have a fully installed Tripal development environment where you can start working on the potato module in your local folder and all the changes will automatically be reflected in the Tripal site (i.e. docker container). This process is the same no matter where you are at in development of your Tripal 4 extension module (obviously using a git repo clone in later stages as compared to the empty directory above).

Now we are going to generate code for a Drupal module based on templates using Drush!

To use drush we are going to need to be inside the docker container since that is where it is installed. To do that we do the docker equivalent of ssh-ing into a server, which is to use docker exec to open an interactive terminal inside the container:

docker exec -it potatodev bash

Now you can run any commands just as if you had installed everything locally!

We are going to run drush generate without any arguements first in order to see all the automated generators available to us --mostly for future reference.

❯ docker exec -it potatodev bash
root@0335ed1a94c3:/var/www/drupal9/web# drush generate
Drupal Code Generator 11.4.0

Run `drush generate [command]` and answer a few questions in order to write starter code to your project.

Available commands:                                                          
_global:                                                                     
  composer (composer.json)                  Generates a composer.json file   
  controller                                Generates a controller           
  field                                     Generates a field                
  hook                                      Generates a hook                 
  install-file                              Generates an install file        
  javascript                                Generates Drupal JavaScript file 
  layout                                    Generates a layout               
  migration                                 Generates the yml and PHP class  
                                            for a Migration                  
  module                                    Generates Drupal module          
  module-file                               Generates a module file          
  phpstorm-metadata                         Generates PhpStorm metadata      
  render-element                            Generates Drupal render element  
  service-provider                          Generates a service provider     
drush:                                                                       
  drush:alias-file (daf)                    Generates a Drush site alias     
                                            file.                            
  drush:command-file (dcf)                  Generates a Drush command file.  
entity:                                                                      
  entity:bundle-class (bundle-class)        Generate a bundle class for a    
                                            content entity.                  
  entity:configuration (config-entity)      Generates configuration entity   
  entity:content (content-entity)           Generates content entity         
form:                                                                        
  form:config (config-form)                 Generates a configuration form   
  form:confirm (confirm-form)               Generates a confirmation form    
  form:simple (form)                        Generates simple form            
misc:                                                                        
  misc:apache-virtual-host                  Generates an Apache site         
(apache-virtual-host)                       configuration file               
  misc:html-page (html-page)                Generates a simple html page     
  misc:nginx-virtual-host                   Generates an Nginx site          
(nginx-virtual-host)                        configuration file               
  misc:project (project)                    Generates a composer project     
plugin:                                                                      
  plugin:action (action)                    Generates action plugin          
  plugin:block (block)                      Generates block plugin           
  plugin:ckeditor (ckeditor)                Generates CKEditor plugin        
  plugin:condition (condition)              Generates condition plugin       
  plugin:constraint (constraint)            Generates constraint plugin      
  plugin:entity-reference-selection         Generates entity reference       
(entity-reference-selection)                selection plugin                 
  plugin:field:formatter (field-formatter)  Generates field formatter plugin 
  plugin:field:type (field-type)            Generates field type plugin      
  plugin:field:widget (field-widget)        Generates field widget plugin    
  plugin:filter (filter)                    Generates filter plugin          
  plugin:manager                            Generates plugin manager         
  plugin:menu-link (menu-link)              Generates menu-link plugin       
  plugin:migrate:destination                Generates migrate destination    
(migrate-destination)                       plugin                           
  plugin:migrate:process (migrate-process)  Generates migrate process plugin 
  plugin:migrate:source (migrate-source)    Generates migrate source plugin  
  plugin:queue-worker (queue-worker)        Generates queue worker plugin    
  plugin:rest-resource (rest-resource)      Generates rest resource plugin   
  plugin:views:argument-default             Generates views default argument 
(views-argument-default)                    plugin                           
  plugin:views:field (views-field)          Generates views field plugin     
  plugin:views:style (views-style)          Generates views style plugin     
service:                                                                     
  service:access-checker (access-checker)   Generates an access checker      
                                            service                          
  service:breadcrumb-builder                Generates a breadcrumb builder   
(breadcrumb-builder)                        service                          
  service:cache-context (cache-context)     Generates a cache context        
                                            service                          
  service:custom (custom-service)           Generates a custom Drupal        
                                            service                          
  service:event-subscriber                  Generates an event subscriber    
(event-subscriber)                                                           
  service:logger (logger)                   Generates a logger service       
  service:middleware (middleware)           Generates a middleware           
  service:param-converter (param-converter) Generates a param converter      
                                            service                          
  service:path-processor (path-processor)   Generates a path processor       
                                            service                          
  service:request-policy (request-policy)   Generates a request policy       
                                            service                          
  service:response-policy (response-policy) Generates a response policy      
                                            service                          
  service:route-subscriber                  Generates a route subscriber     
(route-subscriber)                                                           
  service:theme-negotiator                  Generates a theme negotiator     
(theme-negotiator)                                                           
  service:twig-extension (twig-extension)   Generates Twig extension service 
  service:uninstall-validator               Generates a uninstall validator  
(uninstall-validator)                       service                          
test:                                                                        
  test:browser (browser-test)               Generates a browser based test   
  test:kernel (kernel-test)                 Generates a kernel based test    
  test:nightwatch (nightwatch-test)         Generates a nightwatch test      
  test:unit (unit-test)                     Generates a unit test            
  test:webdriver (webdriver-test)           Generates a test that supports   
                                            JavaScript                       
theme:                                                                       
  theme                                     Generates Drupal theme           
  theme:file                                Generates a theme file           
  theme:settings                            Generates Drupal                 
                                            theme-settings.php file          
yml:                                                                         
  yml:breakpoints (breakpoints)             Generates a breakpoints yml file 
  yml:links:action (action-links)           Generates a links.action yml     
                                            file                             
  yml:links:contextual (contextual-links)   Generates links.contextual yml   
                                            file                             
  yml:links:menu (menu-links)               Generates a links.menu yml file  
  yml:links:task (task-links)               Generates a links.task yml file  
  yml:module-info (module-info)             Generates a module info yml file 
  yml:module-libraries (module-libraries)   Generates module libraries yml   
                                            file                             
  yml:permissions (permissions)             Generates a permissions yml file 
  yml:routing (routing)                     Generates a routing yml file     
  yml:services (services)                   Generates a services yml file    
  yml:theme-info (theme-info)               Generates a theme info yml file  
  yml:theme-libraries (theme-libraries)     Generates theme libraries yml    
                                            file                             
root@0335ed1a94c3:/var/www/drupal9/web#

Now onto actually generating:

root@0335ed1a94c3:/var/www/drupal9/web# drush generate module

 Welcome to module generator!
––––––––––––––––––––––––––––––

 Module name [Web]:
 ➤ My Potato Module

 Module machine name [my_potato_module]:
 ➤ potato_module

 Module description [Provides additional functionality for the site.]:
 ➤ This module will integrate beautiful potato pictures in a Drupal site.

 Package [Custom]:
 ➤ Tripal Extensions

 Dependencies (comma separated):
 ➤ tripal, tripal_chado

 Would you like to create module file? [No]:
 ➤ Yes

 Would you like to create install file? [No]:
 ➤ Yes

 Would you like to create libraries.yml file? [No]:
 ➤ Yes

 Would you like to create permissions.yml file? [No]:
 ➤ Yes

 Would you like to create event subscriber? [No]:
 ➤ 

 Would you like to create block plugin? [No]:
 ➤ 

 Would you like to create a controller? [No]:
 ➤ Yes

 Would you like to create settings form? [No]:
 ➤ Yes

 The following directories and files have been created or updated:
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
 • /var/www/drupal9/web/modules/potato_module/potato_module.info.yml
 • /var/www/drupal9/web/modules/potato_module/potato_module.install
 • /var/www/drupal9/web/modules/potato_module/potato_module.libraries.yml
 • /var/www/drupal9/web/modules/potato_module/potato_module.links.menu.yml
 • /var/www/drupal9/web/modules/potato_module/potato_module.module
 • /var/www/drupal9/web/modules/potato_module/potato_module.permissions.yml
 • /var/www/drupal9/web/modules/potato_module/potato_module.routing.yml
 • /var/www/drupal9/web/modules/potato_module/config/schema/potato_module.schema.yml
 • /var/www/drupal9/web/modules/potato_module/src/Controller/PotatoModuleController.php
 • /var/www/drupal9/web/modules/potato_module/src/Form/SettingsForm.php

root@0335ed1a94c3:/var/www/drupal9/web#

And now you will have the files above both inside your container and locally!

This means you can open your editor of choice and edit the generated files added to your local potato_module directory to make it more specific to your extension module. You can also use the other generators we saw above to continually add to this module!

Make sure to enable your module to see the changes live on your site! enable-module