Barcodes
Generates and displays various types of 1D and 2D barcodes from field values, blocks, or Twig templates using the TC-LIB-BARCODE PHP library.
barcodes
Install
composer require 'drupal/barcodes:^2.1'
composer require 'drupal/barcodes:^2.0'
Overview
The Barcodes module provides comprehensive barcode generation capabilities for Drupal. It integrates the powerful tecnickcom/tc-lib-barcode PHP library to generate over 35 different types of 1D and 2D barcodes including QR codes, Data Matrix, PDF417, EAN, UPC, Code 39, Code 128, and many more.
The module offers multiple ways to display barcodes: as a field formatter for various field types (email, integer, link, string, telephone, text, etc.), as a configurable block that can be placed in any region, or directly in Twig templates using a filter. Token module integration enables dynamic barcode values based on current page context or entity data.
Output formats include SVG (vector graphics), PNG (raster images), HTML DIV elements, Unicode text representation, and binary string representation. All barcode settings such as type, color, dimensions, and padding are fully configurable.
Features
- Field formatter supporting 10 field types: email, integer, link, string, telephone, text, text_long, text_with_summary, bigint, and uuid
- Block plugin for displaying barcodes in any theme region with full configuration options
- Twig filter for generating barcodes directly in templates with customizable parameters
- Support for 36+ barcode types including 1D codes (Code 39, Code 128, EAN, UPC, etc.) and 2D codes (QR Code, Data Matrix, PDF417, Aztec)
- Five output formats: SVG (vector), PNG (raster image), HTML DIV, Unicode text, and Binary string
- Token module integration for dynamic barcode values based on entities or current page context
- Drush commands for command-line barcode generation and format listing
- Fully customizable appearance: color, width, height, and padding on all sides
- Option to display the raw value alongside the barcode image
- Dedicated CSS library for each barcode type allowing type-specific styling
Use Cases
Product Labels with EAN/UPC Barcodes
Add a string or integer field to product content types and configure the Barcode formatter with EAN-13 or UPC-A type. Products will display scannable retail barcodes that work with standard barcode readers.
QR Codes Linking to Content
Add a link field to content types and configure it with the Barcode formatter using QRCODE type. Visitors can scan the QR code with their mobile devices to quickly access the linked resource.
Event Tickets with Unique Identifiers
Use the UUID field type formatted as a barcode (Code 128 or QR Code) to create unique, scannable tickets for events. The barcode can be verified at entry points.
Dynamic QR Codes in Page Templates
Use the Twig filter in templates to generate QR codes linking to the current page: {{ url('<current>') | barcode(type='QRCODE', width=150, height=150) }}. Useful for print stylesheets or sharing pages.
Contact Information QR Codes
Place a barcode block on user profile pages with a token-based value containing vCard data. Visitors can scan to add contact information to their phones.
Inventory Management Barcodes
Generate Code 128 or Code 39 barcodes from SKU or inventory ID fields for warehouse management and stock tracking systems.
Batch Barcode Generation via Drush
Use the drush barcodes:generate command in scripts to batch-generate barcode images for products, assets, or documents outside of the Drupal interface.
Tips
- Choose SVG format for barcodes that need to scale without losing quality, especially for print applications
- Use the PNG format with --binary flag in Drush to generate barcode image files directly from the command line
- For 1D retail barcodes (EAN, UPC), ensure your input values have the correct number of digits including check digits
- QR codes can encode URLs, text, vCard data, WiFi credentials, and more - format the value string appropriately
- The Token module integration allows encoding dynamic values like [node:url] or [current-user:mail] in block barcodes
- Each barcode type has its own CSS library, allowing you to override styles for specific barcode types in your theme
- Use the 'show_value' option to display human-readable text beneath barcodes for accessibility or manual entry fallback
Technical Details
Hooks 1
hook_theme
Defines theme hooks for the base barcode template and 36+ barcode-type-specific template variants (e.g., barcode__qrcode, barcode__ean13)
Drush Commands 2
drush barcodes:generate
Generates a barcode with the specified value and options. Outputs barcode markup or binary data.
drush barcodes:formats
Lists all available barcode types/formats supported by the module with their descriptions.
Troubleshooting 4
Check the Drupal logs (admin/reports/dblog) for error messages from the barcodes logger channel. Common issues include invalid characters for the selected barcode type or incorrect value length for types like EAN-13 that require specific digit counts.
The Barcode formatter only works with supported field types: email, integer, link, string, telephone, text, text_long, text_with_summary, bigint, and uuid. Ensure your field is one of these types.
Ensure the Token module is installed and enabled. Token replacement in blocks works with entity tokens when viewing entity pages (nodes, users, etc.).
Increase the width and height settings for better resolution. Some barcode types have minimum size requirements. Also ensure adequate contrast between the barcode color and background.