Change the extension from svg to png using |barcode

Created on 25 January 2024, 10 months ago
Updated 10 September 2024, 2 months ago

When I use |barcode {{ custom value|barcode }} it generates an SVG image and I need it with a PNG extension.

There is a way to change the extension from svg to png in twig? I am using entity print β†’ module to create a pdf where it has a QR with the link to the node.

Temporarily what I did was modify
\src\template\bardoce
line 107

-  return $barcode->getSvgCode();
+ return "<img alt=\"Embedded Image\" src=\"data:image/png;base64," . base64_encode($barcode->getPngData()) . "\" />";
✨ Feature request
Status

Fixed

Version

2.1

Component

Documentation

Created by

πŸ‡¦πŸ‡·Argentina claudioasaez

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @claudioasaez
  • Status changed to Fixed 10 months ago
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    The Barcodes module provides a Block and a Field Formatter. Both have settings options for the image format. SVG is default, but you can choose PNG if that's what you want.

  • πŸ‡¦πŸ‡·Argentina claudioasaez

    Yes, I know, but I am using display suit and generating the QR based on a link that I created for the presentation of the node. For this reason I use |barcode.
    I don't want to add a field just to get something I already have, which is the link to the entity's pdf view mode

  • Status changed to Active 10 months ago
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    Sorry, I totally missed that because I forgot this module provides a TwigFilter!

    The filter does allow you to pass in some arguments currently, but it's not documented and the image format is not one of the options.

    I propose that we enhance the filter so that in your template you can pass values for ANY of the barcode settings. And in any order, because to do otherwise would make it a lot harder to use. Using it would look something like this:

    {{ custom value|barcode({'type' : 'QRCODE', 'format' : 'PNG'}) }}

    Where if you didn't provide any arguments, it would still do the same thing it currently does (which uses the default arguments), but if you wanted to change 1 or more of the settings you would pass the setting and the value as array elements to the barcode filter. And because the barcode filter accepts tokens, you could even pass a token value in for any of the settings.

    Does that sound like a good plan?

    I think the README and the project page needs to be changed to mention that this module provides a Block and a TwigFilter, and we need to create a documentation guide for this project to start documenting how to use it and to give some examples.

    Honestly, I'm not sure how much time I have to work on this right now, so if you have the ability to do some of this that would be great - I would be happy to review and commit any progress toward this.

  • First commit to issue fork.
  • Merge request !6Resolve #3416962 "Change the extension" β†’ (Open) created by sanduhrs
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.5 + Environment: PHP 7.3 & MySQL 5.7
    last update 9 months ago
    7 pass
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.5 + Environment: PHP 7.3 & MySQL 5.7
    last update 9 months ago
    7 pass
  • Status changed to Needs review 9 months ago
  • πŸ‡©πŸ‡ͺGermany sanduhrs πŸ‡ͺπŸ‡Ί Heidelberg, Germany, Europe
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    Rebased MR against 2.1.x.

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    TR β†’ changed the visibility of the branch 3416962-change-the-extension to hidden.

  • Merge request !16Resolve #3416962 β†’ (Merged) created by tr
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    MR16 is now rebased against the latest 2.1.x branch.

    I made a few changes for coding standards.

    I removed the getName() method, since that is not a method that is defined on AbstractExpression and is not a method that is used by Twig. The name of the filter is actually only defined by the first argument of the new TwigFilter() call.

    I added parameter typing and return type hints to all methods.

    I added a default case to the filter, to handle cases when the 'format' specified is unknown. I have the filter returning an empty string in the default case. I don't know if it would be better to throw an exception? Regardless, the method needs to return a string and having a default case documents what is intended to happen with the format is unknown.

  • Pipeline finished with Skipped
    3 months ago
    #266389
    • TR β†’ committed c7c5f107 on 2.1.x
      Issue #3416962 by TR, sanduhrs, claudioasaez: Change the extension from...
  • Status changed to Fixed 3 months ago
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    I also wrote up a documentation page on how to use the Twig filter, including these new changes.

    https://www.drupal.org/docs/extending-drupal/contributed-modules/contrib... β†’

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024