function link_widget in Link 5
Same name and namespace in other branches
- 6.2 link.module \link_widget()
- 6 link.module \link_widget()
Implementation of hook_widget().
File
- ./
link.module, line 258 - Defines simple link field types.
Code
function link_widget($op, &$node, $field, &$items) {
switch ($op) {
case 'prepare form values':
foreach ($items as $delta => $value) {
if (is_numeric($delta)) {
_link_widget_prepare($items[$delta], $delta);
}
}
if ($_POST[$field['field_name']]) {
$items = $_POST[$field['field_name']];
unset($items['count'], $items['more-url'], $items['more']);
}
return;
case 'form':
$form = array();
$form[$field['field_name']] = array(
'#tree' => TRUE,
'#theme' => 'link_widget_form',
'#type' => $field['multiple'] ? 'fieldset' : 'markup',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => t($field['widget']['label']),
'#description' => $field['widget']['description'],
);
// Add token module replacements if available.
if (module_exists('token') && $field['enable_tokens']) {
$tokens_form = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Placeholder tokens'),
'#description' => t("The following placeholder tokens can be used in both titles and URLs. When used in a URL or title, they will be replaced with the appropriate values."),
'#weight' => 2,
);
$tokens_form['help'] = array(
'#value' => theme('token_help', 'node'),
);
}
if ($field['multiple']) {
drupal_add_js(drupal_get_path('module', 'link') . '/link.js');
$delta = 0;
// Render link fields for all the entered values.
foreach ($items as $data) {
if (is_array($data) && ($data['url'] || $data['title'])) {
_link_widget_form($form[$field['field_name']][$delta], $field, $data, $delta);
$delta++;
}
}
// Render two additional new link fields.
foreach (range($delta, $delta + 1) as $delta) {
_link_widget_form($form[$field['field_name']][$delta], $field, array(), $delta);
}
// Create a wrapper for additional fields.
$form[$field['field_name']]['wrapper'] = array(
'#type' => 'markup',
'#value' => '<div id="' . str_replace('_', '-', $field['field_name']) . '-wrapper" class="clear-block"></div>',
);
// Add 'More' Javascript Callback.
$form[$field['field_name']]['more-url'] = array(
'#type' => 'hidden',
'#value' => url('link/widget/js/' . $field['type_name'] . '/' . $field['field_name'], NULL, NULL, TRUE),
'#attributes' => array(
'class' => 'more-links',
),
'#id' => str_replace('_', '-', $field['field_name']) . '-more-url',
);
// Add Current Field Count.
$form[$field['field_name']]['count'] = array(
'#type' => 'hidden',
'#value' => $delta,
'#id' => str_replace('_', '-', $field['field_name']) . '-count',
);
// Add More Button.
$form[$field['field_name']]['more'] = array(
'#type' => 'button',
'#value' => t('More Links'),
'#name' => 'more',
'#id' => str_replace('_', '-', $field['field_name']) . '-more',
'#weight' => 1,
);
if (isset($tokens_form)) {
$form[$field['field_name']]['tokens'] = $tokens_form;
}
}
else {
_link_widget_form($form[$field['field_name']][0], $field, $items[0]);
if (isset($tokens_form)) {
$form[$field['field_name']][0]['tokens'] = $tokens_form;
}
}
return $form;
case 'validate':
if (!is_object($node)) {
return;
}
unset($items['count']);
unset($items['more-url']);
unset($items['more']);
$optional_field_found = FALSE;
foreach ($items as $delta => $value) {
_link_widget_validate($items[$delta], $delta, $field, $node, $optional_field_found);
}
if ($field['url'] == 'optional' && $field['title'] == 'optional' && $field['required'] && !$optional_field_found) {
form_set_error($field['field_name'] . '][0][title', t('At least one title or URL must be entered.'));
}
return;
case 'process form values':
// Remove the JS helper fields.
unset($items['more-url']);
unset($items['count']);
unset($items['more']);
foreach ($items as $delta => $value) {
if (is_numeric($delta)) {
_link_widget_process($items[$delta], $delta, $field, $node);
}
}
return;
case 'submit':
// Don't save empty fields (beyond the first one).
$save_field = array();
unset($items['more-url']);
unset($items['count']);
unset($items['more']);
foreach ($items as $delta => $value) {
if ($value['url'] !== 'optional' || $delta == 0) {
$save_items[] = $items[$delta];
}
}
$items = $save_items;
return;
}
}