I'm trying to fix a link field that was added programmatically to a form using the Form API. At some point in time the Title subfield disappeared and now only one text field appears, which apparently is the URL subfield (though it's not labeled as such).

This is the code currently, which works for adding a URL but does not work for adding a Title, because no field is rendered in which to add a title.

  $form['link'] = array(
    '#type' => 'link_field',
    '#title' => t('Link'),
    '#description' => t('Callout link'),
    '#default_value' => array(
      'url' => isset($conf['link']['url']) ? $conf['link']['url'] : NULL,
      'title' => isset($conf['link']['title']) ? $conf['link']['title'] : NULL,
    ),
    '#attributes' => array(
      'configurable_title' => TRUE, // I don't know if I'm using this correctly.
    ),
    '#tree' => TRUE,
    '#settings' => array( // Is this where settings should go?
      'title' => 'required',
      'url' => 'required',
    )
  );

This issue is very related to https://www.drupal.org/project/link/issues/2718563, but it appears that not everything that was proposed in that issue made it into the release. I've browsed the code in link.module, but I just can't piece together how this is supposed to work.

Comments

maskedjellybean created an issue. See original summary.

idebr’s picture

When running the latest release (currently 7.x-1.6) you still have to apply the patch from #2718563: Use link field widget in custom form, since the last two releases have been security releases that did not include the full 7.x-1.x changelog.

Then use the following code:

$form['custom_link'] = array(
      '#tree' => TRUE,
      '#type' => 'link_field',
      '#title' => t('Custom link field'),
      '#title_mode' => 'required',
      '#url' => 'required',
      '#delta' => 0,
      '#language' => $lang_key,
      '#field_parents' => array(),
      '#field_name' => 'link_field',
      '#attributes' => array(
        'target' => LINK_TARGET_USER,
      ),
    );
kenorb’s picture

Category: Support request » Bug report

I can confirm the problem. I've created Link field programmatically via Form API using 1.6 version and I don't see the Title field, only one field. This was working fine in 1.4, and it was broken after the security updates to 1.6.

After applying the patch from #2718563-10: Use link field widget in custom form, the problem is solved. So I don't know why the other issue claims to be fixed when it's not (and I cannot re-open it).

Here is my field:

    $settings['link'] = array(
      // #tree: Required so all the field values are put in a single variable in $form_state.
      '#tree' => TRUE,
      '#title' => t('Link or button'),
      '#description' => t("Link to other content."),
      '#type' => 'link_field',
      '#title_mode' => 'required',
      '#url' => 'required',
      '#default_value' => $defaults['link'],
      '#delta' => 10,
      '#field_parents' => [],
      '#field_name' => 'link_field',
      '#language' => LANGUAGE_NONE,
    );