You are here

function barcodes_field_formatter_view in Barcodes 7

Implements hook_field_formatter_view().

File

./barcodes.module, line 338
Contains barcodes.module.

Code

function barcodes_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $elements = array();
  $settings = $display['settings'];
  switch ($display['type']) {
    case 'barcode':
      $generator = new BarcodeGenerator();
      foreach ($items as $delta => $item) {
        $suffix = str_replace('+', 'plus', strtolower($settings['type']));
        $value = $item['value'];
        if (module_exists('token')) {
          $value = token_replace($value);
        }
        $elements[$delta] = array(
          '#theme' => 'barcode__' . $suffix,
          '#attached' => array(
            'library' => array(
              'barcodes/' . $suffix,
            ),
          ),
          '#type' => $settings['type'],
          '#value' => check_plain($value),
          '#width' => $settings['width'],
          '#height' => $settings['height'],
          '#color' => $settings['color'],
          '#padding_top' => $settings['padding_top'],
          '#padding_right' => $settings['padding_right'],
          '#padding_bottom' => $settings['padding_bottom'],
          '#padding_left' => $settings['padding_left'],
          '#show_value' => $settings['show_value'],
        );
        try {
          $barcode = $generator
            ->getBarcodeObj($settings['type'], $value, $settings['width'], $settings['height'], $settings['color'], array(
            $settings['padding_top'],
            $settings['padding_right'],
            $settings['padding_bottom'],
            $settings['padding_left'],
          ));
          $elements[$delta]['#svg'] = $barcode
            ->getSvgCode();
        } catch (\Exception $e) {
          watchdog('barcodes', 'Error: @error, given: @value', array(
            '@error' => $e
              ->getMessage(),
            '@value' => $value,
          ));
        }
      }
      break;
  }
  return $elements;
}