Drupal contrib project online code lookup
Given a pasted code snippet from a Drupal contrib module or from Drupal core, find the equivalent URL on git.drupalcode.org.
Workflow
Step 1: Identify lines in the file
- Count or identify the line numbers ($lines) of the pasted code within its source file.
- Format:
L17-L34style (e.g.#L17-L34). - If the user has not provided the file path, ask for it OR infer it from context clues in the code (hook names, module prefixes, namespaces).
Step 2: Identify the parent module path
- Determine the module folder containing the code file.
- Typical Drupal path:
modules/contrib/<module_name>/or a subfolder within it. - Example:
modules/contrib/admin_toolbar/admin_toolbar_tools/
Step 3: Find the machine name from *.info.yml
- Look for a
*.info.ymlfile in the module directory. - The machine name ($machine_name) = the filename prefix of that
.info.ymlfile. - Example:
admin_toolbar_tools.info.yml→ $machine_name =admin_toolbar_tools
Step 4: Extract project and version
From the *.info.yml file, read:
project:→ $projectversion:→ $version
Example values:
project: admin_toolbar
version: 8.x-3.4
Step 5: Walk up if project/version not found
- If the
*.info.ymlin the current folder does not containproject:orversion:, move up one directory level. - Repeat Steps 3-4 until you find an
*.info.ymlthat contains both values. - The $project value is typically the top-level contrib module name (e.g.
admin_toolbar, not the submodule).
Step 6: Check if this code is from Drupal core
- If the
*.info.ymlhas no parent with aversionand is under folders likeweb/coreordocroot/corethen it is probably a Drupal core module or code snippet. - In this case get the project is "drupal" and the version can be found with one of the methods below (prefer this order):
- From the
const VERSIONvariable oncore/lib/Drupal.php - With jq to parse composer.lock directly:
cat composer.lock | jq '.packages[] | select(.name=="drupal/core-recommended") | .version' - With composer:
composer show drupal/core --format=json | jq -r '.versions[0]'or if need ddevddev composer show drupal/core --format=json | jq -r '.versions[0]'
- From the
Step 7: Identify the relative path
- $relative_path = path of the code file relative to the $project folder.
- Example: if file is at
modules/contrib/admin_toolbar/admin_toolbar_tools/admin_toolbar_tools.moduleand $project =admin_toolbar, then: $relative_path =admin_toolbar_tools/admin_toolbar_tools.module
Step 8: Build the URL
https://git.drupalcode.org/project/$project/-/blob/$version/$relative_path?ref_type=tags#L$lines
Example:
https://git.drupalcode.org/project/admin_toolbar/-/blob/8.x-3.4/admin_toolbar_tools/admin_toolbar_tools.module?ref_type=tags#L17-L34
Output Format
Return:
- The URL as a clickable link.
- A simple markdown table of resolved values:
| Variable | Value |
|---|---|
| $lines | L17-L34 |
| $machine_name | admin_toolbar_tools |
| $project | admin_toolbar |
| $version | 8.x-3.4 |
| $relative_path | admin_toolbar_tools/admin_toolbar_tools.module |
Notes
- If
project:is missing from the.info.yml, the module was likely installed from source (git clone) and may not have a tagged release. Inform the user. - For Drupal 10+ modules, versions may look like
2.1.0instead of8.x-3.4- both are valid tag formats. - If the user does not know the file path, ask for: the module name and the function/class name, then infer the likely file.