diff --git a/link.module b/link.module index e6a34d5b56bcae1e66fe24cc836266da2070a1a3..4e792235fb24eff7a6c4cddc26fd4a665a4a8c2e 100644 --- a/link.module +++ b/link.module @@ -39,7 +39,7 @@ function link_field_settings($op, $field) { 'user' => t('Allow the user to choose'), ); $form['attributes'] = array ( - '#tree' => true, + '#tree' => true, ); $form['attributes']['target'] = array( '#type' => 'radios', @@ -82,13 +82,24 @@ function link_field($op, &$node, $field, &$node_field, $teaser, $page) { case 'view': $output = ''; if ($field['multiple']) { + // Convert an old 'single value' field to the first of multiple + if ($node_field['value']) { + $node_field[0]['value'] = $node_field['value']; + unset($node_field['value']); + } foreach ($node_field as $delta => $item) { - $node_field[$delta]['view'] = link_field_view($field, $item['value'], $item, $node); + $node_field[$delta]['view'] = _link_field_view($field, $item['value'], $item, $node); } } else { - $node_field['view'] = link_field_view($field, $node_field['value'], $node_field, $node); + // Convert an old 'multiple value' field to single + if ($node_field[0]['value']) { + $node_field['value'] = $node_field[0]['value']; + unset($node_field[0]['value']); + } + $node_field['view'] = _link_field_view($field, $node_field['value'], $node_field, $node); } + $node->$field['field_name'] = $node_field; if ($field['multiple']) { $output = ''; @@ -164,128 +175,67 @@ function link_widget($op, &$node, $field, &$node_field) { $form[$field['field_name']] = array('#tree' => TRUE); if ($field['multiple']) { - // Generate more fields if necessary on preview - if ($_POST['edit'][$field['field_name']]) { - $node_field = $_POST['edit'][$field['field_name']]; - } - // Convert an old 'single' value field into the first value of a new multi-value field - if ($node_field['value']) { - $node_field[0]['value'] = $node_field['value']; - unset($node_field['value']); - } + // Generate more fields if necessary on preview + if ($_POST['edit'][$field['field_name']]) { + $node_field = $_POST['edit'][$field['field_name']]; + } + // Convert an old 'single' value field into the first value of a new multi-value field + if ($node_field['value']) { + $node_field[0]['value'] = $node_field['value']; + unset($node_field['value']); + } $delta = 0; + // Render link fields for all the entered values foreach ($node_field as $data) { - if ($data['value']['link']) { - $form[$field['field_name']][$delta]['value'] = array( - '#type' => 'fieldset', - '#title' => t($field['widget']['label']), - '#tree' => true, - ); - $form[$field['field_name']][$delta]['value']['link'] = array( - '#type' => 'textfield', - '#title' => t('URL'), - '#default_value' => $data['value']['link'], - '#required' => ($delta == 0) ? $field['required'] : FALSE, - ); - $form[$field['field_name']][$delta]['value']['title'] = array( - '#type' => 'textfield', - '#title' => t('Title'), - '#default_value' => $data['value']['title'], - ); - if ($field['attributes']['target'] == 'user') { - $form[$field['field_name']][$delta]['value']['attributes']['target'] = array( - '#type' => 'checkbox', - '#title' => t('Open New Window'), - '#default_value' => $data['value']['attributes']['target'], - '#return_value' => "_blank", - ); - } + if ($data['value']['link']) { + _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) { - $form[$field['field_name']][$delta]['value'] = array( - '#type' => 'fieldset', - '#title' => t($field['widget']['label']), - '#tree' => true, - ); - $form[$field['field_name']][$delta]['value']['link'] = array( - '#type' => 'textfield', - '#title' => t('URL'), - '#default_value' => $node_field[$delta]['value']['link'], - '#required' => ($delta == 0) ? $field['required'] : FALSE, - ); - $form[$field['field_name']][$delta]['value']['title'] = array( - '#type' => 'textfield', - '#title' => t('Title'), - '#default_value' => $node_field[$delta]['value']['title'], - ); - if ($field['attributes']['target'] == 'user') { - $form[$field['field_name']][$delta]['value']['attributes']['target'] = array( - '#type' => 'checkbox', - '#title' => t('Open New Window'), - '#return_value' => "_blank", - ); - } + _link_widget_form($form[$field['field_name']][$delta], $field, $node_field, $delta); } } // end if multiple else { - $form[$field['field_name']]['value'] = array( - '#type' => 'fieldset', - '#title' => t($field['widget']['label']), - '#tree' => true, - ); - $form[$field['field_name']]['value']['link'] = array( - '#type' => 'textfield', - '#title' => t('URL'), - '#default_value' => $node_field['value']['link'], - '#required' => ($delta == 0) ? $field['required'] : FALSE, - ); - $form[$field['field_name']]['value']['title'] = array( - '#type' => 'textfield', - '#title' => t('Title'), - '#default_value' => $node_field['value']['title'], - ); - if ($field['attributes']['target'] == 'user') { - $form[$field['field_name']]['value']['attributes']['target'] = array( - '#type' => 'checkbox', - '#title' => t('Open New Window'), - '#default_value' => $node_field['value']['attributes']['target'], - '#return_value' => "_blank", - ); - } + // Convert an old 'multiple' value field into the value of a new single-value field + if ($node_field[0]['value']) { + $node_field['value'] = $node_field[0]['value']; + unset($node_field[0]['value']); + } + _link_widget_form($form[$field['field_name']], $field, $node_field); } return $form; case 'validate': if ($field['multiple']) { foreach($node_field as $delta => $value) { - if ($node_field[$delta]['value']['link']) { - if (!link_validate_link($value['value']['link'])) { + if ($node_field[$delta]['value']['link']) { + if (!link_validate_link($value['value']['link'])) { form_set_error($field['field_name'] .']['. $delta. '][value][link', t('Not a valid URL.')); } } } } else { - if ($node_field['value']['link']) { + if ($node_field['value']['link']) { if (!link_validate_link($node_field['value']['link'])) { form_set_error($field['field_name'] .'][value][link', t('Not a valid URL.')); } - } + } } return; case 'process form values': if ($field['multiple']) { foreach($node_field as $delta => $value) { - if (!$node_field[$delta]['value']['attributes']['target'] || $node_field[$delta]['value']['attributes']['target'] == "default") { + if (!$node_field[$delta]['value']['attributes']['target'] || $node_field[$delta]['value']['attributes']['target'] == "default") { unset($node_field[$delta]['value']['attributes']['target']); } } } else { - if (!$node_field['value']['attributes']['target'] || $node_field[$delta]['value']['attributes']['target'] == "default") { + if (!$node_field['value']['attributes']['target'] || $node_field[$delta]['value']['attributes']['target'] == "default") { unset($node_field['value']['attributes']['target']); } } @@ -296,33 +246,63 @@ function link_widget($op, &$node, $field, &$node_field) { } } +/** + * Helper function renders the link widget in both single and multiple value cases. + */ + +function _link_widget_form (&$form_item, $field, $node_field, $delta = 0) { + $form_item['value'] = array( + '#tree' => true, + ); + $form_item['value']['link'] = array( + '#type' => 'textfield', + '#title' => t($field['widget']['label'])." ".t('URL'), + '#default_value' => $node_field['value']['link'], + '#required' => ($delta == 0) ? $field['required'] : FALSE, + ); + $form_item['value']['title'] = array( + '#type' => 'textfield', + '#title' => t($field['widget']['label'])." ".t('Title'), + '#default_value' => $node_field['value']['title'], + ); + if ($field['attributes']['target'] == 'user') { + $form_item['value']['attributes']['target'] = array( + '#type' => 'checkbox', + '#title' => t('Open URL in a New Window'), + '#default_value' => $node_field['value']['attributes']['target'], + '#return_value' => "_blank", + ); + } +} + /** * Implementation of hook_field_view() which performs any translation necessary. */ -function link_field_view($field, $value, $addlfields = array(), $node = NULL) { +function _link_field_view($field, $value, $addlfields = array(), $node = NULL) { + $attributes = array(); // Add attributes defined at the widget level if (is_array($value['attributes'])) { foreach($value['attributes'] as $attribute => $attbvalue) { - if (isset($field['attributes'][$attribute]) && $field['attributes'][$attribute] == 'user') { - $attributes .= ' '.$attribute.'="'.$attbvalue.'"'; - } + if (isset($field['attributes'][$attribute]) && $field['attributes'][$attribute] == 'user') { + $attributes[$attribute] = $attbvalue; + } } } // Add attributes defined at the field level if (is_array($field['attributes'])) { foreach($field['attributes'] as $attribute => $attbvalue) { if (!empty($attbvalue) && $attbvalue != 'default' && $attbvalue != 'user') { - $attributes .= ' '.$attribute.'="'.$attbvalue.'"'; + $attributes[$attribute] = $attbvalue; } } } // Build the link with a title if (strlen(trim($value['title']))) { - $output = ''.check_plain($value['title']).''; + $output = l($value['title'],$value['link'],$attributes); } // Build the link with the URL as the title else { - $output = ''.$value['link'].''; + $output = l($value['link'],$value['link'],$attributes); } return $output; } @@ -394,27 +374,27 @@ function link_views_tables() { } function link_validate_link($text) { - if (!preg_match( - // The protocols: http:// + if (!preg_match( + // The protocols: http:// '/^((https|http|ftp|news):\/\/)?'. - '('. - // domains + '('. + // domains '(([a-z0-9]([a-z0-9\-_]*\.)+)(aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|[a-z]{2}))'. // OR ip addresses '|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'. - ')'. + ')'. // port number '(:([0-9]{1,4}))?'. - // the rest of the path - '(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.\/%=&]*)?)?'. + // the rest of the path + '(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.\/%=&]*)?)?'. // forward slash 0 or 1 times '(\/)?'. // end of the expression, case insensitive '$/i', $text, $m)) { - return false; + return false; } else { - $url = new stdClass(); + $url = new stdClass(); $url->protocol = $m[2]; $url->hostname = strtolower($m[5]).strtolower($m[7]); $url->ip = $m[8];