You are here

function link_field_process in Link 7

Processes the link type element before displaying the field.

Build the form element. When creating a form using FAPI #process, note that $element['#value'] is already set.

The $fields array is in $complete_form['#field_info'][$element['#field_name']].

1 string reference to 'link_field_process'
link_element_info in ./link.module
Implements hook_element_info().

File

./link.module, line 1079
Defines simple link field types.

Code

function link_field_process($element, $form_state, $complete_form) {
  $instance = field_widget_instance($element, $form_state);
  if (!$instance) {

    // The element comes from a custom form, we have to manually create the
    // $instance settings.
    $instance['settings'] = array(
      'title_maxlength' => isset($element['#title_maxlength']) ? $element['#title_maxlength'] : 128,
      'title' => isset($element['#title_mode']) ? $element['#title_mode'] : 'optional',
      'title_label_use_field_label' => isset($element['#title_label_use_field_label']) ? $element['#title_label_use_field_label'] : FALSE,
      'url' => isset($element['#url']) ? $element['#url'] : 'optional',
    );
    if (isset($element['#attributes'])) {
      $instance['settings']['attributes'] = $element['#attributes'];
    }
  }
  $settings = $instance['settings'];
  $element['url'] = array(
    '#type' => 'textfield',
    '#maxlength' => LINK_URL_MAX_LENGTH,
    '#title' => t('URL'),
    '#required' => $element['#delta'] == 0 && $settings['url'] !== 'optional' ? $element['#required'] : FALSE,
    '#default_value' => isset($element['#value']['url']) ? $element['#value']['url'] : NULL,
  );
  if (in_array($settings['title'], array(
    'optional',
    'required',
  ))) {

    // Figure out the label of the title field.
    if (!empty($settings['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');
    }

    // Default value.
    $title_maxlength = 128;
    if (!empty($settings['title_maxlength'])) {
      $title_maxlength = $settings['title_maxlength'];
    }
    $element['title'] = array(
      '#type' => 'textfield',
      '#maxlength' => $title_maxlength,
      '#title' => $title_label,
      '#description' => t('The link title is limited to @maxlength characters maximum.', array(
        '@maxlength' => $title_maxlength,
      )),
      '#required' => $settings['title'] == 'required' && ($element['#delta'] == 0 && $element['#required'] || !empty($element['#value']['url'])) ? TRUE : FALSE,
      '#default_value' => isset($element['#value']['title']) ? $element['#value']['title'] : NULL,
    );
  }
  elseif ($settings['title'] == 'select') {
    $options = drupal_map_assoc(array_filter(explode("\n", str_replace("\r", "\n", trim($settings['title_allowed_values'])))));
    $element['title'] = array(
      '#type' => 'select',
      '#title' => t('Title'),
      '#description' => t('Select the a title for this link.'),
      '#default_value' => isset($element['#value']['title']) ? $element['#value']['title'] : NULL,
      '#options' => $options,
      '#empty_value' => '',
    );
  }

  // Initialize field attributes as an array if it is not an array yet.
  if (!is_array($settings['attributes'])) {
    $settings['attributes'] = array();
  }

  // Add default attributes.
  $settings['attributes'] += _link_default_attributes();
  $attributes = isset($element['#value']['attributes']) ? $element['#value']['attributes'] : $settings['attributes'];
  if (!empty($settings['attributes']['target']) && $settings['attributes']['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 (!empty($settings['attributes']['configurable_title']) && $settings['attributes']['configurable_title'] == 1) {
    $element['attributes']['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Link "title" attribute'),
      '#default_value' => isset($attributes['title']) ? $attributes['title'] : '',
      '#field_prefix' => 'title = "',
      '#field_suffix' => '"',
    );
  }
  if (!empty($settings['attributes']['configurable_class']) && $settings['attributes']['configurable_class'] == 1) {
    $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;
}