function link_field_widget_form

Implements hook_field_widget_form().

File

drupal/core/modules/field/modules/link/link.module, line 139
Defines simple link field types.

Code

function link_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $settings = $instance['settings'];
  $widget_settings = $instance['widget']['settings'];
  $element['url'] = array(
    '#type' => 'url',
    '#title' => t('URL'),
    '#placeholder' => isset($widget_settings['placeholder_url']) ? $widget_settings['placeholder_url'] : '',
    '#default_value' => isset($items[$delta]['url']) ? $items[$delta]['url'] : NULL,
    '#maxlength' => 2048,
    '#required' => $element['#required'],
  );
  $element['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#placeholder' => isset($widget_settings['placeholder_title']) ? $widget_settings['placeholder_title'] : '',
    '#default_value' => isset($items[$delta]['title']) ? $items[$delta]['title'] : NULL,
    '#maxlength' => 255,
    '#access' => $settings['title'] != DRUPAL_DISABLED,
  );

  // Post-process the title field to make it conditionally required if URL is
  // non-empty. Omit the validation on the field edit form, since the field
  // settings cannot be saved otherwise.
  $is_field_edit_form = $element['#entity'] === NULL;
  if (!$is_field_edit_form && $settings['title'] == DRUPAL_REQUIRED) {
    $element['#element_validate'][] = 'link_field_widget_validate_title';
  }

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