Download & Extend

Using hook_form_alter() to set a link field's #default_value

Project:Link
Version:7.x-1.0
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

The link field seems to work fine without a default value:

<input id="edit-field-fieldgroup-und-0-field-web-site-link-und-0-url" class="fluid form-text" type="text" maxlength="2048" size="" value="" name="field_fieldgroup[und][0][field_web_site_link][und][0][url]">

But when I use a hook_form_alter() to set a default value, the form field breaks:
<?php
$form
['field_fieldgroup']['und'][0]['field_web_site_link']['und'][0]['#default_value'] = 'http://www.example.com';
?>

The size is blank and the value only has the first character:
<input id="edit-field-fieldgroup-und-0-field-web-site-link-und-0-url" class="fluid form-text" type="text" maxlength="2048" size="" value="h" name="field_fieldgroup[und][0][field_web_site_link][und][0][url]">

Comments

#1

This field has sub-components, so you need to set the 'url' index for it:

<?php
$form
['field_fieldgroup']['und'][0]['field_web_site_link']['und'][0]['#default_value']['url'] = 'http://www.example.com';
?>

#2

I am also having a similar problem with adding a default value.

http://drupal.org/node/1782516

<input id="edit-field-company-website-und-0-url" class="form-text" type="text" maxlength="2048" size="60" value="" name="field_company_website[und][0][url]">

I have tried all the following code but to no avail.

<?php
$form
['field_company_website']['und'][0]['url']['#default_value']['url'] = 'http://www.example.com' ;
$form['field_company_website']['und'][0]['#default_value']['url'] = 'http://www.example.com' ;
$form['field_company_website']['und'][0]['url']['#default_value'] = 'http://www.example.com' ;
$form['field_company_website']['und'][0]['#default_value'] = 'http://www.example.com' ;
$form['field_company_website']['und'][0]['value']['url']['#default_value']['url'] = 'http://www.example.com' ;
$form['field_company_website']['und'][0]['url']['value']['#default_value']['url'] = 'http://www.example.com' ;
$form['field_company_website']['und'][0]['value']['url']['#default_value'] = 'http://www.example.com' ;
$form['field_company_website']['und'][0]['url']['value']['#default_value'] = 'http://www.example.com' ;
$form['field_company_website']['und'][0]['value']['#default_value']['url'] = 'http://www.example.com' ;
$form['field_company_website']['und'][0]['value']['#default_value'] = 'http://www.example.com' ;
?>

If you found a solution it would be greatly appreciated