Allow settings to be altered based on context

Created on 1 April 2023, about 2 years ago

Hi everyone,

While working on a project where we had the need to override some of the JS settings passed to the Colorbox, we found the very practical hook provided by the module hook_colorbox_settings_alter, see:
https://git.drupalcode.org/project/colorbox/-/blob/2.0.x/colorbox.api.ph...

However, as the hook currently stands, it appears it doesn't provide any information on the context for which the settings are overridden, thus preventing us from having multiple, different configurations for the various components using the Colorbox formatter on the website.

For example, it would be great if we could determine the field/field name, the item or entity being displayed for which the settings are being attached and/or overridden.

Additionally, looking at the code in 'ColorboxAttachment.php' calling the hook at:
https://git.drupalcode.org/project/colorbox/-/blob/2.0.x/src/ColorboxAtt...
it would seem the context is available in the method attach, passed as the $page argument/variable.

Would there be any reason not to pass the variable to the hook?
Could we be adding the $page as a contextual parameter so hooks can alter the settings based on the values of the element being displayed?

Lastly it would seem something similar came up in a previous ticket for the 7.x version of the module, see #1529412-1: add ability for other modules to alter colorbox settings that aren't strings (like callback settings) , with the mention of the following hook:
function MYMODULE_colorbox_settings_alter($data, $context)
Not sure if this ever existed in the module, but it seems it used to pass the $context variable to the hook.
 

Any advice, feedback or suggestions would be greatly appreciated.
Thanks in advance.

Feature request
Status

Needs review

Version

2.0

Component

Code

Created by

🇫🇷France dydave

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

Comments & Activities

  • Issue created by @dydave
  • Status changed to Needs review about 2 years ago
  • 🇫🇷France dydave

    Quick follow-up on this issue:

    Please find attached to this comment a patch implementing the changes suggested above for the 2.0.x branch:
    File attached as: 3351704-2-colorbox-settings-alter-with-context.patch
     

    • Added the $page as a parameter in the hook invocation.
    • Updated the API file with corresponding changes in hook's signature and provided basic example on how the $context could be used.

     
    We would greatly appreciate if you could please try testing the patch, in particular, whether you would see any problem that may arise with the added parameter.
    Could you please give us your feedback and let us know if the patch works as expected?

    Feel free to let us know if you have any questions, comments or concerns on the suggested patch, or this ticket in general, we would be happy to provide more information or make additional changes if needed.
    Thanks in advance.

Production build 0.71.5 2024