class Barcode in Barcodes 8
Same name in this branch
- 8 src/Template/Barcode.php \Drupal\barcodes\Template\Barcode
- 8 src/Plugin/Block/Barcode.php \Drupal\barcodes\Plugin\Block\Barcode
- 8 src/Plugin/Field/FieldFormatter/Barcode.php \Drupal\barcodes\Plugin\Field\FieldFormatter\Barcode
Same name and namespace in other branches
- 2.0.x src/Plugin/Block/Barcode.php \Drupal\barcodes\Plugin\Block\Barcode
Provides a 'Barcode' block.
Plugin annotation
@Block(
id = "barcode",
admin_label = @Translation("Barcode"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Component\Plugin\ContextAwarePluginBase implements ContextAwarePluginInterface
- class \Drupal\Core\Plugin\ContextAwarePluginBase implements CacheableDependencyInterface, ContextAwarePluginInterface uses DependencySerializationTrait, StringTranslationTrait, TypedDataTrait
- class \Drupal\Core\Block\BlockBase implements BlockPluginInterface, PluginWithFormsInterface, PreviewFallbackInterface uses BlockPluginTrait, ContextAwarePluginAssignmentTrait
- class \Drupal\barcodes\Plugin\Block\Barcode implements ContainerFactoryPluginInterface
- class \Drupal\Core\Block\BlockBase implements BlockPluginInterface, PluginWithFormsInterface, PreviewFallbackInterface uses BlockPluginTrait, ContextAwarePluginAssignmentTrait
- class \Drupal\Core\Plugin\ContextAwarePluginBase implements CacheableDependencyInterface, ContextAwarePluginInterface uses DependencySerializationTrait, StringTranslationTrait, TypedDataTrait
- class \Drupal\Component\Plugin\ContextAwarePluginBase implements ContextAwarePluginInterface
Expanded class hierarchy of Barcode
File
- src/
Plugin/ Block/ Barcode.php, line 21
Namespace
Drupal\barcodes\Plugin\BlockView source
class Barcode extends BlockBase implements ContainerFactoryPluginInterface {
/**
* A logger instance.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Construct.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param string $plugin_definition
* The plugin implementation definition.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('logger.channel.barcodes'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'type' => 'QRCODE',
'format' => 'SVG',
'value' => '',
'color' => '#000000',
'height' => 100,
'width' => 100,
'padding_top' => 0,
'padding_right' => 0,
'padding_bottom' => 0,
'padding_left' => 0,
'show_value' => FALSE,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$generator = new BarcodeGenerator();
$form['value'] = [
'#type' => 'textfield',
'#title' => $this
->t('Value'),
'#description' => $this
->t('The Barcode value.'),
'#default_value' => $this->configuration['value'],
];
if (\Drupal::moduleHandler()
->moduleExists('token')) {
$form['value'] += [
'#element_validate' => [
'token_element_validate',
],
'#token_types' => [
'node',
],
];
$form['token_help'] = [
'#theme' => 'token_tree_link',
'#token_types' => [
'node',
],
];
}
$form['type'] = [
'#type' => 'select',
'#title' => $this
->t('Barcode Type'),
'#description' => $this
->t('The Barcode type.'),
'#options' => array_combine($generator
->getTypes(), $generator
->getTypes()),
'#default_value' => $this->configuration['type'],
];
$form['format'] = [
'#type' => 'select',
'#title' => $this
->t('Display Format'),
'#description' => $this
->t('The display format, e.g. png, svg, jpg.'),
'#options' => [
'PNG' => 'PNG Image',
'SVG' => 'SVG Image',
'HTMLDIV' => 'HTML DIV',
'UNICODE' => 'Unicode String',
'BINARY' => 'Binary String',
],
'#default_value' => $this->configuration['format'],
];
$form['color'] = [
'#type' => 'color',
'#title' => $this
->t('Color'),
'#default_value' => $this->configuration['color'],
'#description' => $this
->t('The color code.'),
];
$form['height'] = [
'#type' => 'textfield',
'#title' => $this
->t('Height'),
'#size' => 10,
'#default_value' => $this->configuration['height'],
'#description' => $this
->t('The height in pixels.'),
];
$form['width'] = [
'#type' => 'textfield',
'#title' => $this
->t('Width'),
'#size' => 10,
'#default_value' => $this->configuration['width'],
'#description' => $this
->t('The width in pixels'),
];
$form['padding_top'] = [
'#type' => 'textfield',
'#title' => $this
->t('Padding-Top'),
'#size' => 4,
'#maxlength' => 4,
'#default_value' => $this->configuration['padding_top'],
'#description' => $this
->t('The top padding in pixels'),
];
$form['padding_right'] = [
'#type' => 'textfield',
'#title' => $this
->t('Padding-Right'),
'#size' => 4,
'#maxlength' => 4,
'#default_value' => $this->configuration['padding_right'],
'#description' => $this
->t('The right padding in pixels'),
];
$form['padding_bottom'] = [
'#type' => 'textfield',
'#title' => $this
->t('Padding-Bottom'),
'#size' => 4,
'#maxlength' => 4,
'#default_value' => $this->configuration['padding_bottom'],
'#description' => $this
->t('The bottom padding in pixels'),
];
$form['padding_left'] = [
'#type' => 'textfield',
'#title' => $this
->t('Padding-Left'),
'#size' => 4,
'#maxlength' => 4,
'#default_value' => $this->configuration['padding_left'],
'#description' => $this
->t('The left padding in pixels'),
];
$form['show_value'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show value'),
'#default_value' => $this->configuration['show_value'],
'#description' => $this
->t('Show the actual value in addition to the barcode'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$this
->setConfiguration($values);
}
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$token_service = \Drupal::token();
$generator = new BarcodeGenerator();
$suffix = str_replace('+', 'plus', strtolower($this->configuration['type']));
$tokens = [];
$parameters = \Drupal::routeMatch()
->getParameters();
foreach ($parameters as $parameter) {
if ($parameter instanceof EntityInterface) {
$tokens[$parameter
->getEntityTypeId()] = $parameter;
}
}
$value = $token_service
->replace($this->configuration['value'], $tokens);
$build['barcode'] = [
'#theme' => 'barcode__' . $suffix,
'#attached' => [
'library' => [
'barcodes/' . $suffix,
],
],
'#type' => $this->configuration['type'],
'#value' => $value,
'#width' => $this->configuration['width'],
'#height' => $this->configuration['height'],
'#color' => $this->configuration['color'],
'#padding_top' => $this->configuration['padding_top'],
'#padding_right' => $this->configuration['padding_right'],
'#padding_bottom' => $this->configuration['padding_bottom'],
'#padding_left' => $this->configuration['padding_left'],
'#show_value' => $this->configuration['show_value'],
];
try {
$barcode = $generator
->getBarcodeObj($this->configuration['type'], $value, $this->configuration['width'], $this->configuration['height'], $this->configuration['color'], [
$this->configuration['padding_top'],
$this->configuration['padding_right'],
$this->configuration['padding_bottom'],
$this->configuration['padding_left'],
]);
$build['barcode']['#format'] = $this->configuration['format'];
$build['barcode']['#svg'] = $barcode
->getSvgCode();
$build['barcode']['#png'] = "<img alt=\"Embedded Image\" src=\"data:image/png;base64," . base64_encode($barcode
->getPngData()) . "\" />";
$build['barcode']['#htmldiv'] = $barcode
->getHtmlDiv();
$build['barcode']['#unicode'] = "<pre style=\"font-family:monospace;line-height:0.61em;font-size:6px;\">" . $barcode
->getGrid(json_decode('"\\u00A0"'), json_decode('"\\u2584"')) . "</pre>";
$build['barcode']['#binary'] = "<pre style=\"font-family:monospace;\">" . $barcode
->getGrid() . "</pre>";
$build['barcode']['#barcode'] = $build['barcode']['#' . strtolower($this->configuration['format'])];
$build['barcode']['#extended_value'] = $barcode
->getExtendedCode();
} catch (\Exception $e) {
$this->logger
->error('Error: @error, given: @value', [
'@error' => $e
->getMessage(),
'@value' => $this->configuration['value'],
]);
}
return $build;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Barcode:: |
protected | property | A logger instance. | |
Barcode:: |
public | function |
Overrides BlockPluginTrait:: |
|
Barcode:: |
public | function |
Overrides BlockPluginTrait:: |
|
Barcode:: |
public | function |
Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface:: |
|
Barcode:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
Barcode:: |
public | function |
Overrides BlockPluginTrait:: |
|
Barcode:: |
public | function |
Construct. Overrides BlockPluginTrait:: |
|
BlockPluginInterface:: |
constant | Indicates the block label (title) should be displayed to end users. | ||
BlockPluginTrait:: |
protected | property | The transliteration service. | |
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
protected | function | Returns generic default configuration for block plugins. | |
BlockPluginTrait:: |
protected | function | Indicates whether the block should be shown. | 16 |
BlockPluginTrait:: |
public | function | 3 | |
BlockPluginTrait:: |
public | function | Creates a generic configuration form for all block types. Individual block plugins can add elements to this form by overriding BlockBase::blockForm(). Most block plugins should not override this method unless they need to alter the generic form elements. | 2 |
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | 1 | |
BlockPluginTrait:: |
public | function | 1 | |
BlockPluginTrait:: |
public | function | 3 | |
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | Sets the transliteration service. | |
BlockPluginTrait:: |
public | function | Most block plugins should not override this method. To add submission handling for a specific block type, override BlockBase::blockSubmit(). | |
BlockPluginTrait:: |
protected | function | Wraps the transliteration service. | |
BlockPluginTrait:: |
public | function | Most block plugins should not override this method. To add validation for a specific block type, override BlockBase::blockValidate(). | 1 |
ContextAwarePluginAssignmentTrait:: |
protected | function | Builds a form element for assigning a context to a given slot. | |
ContextAwarePluginAssignmentTrait:: |
protected | function | Wraps the context handler. | |
ContextAwarePluginBase:: |
protected | property | The data objects representing the context of this plugin. | |
ContextAwarePluginBase:: |
private | property | Data objects representing the contexts passed in the plugin configuration. | |
ContextAwarePluginBase:: |
protected | function |
Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
The cache contexts associated with this object. Overrides CacheableDependencyInterface:: |
9 |
ContextAwarePluginBase:: |
public | function |
The maximum age for which this object may be cached. Overrides CacheableDependencyInterface:: |
7 |
ContextAwarePluginBase:: |
public | function |
The cache tags associated with this object. Overrides CacheableDependencyInterface:: |
4 |
ContextAwarePluginBase:: |
public | function |
This code is identical to the Component in order to pick up a different
Context class. Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Gets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Gets the defined contexts. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Gets the value for a defined context. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Gets the values for all defined contexts. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Set a context on this plugin. Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Sets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Sets the value for a defined context. Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Validates the set values for the defined contexts. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function | Implements magic __get() method. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginWithFormsTrait:: |
public | function | ||
PluginWithFormsTrait:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TypedDataTrait:: |
protected | property | The typed data manager used for creating the data types. | |
TypedDataTrait:: |
public | function | Gets the typed data manager. | 2 |
TypedDataTrait:: |
public | function | Sets the typed data manager. | 2 |