After searching for a while on the net, I couldn't find any good example of how to create a field of type link_field in a custom form.
I had a look at the module's code, and I found to my surprise that using a link field widget in a custom form is not really implemented.
The most you can do is display a widget with title and url fields, but even that is not really working, a message "The link title is limited to characters maximum." is displayed, without any possibility to set the maxlength.
Also it's impossible to control the widget options, like displaying only the url field, or having the "open in a new window" checkbox.
So basically, to my current knowledge, the link module is not compatible with the form API.
This is a little bit disappointing, as I think a lot of people would be interested to have link fields in their custom forms without having to implement everything from scratch.
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | link-widget_in_custom_form-2718563-10.patch | 1.07 KB | makbul_khan8 |
| #6 | link-widget_in_custom_form-2718563-5.patch | 1.07 KB | makbul_khan8 |
| #2 | link-widget_in_custom_form-2718563-0-7.x.patch | 1.07 KB | jgalletta |
Comments
Comment #2
jgalletta commentedI created a patch to allow to use all the link field widget options in a custom form.
This patch is not perfect, but I think it's a good start. Note: I didn't test it with a multi valued widget instance.
Example of implementation in an admin settings form.
New properties are:
Available attributes are:
In the form_submit functions, all the field values will be stored in $form_state['my_custom_link'] as an array.
Comment #3
malcomio commentedHi Julien - long time no speak.
The patch looks good to me - we're currently testing it on a project you used to work on :)
The only review comments I've got are pretty trivial:
* there's trailing whitespace in line 841
* better to use FALSE than 0
Comment #5
makbul_khan8 commentedComment #6
makbul_khan8 commentedI have uploaded new patch with below mentioned points resolved.
* there's trailing whitespace in line 841
* better to use FALSE than 0
Can someone please review and test?
Thanks
Comment #7
makbul_khan8 commentedComment #10
makbul_khan8 commentedUploaded new patch.
Comment #11
makbul_khan8 commentedComment #13
joekersThanks for the patch, it's working well for me.
Comment #14
idebr commentedHi everyone,
This was fixed in #2566443: Broken description text when used through Form API. I'll close this issue as a duplicate.
Comment #15
b.lammers commentedUnfortunately this does not solve the entire issue discussed here. Configuring the field when used in the Form API is still not possible.
Only thing changed in the related issue you mentioned is the display of the label
Comment #16
idebr commented#15 You are correct, I misread the issue summary. My apologies!
Comment #17
pifagorComment #19
pifagorComment #20
alex_optimGood work.
Comment #22
kenorb commentedPatch from #10 is still not committed and the problem exists with the latest v1.6.
I've tested patch from #10 and it works as expected. Without patch, I can't see Title field when created field programmatically.
See: #3053288: Use URL and Title subfields in Form API
Comment #23
ciss commented@kenorb You can find the commit in branch 7.x-1.x. The release 1.6 (as well as 1.5) is part of a separate branch 7.x-1.x-release that only contains security fixes. The last common parent is 77147ee (2018-02-15).