class Barcode in Barcodes 2.0.x
Same name in this branch
- 2.0.x src/Template/Barcode.php \Drupal\barcodes\Template\Barcode
- 2.0.x src/Plugin/Block/Barcode.php \Drupal\barcodes\Plugin\Block\Barcode
- 2.0.x src/Plugin/Field/FieldFormatter/Barcode.php \Drupal\barcodes\Plugin\Field\FieldFormatter\Barcode
Same name and namespace in other branches
- 8 src/Template/Barcode.php \Drupal\barcodes\Template\Barcode
Provides a "barcode" Twig filter for formatting text as a barcode.
@package Drupal\barcodes\Template
Hierarchy
- class \Drupal\barcodes\Template\Barcode extends \Twig\Extension\AbstractExtension
Expanded class hierarchy of Barcode
1 string reference to 'Barcode'
1 service uses Barcode
File
- src/
Template/ Barcode.php, line 15
Namespace
Drupal\barcodes\TemplateView source
class Barcode extends AbstractExtension {
/**
* The token service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* Constructs a Barcode Twig extension.
*
* @param \Drupal\Core\Utility\Token $token
* The token service.
*/
public function __construct(Token $token) {
$this->token = $token;
}
/**
* {@inheritdoc}
*/
public function getName() {
return 'barcode';
}
/**
* {@inheritdoc}
*/
public function getFilters() {
return [
new TwigFilter('barcode', [
$this,
'filterBarcode',
], [
'is_safe' => [
'html',
],
]),
];
}
/**
* Barcode filter.
*
* @param string $value
* The string that should be formatted as a barcode.
* @param string $type
* The barcode type.
* @param string $color
* The barcode color.
* @param int $height
* The barcode height.
* @param int $width
* The barcode width.
* @param int $padding_top
* The barcode top padding.
* @param int $padding_right
* The barcode right padding.
* @param int $padding_bottom
* The barcode bottom padding.
* @param int $padding_left
* The barcode left padding.
*
* @return string
* The barcode markup to display.
*
* @throws \Com\Tecnick\Barcode\Exception
*/
public function filterBarcode($value, $type = 'QRCODE', $color = '#000000', $height = 100, $width = 100, $padding_top = 0, $padding_right = 0, $padding_bottom = 0, $padding_left = 0) {
$value = (string) $value;
$generator = new BarcodeGenerator();
$value = $this->token
->replace($value);
$barcode = $generator
->getBarcodeObj($type, $value, $width, $height, $color, [
$padding_top,
$padding_right,
$padding_bottom,
$padding_left,
]);
return $barcode
->getSvgCode();
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Barcode:: |
protected | property | The token service. | |
Barcode:: |
public | function | Barcode filter. | |
Barcode:: |
public | function | ||
Barcode:: |
public | function | ||
Barcode:: |
public | function | Constructs a Barcode Twig extension. |