You are here

function media_field_widget_process in D7 Media 7.3

Same name and namespace in other branches
  1. 7.4 includes/media.fields.inc \media_field_widget_process()
  2. 7.2 includes/media.fields.inc \media_field_widget_process()

An element #process callback for the media field type.

Expands the media type to include the description and display fields.

1 string reference to 'media_field_widget_process'
media_field_widget_form in includes/media.fields.inc
Implements hook_field_widget_form().

File

includes/media.fields.inc, line 237
Provide media selector widget and media field formatters to the fields API.

Code

function media_field_widget_process($element, &$form_state, $form) {
  $item = $element['#value'];
  $item['fid'] = $element['fid']['#value'];
  $field = field_widget_field($element, $form_state);
  $instance = field_widget_instance($element, $form_state);
  $settings = $instance['widget']['settings'];
  $element['#theme'] = 'media_widget';

  // Add the display field if enabled.
  if (!empty($field['settings']['display_field']) && $item['fid']) {
    $element['display'] = array(
      '#type' => empty($item['fid']) ? 'hidden' : 'checkbox',
      '#title' => t('Include file in display'),
      '#value' => isset($item['display']) ? $item['display'] : $field['settings']['display_default'],
      '#attributes' => array(
        'class' => array(
          'file-display',
        ),
      ),
    );
  }
  else {
    $element['display'] = array(
      '#type' => 'hidden',
      '#value' => '1',
    );
  }

  // Add the description field if enabled.
  if (!empty($instance['settings']['description_field']) && $item['fid']) {
    $element['description'] = array(
      '#type' => variable_get('file_description_type', 'textfield'),
      '#title' => t('Description'),
      '#value' => isset($item['description']) ? $item['description'] : '',
      '#maxlength' => variable_get('file_description_length', 128),
      '#description' => t('The description may be used as the label of the link to the file.'),
    );
  }

  // Add the alt field if enabled.
  if (!empty($instance['settings']['alt_field']) && $item['fid']) {
    $element['alt'] = array(
      '#title' => t('Alternate text'),
      '#type' => 'textfield',
      '#default_value' => isset($item['alt']) ? $item['alt'] : '',
      '#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.<br />
<em>(Notice: this field is not fetched on -all- formatters yet. For example: If the Rendered file formatter will be used, this field is not available at the moment.)</em>'),
      '#maxlength' => variable_get('image_alt_length', 100),
      '#weight' => 1,
    );
  }

  // Add the title field if enabled.
  if (!empty($instance['settings']['title_field']) && $item['fid']) {
    $element['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#default_value' => isset($item['title']) ? $item['title'] : '',
      '#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.<br />
<em>(Notice: this field is not fetched on -all- formatters yet. For example: If the Rendered file formatter will be used, this field is not available at the moment.)</em>'),
      '#maxlength' => variable_get('image_title_length', 500),
      '#weight' => 2,
    );
  }

  // Adjust the Ajax settings so that on upload and remove of any individual
  // file, the entire group of file fields is updated together.
  if ($field['cardinality'] != 1) {
    $parents = array_slice($element['#array_parents'], 0, -1);
    $new_path = 'media/ajax/' . implode('/', $parents) . '/' . $form['form_build_id']['#value'];
    $field_element = drupal_array_get_nested_value($form, $parents);
    $new_wrapper = $field_element['#id'] . '-ajax-wrapper';
    foreach (element_children($element) as $key) {
      if (isset($element[$key]['#ajax'])) {
        $element[$key]['#ajax']['path'] = $new_path;
        $element[$key]['#ajax']['wrapper'] = $new_wrapper;
      }
    }
    unset($element['#prefix'], $element['#suffix']);
  }

  // Add another submit handler to the upload and remove buttons, to implement
  // functionality needed by the field widget. This submit handler, along with
  // the rebuild logic in media_field_widget_form() requires the entire field,
  // not just the individual item, to be valid.
  foreach (array(
    'attach_button',
    'remove_button',
  ) as $key) {
    $element[$key]['#submit'][] = 'media_field_widget_submit';
    $element[$key]['#limit_validation_errors'] = array(
      array_slice($element['#parents'], 0, -1),
    );
  }
  return $element;
}