Would it be possible to add an alter hook for component defaults? The use case is where a developer wants to effectively add additional functionality to an existing component type. Currently I've found that that's not tricky to do by implementing hook_form_webform_component_edit_form_alter() and adding additional fields, with '#parents' => array('extra', 'property_name'). I can then use this additional stored data within preprocess functions, form_alters, etc.
Obviously, defaults can be implemented with a simple isset() conditional. The problem is that some associated modules use the defined list of defaults for additional functionality. Specifically, Webform Localisation runs webform_component_invoke($component['type'], 'defaults') in order to generate a list of synchronisable properties. As is there's no way I can make my added properties synchronisable without hacking Webform Localisation.
I've attached a patch which adds an alter hook. I appreciate that this doesn't actually resolve the Webform Localisation issue, but it would set up a standardised interface hook that modules like Webform Localisation could invoke, and add extra benefits, such as being able to override defaults to those most appropriate to a specific site, which would help admins in instances where they would otherwise have to change the same value in every new component added.
I have raised a corresponding issue on Webform Localisation's issue queue (Allow list of synchronisable properties to be altered)
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | webform_component_defaults_alter-2013523.patch | 1.73 KB | quicksketch |
| #3 | webform--defaults-alter-patch.diff | 1.93 KB | pq |
| webform--defaults-alter-patch.diff | 754 bytes | pq |
Comments
Comment #0.0
pq commentedadde cross link to Weform Localisation issue
Comment #1
quicksketchThis sounds like a good idea to me. Patch looks pretty good too. It looks like webform_localization supports the 3.x branch of the module, which I'm trying to phase-out, but for simple API additions like this it may be valid.
The only thing really missing in this patch is matching documentation in webform.api.php.
Comment #2
quicksketchMarked duplicate: #1836004: How to alter settings defined in _webform_defaults_<componentname>.
Comment #3
pq commentedSorry for the delay in getting back, here's an attempt with documentation. I've not done any contrib docs patches before so I hope it'l be ok but apologies if it's not up to snuff.
Comment #4
quicksketchThanks this looks great! We only just renamed "mandatory" to "required" in #2020723: Rename "mandatory" to "required" for consistency with HTML5 and the rest of Drupal, so for the 4.x branch we'll need to update the example.
Comment #5
pq commentedHi @quicksketch,
The guys on the Webform localisation queue have just queried what the status of this is, as I think they'd like to implement the hook. Sorry, I haven't had an opportunity to look into doing a 4.x conversion, have been changing over jobs with lots of associated overtime and haven't had time for much else, and unfortunately that's likely to continue for a few more weeks.
Are there likely to be any 3.x / 4.x minor point releases coming up. If there are for 4.x then I can try to jump on it in time.
Comment #6
quicksketchI updated this patch for the 4.x branch and modified it to namespace the hook properly. Because this modifies component defaults, this should be "hook_webform_component_defaults_alter" instead of "hook_webform_defaults_alter". There's already defaults set per-node, so leaving out the word "component" could introduce confusion.
The documentation also was missing the word "_alter" in its example code. Other than that looks good. Committed to all branches.
Comment #7.0
(not verified) commentedcorrected typo