Refactor the share buttons template

Created on 8 January 2025, 7 months ago
Updated 15 July 2025, 14 days ago

The issue

The template used for rendering of the share buttons goes against DRY principle, which make it harder to maintain and actually harder to override/customize it. Hard-coded URLs are also a bit of an issue.

Consider following cases...

  • In a hypothetical case where the number of supported social outlets grow you'd have to add more and more repeating code into the template.
  • When a custom button has to be added - you'd have to override and maintain the whole template in your theme
  • In a case where a share URL needs to be changed, and the template is overridden in your theme - besides the module update this will require updating the overridden template as well

Proposed resolution

- create general button template,
- refactor main template to include button template as needed (per what is available in exposed variables)
- move URL formatting into preprocess or specialty callback

✨ Feature request
Status

Active

Version

4.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States euk

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

Merge Requests

Comments & Activities

  • Issue created by @euk
  • πŸ‡ΊπŸ‡ΈUnited States euk
  • First commit to issue fork.
  • Merge request !36moves templates to separate template files β†’ (Merged) created by shelane
  • πŸ‡ΊπŸ‡ΈUnited States shelane

    I have a refactoring of the templates, but it's not by what you suggest. I'm not sure what you are calling for exactly. So, if there is something you would like to see, please submit an MR.

  • Pipeline finished with Skipped
    14 days ago
    #548232
  • πŸ‡ΊπŸ‡ΈUnited States shelane
  • πŸ‡ΊπŸ‡ΈUnited States euk

    Hi there! Thanks for looking into this. What you have in the MR36 is not exactly what I was calling for, but very close. You simple split the main template file into individual button files, but still have the same repeating code across them (probably all of them, I didn't check them all).

    You could simplify things down even more by having a single template file, and then just bring out all variable pieces out into some preprocess callback. Then here, at https://git.drupalcode.org/project/better_social_sharing_buttons/-/merge... you would just need to pass all the variable peaces into the include statement.

    As far as using twig include - you could also use a button theme hook, which would be called automatically when you print a list of buttons.
    This would make things really extensible.

    But let me try and see what I can get you in another MR.

  • πŸ‡ΊπŸ‡ΈUnited States shelane

    I understand your point, but they don't all have the same pattern. The structure of email and print, for instance, is different. Also, some attach libraries in the template.

  • πŸ‡ΊπŸ‡ΈUnited States euk

    Those are just two exceptions. Template suggestions can address differences, and libraries can be attached on as needed basis in preprocess callback. I have pretty much working solution, just need to buff a few things.

Production build 0.71.5 2024