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.

Comments

jgalletta created an issue. See original summary.

jgalletta’s picture

I 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.

/**
 * Settings for my_module.
 */
function my_module_settings_form() {
  $form['my_module_custom_link'] = array(
    // Required so all the field values are put in a single variable in $form_state.
    '#tree' => TRUE, 

    '#type' => 'link_field',
    '#title' => $value,
    '#default_value' => variable_get('my_module_custom_link'),

    // New properties.
    '#title_maxlength' => 30,
    '#attributes' => array(
       'target' => LINK_TARGET_USER,
    ), 
    
    // Required properties, if omitted, php notices are thrown.
    '#delta' => 0,
    '#language' => 'und',
    '#field_parents' => array(),
    '#field_name' => 'link_field',
  );

  return system_settings_form($form);
}

New properties are:

  • #title_maxlength : integer, set the title field maxlength, default to 128
  • #title_label_use_field_label : bool, use field label instead of "Title" for title field label, default to false
  • #title_mode : string, possible values: 'optional', 'required', default to 'optional'
  • #url : string, possible values: 'optional', 'required', default to 'optional'
  • #attributes : array, set widget attributes.

Available attributes are:

  • target : display a checkbox to set the final link href target attribute or directly set it
  • configurable_title : display a textfield to set the final link title attribute
  • configurable_class : display a textfield to set a custom class on final link

In the form_submit functions, all the field values will be stored in $form_state['my_custom_link'] as an array.

malcomio’s picture

Status: Active » Needs review

Hi 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

Status: Needs review » Needs work

The last submitted patch, 2: link-widget_in_custom_form-2718563-0-7.x.patch, failed testing.

makbul_khan8’s picture

Assigned: Unassigned » makbul_khan8
makbul_khan8’s picture

StatusFileSize
new1.07 KB

I 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

makbul_khan8’s picture

Assigned: makbul_khan8 » Unassigned
Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 6: link-widget_in_custom_form-2718563-5.patch, failed testing.

The last submitted patch, 6: link-widget_in_custom_form-2718563-5.patch, failed testing.

makbul_khan8’s picture

Uploaded new patch.

makbul_khan8’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 10: link-widget_in_custom_form-2718563-10.patch, failed testing.

joekers’s picture

Thanks for the patch, it's working well for me.

idebr’s picture

Status: Needs work » Closed (duplicate)

Hi everyone,

This was fixed in #2566443: Broken description text when used through Form API. I'll close this issue as a duplicate.

b.lammers’s picture

Unfortunately 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

idebr’s picture

Status: Closed (duplicate) » Needs review

#15 You are correct, I misread the issue summary. My apologies!

pifagor’s picture

  • pifagor committed fffb5d9 on 7.x-1.x authored by makbul_khan8
    Issue #2718563 by makbul_khan8, jgalletta, idebr, malcomio, joekers, b....
pifagor’s picture

Status: Needs review » Fixed
alex_optim’s picture

Good work.

Status: Fixed » Closed (fixed)

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

kenorb’s picture

Patch 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

ciss’s picture

@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).