You are here

function url_field_widget_form in URL field 8

Same name and namespace in other branches
  1. 7 url.module \url_field_widget_form()

Implements hook_field_widget_form().

File

./url.module, line 144
Provides a URL field type that stores external links with optional titles.

Code

function url_field_widget_form($form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $settings = $instance['settings'];

  // Display this element in a fieldset if there is only one value.
  if ($field['cardinality'] == 1) {
    $element['#type'] = 'fieldset';
  }

  // URL field value.
  $element['value'] = array(
    '#type' => 'url',
    '#title' => t('URL'),
    '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '',
    '#size' => $instance['widget']['settings']['size'],
    '#maxlength' => 2048,
    '#required' => !empty($element['#required']),
    '#prefix' => '<div class="field-value field-value-url">',
    '#suffix' => '</div>',
  );

  // Title field value.
  if (!empty($settings['title_field'])) {
    $element['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#default_value' => isset($items[$delta]['title']) ? $items[$delta]['title'] : '',
      '#maxlength' => 1024,
      '#weight' => 10,
      '#prefix' => '<div class="field-value field-value-title">',
      '#suffix' => '</div>',
    );

    // Add additional styling to make both fields work together visually.
    $element['#attached']['css'][] = drupal_get_path('module', 'url') . '/url.field.css';
  }

  // Exposing the attributes array in the widget is left for alternate and more
  // advanced field widgets.
  $element['attributes'] = array(
    '#type' => 'value',
    '#value' => !empty($items[$delta]['attributes']) ? $items[$delta]['attributes'] : array(),
  );
  return $element;
}