Drupal 8 Developers Cookbook: Top-Level File Formats

Module .info.yml Format

name: My Great Drupal Project
type: module
description: Provides something cool and new that Drupal doesn't do on its own
package: GT
version: "8.x-1.0"
core: "8.x"
dependencies:
  - image
  - block
  - node
  - menu_ui
  - 'drupal:system (>= 8.5)'
project: "my_module"
project status url: https://my_server.localhost/release-history/my_module/8.x

Notes:

  • Only name, type, description, package, version, and core are required.  project and project status url should only be used if you have a Drupal distribution repository for your module

  • dependencies is a list of module machine names that are required for this module to function correctly.  The last one demonstrates how to also require specific versions of a module.

  • This same format is used for themes and profiles, but type should be changed to either 'theme' or 'profile' accordingly

Module .libraries.yml Format

default:
  css:
    theme:
      css/my_module.css: {}
  js:
    js/my_module.js: {}
  dependencies:
    - core/drupal
    - core/jquery

Notes:

  • All sections are optional, so only add what you need.  dependencies is only needed if you are making use of Drupal behaviors or JQuery functions in a custom JavaScript file

  • You can list multiple CSS or JavaScript files as needed.  Just follow the format shown below.

  • While not required, prefixing your CSS and JavaScript files with the machine name of your modules makes in-browser debugging a bit easier.

Module .routing.yml Format

my_module.my_route_id:
  path: 'somewhere/out/there'
  defaults:
    _controller: '\Drupal\my_module\Controller\MyController:myMethod'
    _title: "My Special Dynamic Drupal Page"
  requirements:
    _permission: 'administer nodes'

my_module.my_route_id2:
  path: 'somewhere/in/here/{action}'
  defaults:
    _controller: '\Drupal\my_module\Controller\MyController:myOtherMethod'
    _title: "My Special Dynamic Drupal Page With a Parameter"
    action: ''
  requirements:
    _permission: 'administer nodes'

Notes:

  • Replace all instances of "my_module" with the machine name of your module, and come up with a unique "my_route_id" for each of your routes.

  • Take note that path should not start with a forward slash!

  • _controller can be a method in a controller function, or for old-timers, can also be just a regular function name as defined in your .module file.

  • requirements is optional, and lets you restrict access to the page based on Drupal permissions.  Of course, you'll need the machine name of the permission, and you have to do a little digging to find those values.

Module .links.menu.yml Format

my_module.my_menulink_id:
  title: 'My New Menu Item'
  description: 'Description of this menu item'
  parent: parent_menu_id
  route_name: my_route_id

Notes:

  • Replace all instances of "my_module" with the machine name of your module, and come up with a unique "my_menulink_id" for each of your links.

  • "parent_menu_id" should be the ID of a menu object or an existing menu item on a menu object.

  • "my_route_id" should be a route defined in your module's .routing.yml file.