1 link.module | link_field_process($element, $form_state, $complete_form) |
Processes the link type element before displaying the field.
Build the form element. When creating a form using Form API #process, note that $element['#value'] is already set.
The $fields array is in $complete_form['#field_info'][$element['#field_name']].
File
- core/
modules/ link/ link.module, line 873 - Defines simple link field types.
Code
function link_field_process($element, $form_state, $complete_form) {
$element['#tree'] = TRUE;
$element['#link_field_options'] += _link_field_options_defaults();
$element['url'] = array(
'#type' => 'textfield',
'#maxlength' => LINK_URL_MAX_LENGTH,
'#title' => t('URL'),
'#required' => (empty($element['#delta']) && $element['#link_field_options']['url_mode'] !== 'optional') ? $element['#required'] : FALSE,
'#default_value' => isset($element['#value']['url']) ? $element['#value']['url'] : NULL,
);
if ($element['#link_field_options']['link_type'] != LINK_EXTERNAL) {
$element['url']['#autocomplete_path'] = 'path-autocomplete';
}
if ($element['#link_field_options']['title_mode'] !== 'none' && $element['#link_field_options']['title_mode'] !== 'value') {
// Figure out the label of the title field.
if (!empty($element['#link_field_options']['title_label_use_field_label'])) {
// Use the element label as the title field label.
$title_label = $element['#title'];
// Hide the field label because there is no need for the duplicate labels.
$element['#title_display'] = 'invisible';
}
else {
$title_label = t('Title');
}
$element['title'] = array(
'#type' => 'textfield',
'#maxlength' => $element['#link_field_options']['title_maxlength'],
'#title' => $title_label,
'#required' => ($element['#link_field_options']['title_mode'] == 'required' && (($element['#delta'] == 0 && $element['#required']) || !empty($element['#value']['url']))) ? TRUE : FALSE,
'#default_value' => isset($element['#value']['title']) ? $element['#value']['title'] : NULL,
);
}
// Add sub-elements under the 'attributes' key.
$attributes = isset($element['#value']['attributes']) ? $element['#value']['attributes'] : array();
if ($element['#link_field_options']['target'] == LINK_TARGET_USER) {
$element['attributes']['target'] = array(
'#type' => 'checkbox',
'#title' => t('Open URL in a New Window'),
'#return_value' => LINK_TARGET_NEW_WINDOW,
'#default_value' => isset($attributes['target']) ? $attributes['target'] : FALSE,
);
}
if ($element['#link_field_options']['configurable_title']) {
$element['attributes']['title'] = array(
'#type' => 'textfield',
'#title' => t('Link "title" attribute'),
'#default_value' => isset($attributes['title']) ? $attributes['title'] : '',
'#field_prefix' => 'title = "',
'#field_suffix' => '"',
);
}
if ($element['#link_field_options']['configurable_class']) {
$element['attributes']['class'] = array(
'#type' => 'textfield',
'#title' => t('Custom link class'),
'#default_value' => isset($attributes['class']) ? $attributes['class'] : '',
'#field_prefix' => 'class = "',
'#field_suffix' => '"',
);
}
// If the title field is available or there are field accepts multiple values
// then allow the individual field items display the required asterisk if needed.
if (isset($element['title']) || isset($element['_weight'])) {
// To prevent an extra required indicator, disable the required flag on the
// base element since all the sub-fields are already required if desired.
$element['#required'] = FALSE;
}
return $element;
}