Custom Modules
CoB custom modules - usually taxonomy, nodes and paragraphs.
Boston.gov Conventions
The following development conventions are being followed in developing boston.gov modules.
Module naming and location
City of Boston have the following naming and grouping conventions for custom modules:
Twig Templates
Templates for the component should be saved in:
module_name/templatesTo add a customized template, select a suggestion for the base (node, field, region etc), then
Save the template in the folder above
In the
module_name_theme()hook inmodule_name.moduleadd the following:function module_name_theme() { return [ 'template__suggestion_name' => [ 'base_hook' => 'region', 'path' => 'modules/custom/bos_components/modules/module_name/templates', 'template' => 'template--suggestion-name', ] ]; }If a new suggestion is needed, then add the following:
function module_name_theme_suggestions_XXX_alter(array &$suggestions, array $variables) { // Add a new suggestion based if ($variables['some_element'] == "some_condition") { $suggestions[] = "new__suggestion"; } }Where XXX is the appropriate entity type (node, field, region, etc etc) to add a suggestion to.
Theme overriding
Wherever possible, the style provided from the patterns library should be used. In practice this means that boston.gov can be styled by a Drupal developer ensuring that the twig template files provide HTML structured and given classes that the patterns library expects.
Should the need arise, then the patterns library style sheets can be overridden.  Typically this is done at the module level, although if multiple modules will use the override, consider placing it in the bos_theme theme.
To add overrides,
Create the style sheet
module_name.cssand appropriate markup in the relevant template (see above section),Save the stylesheet in:
module_name/cssUpdate (or create) the
module_name.libraries.ymlfile with the following:something.override: css: theme: css/module_name.css: {}Using a
module_name_preprocess_HOOK()hook inmodule_name.moduleattach the css where and only when it is required. For example:function module_name_preprocess_HOOK(&$variables) { $variables['#attached']['library'][] = "bos_modulename/something.override"; }
Using JavaScript
Wherever possible, JavaScript should not be used on boston.gov. This is to maintain compatibility with as many browsers as possible, and to maximize accessibility for screen readers etc.
Should the need arise, then a JavaScript library can be created and deployed.  Typically this is done at the module level, although if multiple modules will use the override, consider placing it in the bos_theme theme.
To add overrides,
Create the JavaScript library
module_name.js,Save the library in:
module_name/jsUpdate (or create) the
bos_modulename.libraries.ymlwith the JavaScript directive - for example you could add the following:something.javascript: js: js/module_name.js: {} js/module_name1.js: { browsers: { IE: 'IE 9', '!IE': FALSE } } js/module_name2.js: {attributes: {id: "component_nav_script"}} dependencies: - core/jqueryUsing a
bos_modulename_preprocess_HOOK()hook inbos_modulename.moduleattach the JavaScript library where and only when it is required. For example:function module_name_preprocess_HOOK(&$variables) { $variables['#attached']['library'][] = "module_name/something.javascript"; }
Site and Module Configuration
Drupal 8 defines settings and configurations in YML files with the actual "current" settings and configurations being stored in the Drupal (MySQL) database.
Importing YML configuration into the database.
YML files in a modulesdocroot/modules/custom/ ... /module_name/config/install folder will be imported into the database when a module is first installed. 
YML files in the docroot/../config/default/ folder will be imported into the database when the configuration is imported via the Drupal UI, or the drush command.
drush cimExporting all database settings to YML files.
Current (run-time) settings and configurations in the database can be exported to the docroot/../config/default/ folder via the Drupal UI, or the drush command.
drush cexDefining module-specific configurations using config_devel.
If the config_devel module is enabled then a modules configuration can be exported to the modules config/install folder.
The dependent configurations are defined in the module_name.info.yml file as follows:
config_devel:
  - module_name.settings
  - ...To export these configurations to the config/install folder use the following drush command:
drush cde module_nameConfiguration considerations
Modules should try to reuse
field.storage.entity-type.field_nameconfigurations wherever possible.field.storage.entity-type.field_nameconfigurations should be: 1. saved in the modules parent module (e.g.bos_componentsorbos_content)to enable sharing, and 2. added to the parentsconfig_develsection of the.info.ymlfile.
Last updated
Was this helpful?