Php:drupal8:console:scaffolding

De Les Tutoriaux du WebMestre
Aller à : navigation, rechercher

Cas concret création d'un squelette de module

La commande à passer

  • Quelles sont les commandes qui génère quelque chose :
jpmena@jpmena-HP ~/REFONTE_BOFIP/d8refbofip $ drupal list | grep generate
  -c, --generate-chain   Shows command options and arguments as yaml output to be used in chain command
  -i, --generate-inline  Shows command options and arguments as inline command
  -d, --generate-doc     Shows command options and arguments as markdown
 generate
  generate:authentication:provider  Generate an Authentication Provider
  generate:breakpoint               Generate breakpoint
  generate:command                  Generate commands for the console.
  generate:controller               Generate & Register a controller
  generate:doc:cheatsheet           Generate a printable cheatsheet for Commands
  generate:doc:dash                 Generate the DrupalConsole.docset package for Dash
  generate:doc:data                 Generate documentations for Commands.
  generate:doc:gitbook              Generate documentations for Commands
  generate:entity:bundle            Generate a new content type (node / entity bundle)
  generate:entity:config            Generate a new config entity
  generate:entity:content           Generate a new content entity
  generate:event:subscriber         Generate an event subscriber
  generate:form                     Generate a new "FormBase"
  generate:form:alter               Generate an implementation of hook_form_alter() or hook_form_FORM_ID_alter
  generate:form:config              Generate a new "ConfigFormBase"
  generate:help                     Generate an implementation of hook_help()
  generate:module                   Generate a module.
  generate:module:file              Generate a .module file
  generate:permissions              Generate module permissions
  generate:plugin:block             Generate a plugin block
  generate:plugin:ckeditorbutton    Generate CKEditor button plugin.
  generate:plugin:condition         Generate a plugin condition.
  generate:plugin:field             Generate field type, widget and formatter plugins.
  generate:plugin:fieldformatter    Generate field formatter plugin.
  generate:plugin:fieldtype         Generate field type plugin.
  generate:plugin:fieldwidget       Generate field widget plugin.
  generate:plugin:imageeffect       Generate image effect plugin.
  generate:plugin:imageformatter    Generate image formatter plugin.
  generate:plugin:mail              Generate a plugin mail
  generate:plugin:rest:resource     Generate plugin rest resource
  generate:plugin:rulesaction       Generate a plugin rule action
  generate:plugin:skeleton          Generate an implementation of a skeleton plugin for those plugins Drupal Console do not have a specific generator
  generate:plugin:type:annotation   Generate a plugin type with annotation discovery
  generate:plugin:type:yaml         Generate a plugin type with Yaml discovery
  generate:plugin:views:field       Generate a custom plugin view field.
  generate:post:update              commands.generate.post:update.description
  generate:profile                  Generate a profile.
  generate:routesubscriber          Generate a RouteSubscriber
  generate:service                  Generate service
  generate:theme                    Generate a theme.
  generate:twig:extension           Generate a Twig extension.
  generate:update                   Generate an implementation of hook_update_N()
  • l'aide de la commande generate:module
    • On voit que pour accéder à cette commande il faut être à la racine de notre instance Drupal cible
#je ne suis pas à la racine de mon site Drupal 8!!!!
jpmena@jpmena-HP ~ $ drupal help generate:module

                                                                  
  [Symfony\Component\Console\Exception\CommandNotFoundException]  
  There are no commands defined in the "generate" namespace.      
                                                                  

help [--xml] [--raw] [--format FORMAT] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env [ENV]] [--root [ROOT]] [--no-debug] [--learning] [-c|--generate-chain] [-i|--generate-inline] [-d|--generate-doc] [-t|--target [TARGET]] [-l|--uri URI] [-y|--yes] [--] <command> [<command_name>]
#je vais à la racine de mon site Drupal 8
pmena@jpmena-HP ~ $ cd REFONTE_BOFIP/d8refbofip/
## et je repasse la commande pour obtenir l'aide sur generate:module
jpmena@jpmena-HP ~/REFONTE_BOFIP/d8refbofip $ drupal help generate:module
Usage:
  generate:module [options]
  gm

Options:
      --module=MODULE                    The Module name
      --machine-name=MACHINE-NAME        The machine name (lowercase and underscore only)
      --module-path=MODULE-PATH          The path of the module
      --description[=DESCRIPTION]        Module description
      --core[=CORE]                      Core version
      --package[=PACKAGE]                Module package
      --module-file                      Add a .module file
      --features-bundle=FEATURES-BUNDLE  Define module as feature using the given Features bundle name
      --composer                         Add a composer.json file
      --dependencies[=DEPENDENCIES]      Module dependencies separated by commas (i.e. context, panels)
  -h, --help                             Display this help message
  -q, --quiet                            Suppress all output from the command
  -V, --version                          Display this application version
      --ansi                             Force ANSI output
      --no-ansi                          Disable ANSI output
  -n, --no-interaction                   Do not ask any interactive question
  -e, --env[=ENV]                        The Environment name [default: "prod"]
      --root[=ROOT]                      Define the Drupal root to be used in command execution
      --no-debug                         Switches off debug mode
      --learning                         Generate a verbose code output
  -c, --generate-chain                   Shows command options and arguments as yaml output to be used in chain command
  -i, --generate-inline                  Shows command options and arguments as inline command
  -d, --generate-doc                     Shows command options and arguments as markdown
  -t, --target[=TARGET]                  Site name you want to interact with (for local or remote sites)
  -l, --uri=URI                          URI of the Drupal site to use (for multi-site environments or when running on an alternate port)
  -y, --yes                              Skip confirmation and proceed
  -v|vv|vvv, --verbose                   Increase the verbosity of messages: 1 for normal output, 2 for more verbose output, and 3 for debug

Help:
 The generate:module command helps you generates a new module.

Je passe la commande

Génération du squelette de module

  • for de l'aide précédente, je choisis au passage la génération d'un fichier composer.json:
jpmena@jpmena-HP ~/REFONTE_BOFIP/d8refbofip $ drupal generate:module --module importdaisy --composer

 Enter the module machine name [importdaisy]:
 > 

 Enter the module Path [/modules/custom]:
 > 

 Enter module description [My Awesome Module]:
 > Module d'import de l'export initial pour Daisy

 Enter package name [Custom]:
 > Imports

 Enter Drupal Core version [8.x]:
 > 

 Do you want to generate a .module file (yes/no) [yes]:
 > no

 Define module as feature (yes/no) [no]:
 > yes

 Enter Features bundle name [default]:
 > daisy

 Would you like to add module dependencies (yes/no) [no]:
 > 


 Do you confirm generation? (yes/no) [yes]:
 > 

Generated or updated files
 Site path: /home/jpmena/REFONTE_BOFIP/d8refbofip
 1 - modules/custom/importdaisy/importdaisy.info.yml
 2 - modules/custom/importdaisy/importdaisy.features.yml
 3 - modules/custom/importdaisy/composer.json

Génération d'entités particulières portées par ce module

  • L'API relative aux Entités et expliquée sur une section Entité des la documentation officielle de l'API Drupal
  • Dans le cadre de mon projet je vais créer 2 content types:
    • Un content type Commentaire Législatif (nome de machine commentaire)
      • Il s'agit d'un texte officiel relatif à l'interprétation de la loi
    • Un content type de type Actualité (nom de machine actualite)
      • Il s'agit d'un contenu plus simple analogue à un article et destiné à informer des commentaires à paraître
  • La commande qui va bien:
jpmena@jpmena-HP ~/REFONTE_BOFIP/d8refbofip $ drupal help generate:entity:content
Usage:
  generate:entity:content [options]
  gect

Options:
      --module=MODULE              The Module name.
      --entity-class=ENTITY-CLASS  The content entity class
      --entity-name=ENTITY-NAME    The content entity name
      --base-path[=BASE-PATH]      The base-path for the content entity routes
      --label=LABEL                The label
      --has-bundles                Entity has bundles
      --is-translatable            Content entity translatable
  -h, --help                       Display this help message
  -q, --quiet                      Suppress all output from the command
  -V, --version                    Display this application version
      --ansi                       Force ANSI output
      --no-ansi                    Disable ANSI output
  -n, --no-interaction             Do not ask any interactive question
  -e, --env[=ENV]                  The Environment name [default: "prod"]
      --root[=ROOT]                Define the Drupal root to be used in command execution
      --no-debug                   Switches off debug mode
      --learning                   Generate a verbose code output
  -c, --generate-chain             Shows command options and arguments as yaml output to be used in chain command
  -i, --generate-inline            Shows command options and arguments as inline command
  -d, --generate-doc               Shows command options and arguments as markdown
  -t, --target[=TARGET]            Site name you want to interact with (for local or remote sites)
  -l, --uri=URI                    URI of the Drupal site to use (for multi-site environments or when running on an alternate port)
  -y, --yes                        Skip confirmation and proceed
  -v|vv|vvv, --verbose             Increase the verbosity of messages: 1 for normal output, 2 for more verbose output, and 3 for debug

Help:
 Generate a new content entity

Génération du type de contenu Commentaire

jpmena@jpmena-HP ~/REFONTE_BOFIP/d8refbofip $ drupal generate:entity:content
 Enter the module name [console_commands]:
 > importdaisy

 Enter the class of your new content entity [DefaultEntity]:
 > Commentaire

 Enter the machine name of your new content entity [commentaire]:
 > 

 Enter the label of your new content entity [Commentaire]:
 > Commentaire BOfiPI

 Enter the base-path for the content entity routes [/admin/structure]:
 > 


 Do you want this (content) entity to have bundles (yes/no) [no]:
 > yes

 Is your entity translatable (yes/no) [yes]:
 > no

Generated or updated files
 Site path: /home/jpmena/REFONTE_BOFIP/d8refbofip
 1 - modules/custom/importdaisy/importdaisy.permissions.yml
 2 - modules/custom/importdaisy/importdaisy.links.menu.yml
 3 - modules/custom/importdaisy/importdaisy.links.task.yml
 4 - modules/custom/importdaisy/importdaisy.links.action.yml
 5 - modules/custom/importdaisy/src/CommentaireAccessControlHandler.php
 6 - modules/custom/importdaisy/src/Entity/CommentaireInterface.php
 7 - modules/custom/importdaisy/src/Entity/Commentaire.php
 8 - modules/custom/importdaisy/src/CommentaireHtmlRouteProvider.php
 9 - modules/custom/importdaisy/src/Entity/CommentaireViewsData.php
 10 - modules/custom/importdaisy/src/CommentaireListBuilder.php
 11 - modules/custom/importdaisy/src/Form/CommentaireSettingsForm.php
 12 - modules/custom/importdaisy/src/Form/CommentaireForm.php
 13 - modules/custom/importdaisy/src/Form/CommentaireDeleteForm.php
 14 - modules/custom/importdaisy/commentaire.page.inc
 15 - modules/custom/importdaisy/templates/commentaire.html.twig
 16 - modules/custom/importdaisy/templates/commentaire-content-add-list.html.twig
 17 - modules/custom/importdaisy/importdaisy.module
 18 - modules/custom/importdaisy/importdaisy.module
 19 - modules/custom/importdaisy/importdaisy.module
 // generate:entity:config

 Enter the base-path for the config entity routes [/admin/structure]:
 >

Génération du type de contenu Actualité

jpmena@jpmena-HP ~/REFONTE_BOFIP/d8refbofip $ drupal generate:entity:content
 Enter the module name [console_commands]:
 > importdaisy

 Enter the class of your new content entity [DefaultEntity]:
 > Actualite

 Enter the machine name of your new content entity [actualite]:
 > 

 Enter the label of your new content entity [Actualite]:
 > Actualité BOfIPI

 Enter the base-path for the content entity routes [/admin/structure]:
 > 


 Do you want this (content) entity to have bundles (yes/no) [no]:
 > yes

 Is your entity translatable (yes/no) [yes]:
 > no

                                                                                                                        
 [WARNING] It looks like you have a hook_theme already declared                                                         
                                                                                                                        
 Please manually merge the two hook_theme() implementations in                                                          
                                                                                                                        
 /home/jpmena/REFONTE_BOFIP/d8refbofip/modules/custom/importdaisy/importdaisy.module                                    
                                                                                                                        

Generated or updated files
 Site path: /home/jpmena/REFONTE_BOFIP/d8refbofip
 1 - modules/custom/importdaisy/importdaisy.permissions.yml
 2 - modules/custom/importdaisy/importdaisy.links.menu.yml
 3 - modules/custom/importdaisy/importdaisy.links.task.yml
 4 - modules/custom/importdaisy/importdaisy.links.action.yml
 5 - modules/custom/importdaisy/src/ActualiteAccessControlHandler.php
 6 - modules/custom/importdaisy/src/Entity/ActualiteInterface.php
 7 - modules/custom/importdaisy/src/Entity/Actualite.php
 8 - modules/custom/importdaisy/src/ActualiteHtmlRouteProvider.php
 9 - modules/custom/importdaisy/src/Entity/ActualiteViewsData.php
 10 - modules/custom/importdaisy/src/ActualiteListBuilder.php
 11 - modules/custom/importdaisy/src/Form/ActualiteSettingsForm.php
 12 - modules/custom/importdaisy/src/Form/ActualiteForm.php
 13 - modules/custom/importdaisy/src/Form/ActualiteDeleteForm.php
 14 - modules/custom/importdaisy/actualite.page.inc
 15 - modules/custom/importdaisy/templates/actualite.html.twig
 16 - modules/custom/importdaisy/templates/actualite-content-add-list.html.twig
 17 - modules/custom/importdaisy/importdaisy.module
 18 - modules/custom/importdaisy/importdaisy.module
 // generate:entity:config

 Enter the base-path for the config entity routes [/admin/structure]:
 >

Activation du module

  • Tout ne semble pas au point!
    • Quand je tente d'activer le module j'ai la trace d'erreur suivante:
jpmena@jpmena-HP ~/REFONTE_BOFIP/d8refbofip $ drush en -y importdaisy 
The following extensions will be enabled: importdaisy
Do you really want to continue? (y/n): y
PHP Fatal error:  Cannot redeclare importdaisy_theme() (previously declared in /home/jpmena/REFONTE_BOFIP/d8refbofip/modules/custom/importdaisy/importdaisy.module:29) in /home/jpmena/REFONTE_BOFIP/d8refbofip/modules/custom/importdaisy/importdaisy.module on line 76
PHP Stack trace:
PHP   1. {main}() /usr/local/bin/drush:0
PHP   2. require() /usr/local/bin/drush:10
PHP   3. drush_startup() phar:///usr/local/bin/drush/drush:114
PHP   4. drush_main() phar:///usr/local/bin/drush/includes/startup.inc:325
PHP   5. Drush\Boot\BaseBoot->bootstrap_and_dispatch() phar:///usr/local/bin/drush/includes/preflight.inc:66
PHP   6. drush_dispatch() phar:///usr/local/bin/drush/lib/Drush/Boot/BaseBoot.php:67
PHP   7. drush_command() phar:///usr/local/bin/drush/includes/command.inc:185
PHP   8. _drush_invoke_hooks() phar:///usr/local/bin/drush/includes/command.inc:217
PHP   9. drush_pm_enable() phar:///usr/local/bin/drush/includes/command.inc:366
PHP  10. drush_module_enable() phar:///usr/local/bin/drush/commands/pm/pm.drush.inc:1167
PHP  11. drush_module_install() phar:///usr/local/bin/drush/commands/core/drupal/environment.inc:198
PHP  12. Drupal\Core\ProxyClass\Extension\ModuleInstaller->install() phar:///usr/local/bin/drush/commands/core/drupal/environment.inc:131
PHP  13. Drupal\Core\Extension\ModuleInstaller->install() /home/jpmena/REFONTE_BOFIP/d8refbofip/core/lib/Drupal/Core/ProxyClass/Extension/ModuleInstaller.php:83
PHP  14. Drupal\Core\Extension\ModuleHandler->load() /home/jpmena/REFONTE_BOFIP/d8refbofip/core/lib/Drupal/Core/Extension/ModuleInstaller.php:182
PHP  15. Drupal\Core\Extension\Extension->load() /home/jpmena/REFONTE_BOFIP/d8refbofip/core/lib/Drupal/Core/Extension/ModuleHandler.php:127
Drush command terminated abnormally due to an unrecoverable error.                                                                                                                                                             [error]
Error: Cannot redeclare importdaisy_theme() (previously declared in /home/jpmena/REFONTE_BOFIP/d8refbofip/modules/custom/importdaisy/importdaisy.module:29) in
/home/jpmena/REFONTE_BOFIP/d8refbofip/modules/custom/importdaisy/importdaisy.module, line 76