You are here

public function Barcode::build in Barcodes 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Block/Barcode.php \Drupal\barcodes\Plugin\Block\Barcode::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/Barcode.php, line 244

Class

Barcode
Provides a 'Barcode' block.

Namespace

Drupal\barcodes\Plugin\Block

Code

public function build() {
  $build = [];
  $generator = new BarcodeGenerator();
  $suffix = str_replace('+', 'plus', strtolower($this->configuration['type']));
  $tokens = [];
  $parameters = $this->routeMatch
    ->getParameters();
  foreach ($parameters as $parameter) {
    if ($parameter instanceof EntityInterface) {
      $tokens[$parameter
        ->getEntityTypeId()] = $parameter;
    }
  }
  $value = $this->token
    ->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;
}