media.fields.inc 24.6 KB
Newer Older
JacobSingh's avatar
JacobSingh committed
1
<?php
2 3

/**
4
 * @file
5 6
 * Provide the media selector widget and media field formatters to the Fields
 * API.
7 8
 */

JacobSingh's avatar
JacobSingh committed
9
/**
10
 * Implements hook_field_widget_info().
JacobSingh's avatar
JacobSingh committed
11
 */
12 13 14
function media_field_widget_info() {
  return array(
    'media_generic' => array(
15
      'label' => t('Media browser'),
16
      'field types' => array('file', 'image'),
17
      'settings' => array(
18 19 20
        'allowed_types' => array(
          'image' => 'image',
        ),
21
        'browser_plugins' => array(),
22 23 24
        'allowed_schemes' => array(
          'public' => 'public',
        ),
25 26
      ),
      'behaviors' => array(
27
        'multiple values' => FIELD_BEHAVIOR_CUSTOM,
28
        'default value' => FIELD_BEHAVIOR_NONE,
JacobSingh's avatar
JacobSingh committed
29 30 31
      ),
    ),
  );
32
}
JacobSingh's avatar
JacobSingh committed
33

34
/**
35
 * Implements hook_field_widget_settings_form().
36 37 38 39 40
 */
function media_field_widget_settings_form($field, $instance) {
  $widget = $instance['widget'];
  $settings = $widget['settings'];

41 42 43 44 45 46 47 48 49 50 51 52 53
  $plugins = media_get_browser_plugin_info();
  $options = array();
  foreach ($plugins as $key => $plugin) {
    $options[$key] = check_plain($plugin['title']);
  }

  $form['browser_plugins'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Enabled browser plugins'),
    '#options' => $options,
    '#default_value' => $settings['browser_plugins'],
    '#description' => t('Media browser plugins which are allowed for this field. If no plugins are selected, they will all be available.'),
  );
54

55
  $form['allowed_types'] = array(
56
    '#type' => 'checkboxes',
57
    '#title' => t('Allowed file types'),
58
    '#options' => file_entity_type_get_names(),
59
    '#default_value' => $settings['allowed_types'],
60
    '#description' => t('File types which are allowed for this field. If no file types are selected, they will all be available.'),
61 62
  );

63
  $visible_steam_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE);
64
  $options = array();
65 66
  foreach ($visible_steam_wrappers as $scheme => $information) {
    $options[$scheme] = check_plain($information['name']);
67
  }
68

69 70 71 72 73
  $form['allowed_schemes'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Allowed URI schemes'),
    '#options' => $options,
    '#default_value' => $settings['allowed_schemes'],
74
    '#description' => t('URI schemes which are allowed for this field. If no schemes are selected, they will all be available.'),
75
  );
76

77 78 79
  return $form;
}

80 81 82 83
/**
 * Implements hook_field_widget_form().
 */
function media_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  $defaults = array(
    'fid' => 0,
    'display' => !empty($field['settings']['display_default']),
    'description' => '',
  );

  // Load the items for form rebuilds from the field state as they might not be
  // in $form_state['values'] because of validation limitations. Also, they are
  // only passed in as $items when editing existing entities.
  $field_state = field_form_get_state($element['#field_parents'], $field['field_name'], $langcode, $form_state);
  if (isset($field_state['items'])) {
    $items = $field_state['items'];
  }

  $field_settings = $instance['settings'];
  $widget_settings = $instance['widget']['settings'];

  // Essentially we use the media type, extended with some enhancements.
  $element_info = element_info('media');
103 104
  $multiselect = ($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED);

105
  $element += array(
106
    '#type' => 'media',
107 108
    '#value_callback' => 'media_field_widget_value',
    '#process' => array_merge($element_info['#process'], array('media_field_widget_process')),
109 110
    '#media_options' => array(
      'global' => array(
111 112 113
        'types' => array_filter($widget_settings['allowed_types']),
        'enabledPlugins' => array_filter($instance['widget']['settings']['browser_plugins']),
        'schemes' => array_filter($widget_settings['allowed_schemes']),
114
        'file_directory' => isset($field_settings['file_directory']) ? $field_settings['file_directory'] : '',
115
        'file_extensions' => isset($field_settings['file_extensions']) ? $field_settings['file_extensions'] : variable_get('file_entity_default_allowed_extensions', 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm'),
116
        'max_filesize' => isset($field_settings['max_filesize']) ? $field_settings['max_filesize'] : 0,
117
        'uri_scheme' => !empty($field['settings']['uri_scheme']) ? $field['settings']['uri_scheme'] : file_default_scheme(),
118
        'multiselect' => $multiselect,
119
      ),
JacobSingh's avatar
JacobSingh committed
120
    ),
121 122
    // Allows this field to return an array instead of a single value.
    '#extended' => TRUE,
123 124
  );

125 126 127 128
  // If translation is enabled for the entity, store its form/source langcodes
  // on the elements for further usage in media_element_process().
  if (module_invoke('entity_translation', 'enabled', $element['#entity_type'], $element['#entity'])) {
    $translation_handler = entity_translation_get_handler($element['#entity_type'], $element['#entity']);
129
    $element['#media_parent_entity_form_langcode'] = $translation_handler->getActiveLanguage();
130 131 132 133 134 135 136
    if ($source_langcode = $translation_handler->getSourceLanguage()) {
      $element['#media_parent_entity_source_langcode'] = $source_langcode;
    }
  }
  elseif (module_exists('translation') && $element['#entity_type'] == 'node' && translation_supported_type($element['#entity']->type)) {
    $element['#media_parent_entity_form_langcode'] = $element['#entity']->language;
    $element['#media_parent_entity_source_langcode'] = $element['#entity']->language;
137
  } elseif ($element['#entity_type'] == 'field_collection_item' && !empty($form['#entity']) && property_exists($form['#entity'], 'language')) {
138 139
    $element['#media_parent_entity_form_langcode'] = $form['#entity']->language;
  }
140 141 142 143 144
  else if ($element['#entity_type'] == 'paragraphs_item' && !empty($form['#entity'])) {
    $host = $element['#entity']->hostEntity();
    $element['#media_parent_entity_form_langcode'] = $host->language;
    $element['#media_parent_entity_source_langcode'] = $host->language;
  }
145

146 147 148 149 150 151
  // Add image field specific validators.
  if ($field['type'] == 'image') {
    if ($field_settings['min_resolution'] || $field_settings['max_resolution']) {
      $element['#media_options']['global']['min_resolution'] = $field_settings['min_resolution'];
      $element['#media_options']['global']['max_resolution'] = $field_settings['max_resolution'];
    }
152 153
  }

154 155 156 157
  if ($field['cardinality'] == 1) {
    // Set the default value.
    $element['#default_value'] = !empty($items) ? $items[0] : $defaults;
    // If there's only one field, return it as delta 0.
Devin Carlson's avatar
Devin Carlson committed
158 159 160
    if (empty($element['#default_value']['fid'])) {
      $element['#description'] = theme('media_upload_help', array('description' => $element['#description']));
    }
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    $elements = array($element);
  }
  else {
    // If there are multiple values, add an element for each existing one.
    foreach ($items as $item) {
      $elements[$delta] = $element;
      $elements[$delta]['#default_value'] = $item;
      $elements[$delta]['#weight'] = $delta;
      $delta++;
    }
    // And then add one more empty row for new uploads except when this is a
    // programmed form as it is not necessary.
    if (($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta < $field['cardinality']) && empty($form_state['programmed'])) {
      $elements[$delta] = $element;
      $elements[$delta]['#default_value'] = $defaults;
      $elements[$delta]['#weight'] = $delta;
      $elements[$delta]['#required'] = ($element['#required'] && $delta == 0);
    }
    // The group of elements all-together need some extra functionality
    // after building up the full list (like draggable table rows).
    $elements['#file_upload_delta'] = $delta;
    $elements['#theme'] = 'media_widget_multiple';
    $elements['#theme_wrappers'] = array('fieldset');
    $elements['#process'] = array('media_field_widget_process_multiple');
    $elements['#title'] = $element['#title'];
    $elements['#description'] = $element['#description'];
    $elements['#field_name'] = $element['#field_name'];
    $elements['#language'] = $element['#language'];
189
    $elements['#display_field'] = intval(!empty($field['settings']['display_field']));
190 191 192 193 194

    // Add some properties that will eventually be added to the media upload
    // field. These are added here so that they may be referenced easily through
    // a hook_form_alter().
    $elements['#file_upload_title'] = t('Attach media');
Devin Carlson's avatar
Devin Carlson committed
195
    $elements['#file_upload_description'] = theme('media_upload_help', array('description' => ''));
196 197 198 199 200 201 202 203 204 205 206 207 208 209
  }

  return $elements;
}

/**
 * The #value_callback for the media field element.
 */
function media_field_widget_value($element, $input = FALSE, $form_state) {
  if ($input) {
    // Checkboxes lose their value when empty.
    // If the display field is present make sure its unchecked value is saved.
    $field = field_widget_field($element, $form_state);
    if (empty($input['display'])) {
210
      $input['display'] = !empty($field['settings']['display_field']) ? 0 : 1;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    }
  }

  // We depend on the media element to handle uploads.
  $return = media_file_value($element, $input, $form_state);

  // Ensure that all the required properties are returned even if empty.
  $return += array(
    'fid' => 0,
    'display' => 1,
    'description' => '',
  );

  return $return;
}

/**
 * An element #process callback for the media field type.
 *
 * Expands the media type to include the description and display fields.
 */
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 {
252
    $element['display'] = array(
253 254
      '#type' => 'hidden',
      '#value' => '1',
255 256 257
    );
  }

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
  // 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.'),
    );
  }

  // 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;
      }
281
    }
282 283 284 285 286 287 288 289 290 291
    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));
292 293
  }

294
  return $element;
JacobSingh's avatar
JacobSingh committed
295 296
}

297
/**
298 299 300 301 302 303
 * An element #process callback for a group of media fields.
 *
 * Adds the weight field to each row so it can be ordered and adds a new Ajax
 * wrapper around the entire group so it can be replaced all at once.
 */
function media_field_widget_process_multiple($element, &$form_state, $form) {
304 305 306 307 308 309 310 311 312 313 314 315 316
    // In order to support multiple selection, we need to reconstruct the _POST
  // data that is checked in media_attach_file(). We need to reconstruct the
  // field's _POST key name, for example: field_mediafield_und_0.
  $upload_name_prefix = implode('_', $element['#parents']) . '_';
  $upload_name = $upload_name_prefix . $element['#file_upload_delta'];
  if (!empty($_POST['media'][$upload_name])) {
    $files = explode(',', $_POST['media'][$upload_name]);
    $count = count($files);
    // Supposing #file_upload_delta is always the last delta this will work
    for ($i = 0; $i < $count; $i++) {
      // For each file selected, increment the field key to be processed.
      // field_mediafield_und_0 becomes field_mediafield_und_1, etc.
      $_POST['media'][$upload_name_prefix . ($element['#file_upload_delta'] + $i)] = $files[$i];
317 318 319 320 321 322 323

      // Copy the default file element to each newly selected file position.
      $default_element = $element[$element['#file_upload_delta']];
      $element[] = array_merge(
        $default_element,
        array('#weight' => ($element['#file_upload_delta'] + $i + 1))
      );
324 325 326
    }
  }

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
  $element_children = element_children($element, TRUE);
  $count = count($element_children);

  foreach ($element_children as $delta => $key) {
    if ($key != $element['#file_upload_delta']) {
      $description = _media_field_get_description_from_element($element[$key]);
      $element[$key]['_weight'] = array(
        '#type' => 'weight',
        '#title' => $description ? t('Weight for @title', array('@title' => $description)) : t('Weight for new file'),
        '#title_display' => 'invisible',
        '#delta' => $count,
        '#default_value' => $delta,
      );
    }
    else {
      // The title needs to be assigned to the attach field so that validation
      // errors include the correct widget label.
      $element[$key]['#title'] = $element['#title'];
      $element[$key]['_weight'] = array(
        '#type' => 'hidden',
        '#default_value' => $delta,
      );
    }
  }

  // Add a new wrapper around all the elements for Ajax replacement.
  $element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">';
  $element['#suffix'] = '</div>';

  return $element;
}

/**
 * Retrieves the file description from a media field element.
 *
 * This helper function is used by media_field_widget_process_multiple().
 *
 * @param $element
 *   The element being processed.
 *
 * @return
 *   A description of the file suitable for use in the administrative interface.
 */
function _media_field_get_description_from_element($element) {
  // Use the actual file description, if it's available.
  if (!empty($element['#default_value']['description'])) {
    return $element['#default_value']['description'];
  }
  // Otherwise, fall back to the filename.
  if (!empty($element['#default_value']['filename'])) {
    return $element['#default_value']['filename'];
  }
  // This is probably a newly uploaded file; no description is available.
  return FALSE;
}

/**
 * Form submission handler for attach/remove button of media_field_widget_form().
385
 *
386
 * This runs in addition to and after media_field_widget_submit().
387
 *
388
 * @see media_field_widget_submit()
389
 * @see media_field_widget_form()
390
 * @see media_field_widget_process()
391
 */
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
function media_field_widget_submit($form, &$form_state) {
  // During the form rebuild, media_field_widget_form() will create field item
  // widget elements using re-indexed deltas, so clear out $form_state['input']
  // to avoid a mismatch between old and new deltas. The rebuilt elements will
  // have #default_value set appropriately for the current state of the field,
  // so nothing is lost in doing this.
  $parents = array_slice($form_state['triggering_element']['#parents'], 0, -2);
  drupal_array_set_nested_value($form_state['input'], $parents, NULL);

  $button = $form_state['triggering_element'];

  // Go one level up in the form, to the widgets container.
  $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -1));
  $field_name = $element['#field_name'];
  $langcode = $element['#language'];
  $parents = $element['#field_parents'];
408

409
  $submitted_values = drupal_array_get_nested_value($form_state['values'], array_slice($button['#parents'], 0, -2));
410 411 412 413
  foreach ($submitted_values as $delta => $submitted_value) {
    if (!$submitted_value['fid']) {
      unset($submitted_values[$delta]);
    }
414
  }
415

416
  ksort($submitted_values);
417 418 419 420
  // Re-index deltas after removing empty items.
  $submitted_values = array_values($submitted_values);

  // Update form_state values.
421
  drupal_array_set_nested_value($form_state['values'], array_slice($button['#parents'], 0, -2), $submitted_values);
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441

  // Update items.
  $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
  $field_state['items'] = $submitted_values;
  field_form_set_state($parents, $field_name, $langcode, $form_state, $field_state);
}

/**
 * Returns HTML for an individual media widget.
 *
 * @param $variables
 *   An associative array containing:
 *   - element: A render element representing the widget.
 *
 * @ingroup themeable
 */
function theme_media_widget($variables) {
  $element = $variables['element'];
  $output = '';

442
  // Merge provided attributes with existing ones.
443
  // The "form-media" class is required for proper Ajax functionality.
444 445 446 447 448 449 450 451 452 453 454 455
  $attributes = array(
    'class' => array("media-widget", "form-media", "clearfix"),
  );
  if (!empty($element['#attributes'])) {
    $attributes = array_merge_recursive($attributes, $element['#attributes']);
  }
  if (!empty($element['#id'])) {
    $attributes = array_merge_recursive($attributes, array('id' => $element['#id'] . '--widget'));
  }

  // Render attributes into div in one go.
  $output .= '<div ' . drupal_attributes($attributes) . '>';
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
  $output .= drupal_render_children($element);
  $output .= '</div>';

  return $output;
}

/**
 * Returns HTML for a group of media widgets.
 *
 * @param $variables
 *   An associative array containing:
 *   - element: A render element representing the widgets.
 *
 * @ingroup themeable
 */
function theme_media_widget_multiple($variables) {
  $element = $variables['element'];

  // Special ID and classes for draggable tables.
  $weight_class = $element['#id'] . '-weight';
  $table_id = $element['#id'] . '-table';

  // Build up a table of applicable fields.
  $headers = array();
  $headers[] = t('File information');
  if ($element['#display_field']) {
    $headers[] = array(
      'data' => t('Display'),
      'class' => array('checkbox'),
    );
486
  }
487
  $headers[] = t('Weight');
488
  $headers[] = t('Operations');
489

490 491 492 493 494 495 496 497 498 499 500 501 502
  // Get our list of widgets in order (needed when the form comes back after
  // preview or failed validation).
  $widgets = array();
  foreach (element_children($element) as $key) {
    $widgets[] = &$element[$key];
  }
  usort($widgets, '_field_sort_items_value_helper');

  $rows = array();
  foreach ($widgets as $key => &$widget) {
    // Save the uploading row for last.
    if ($widget['#file'] == FALSE) {
      $widget['#title'] = $element['#file_upload_title'];
Devin Carlson's avatar
Devin Carlson committed
503
      $widget['#description'] = $element['#file_upload_description'];
504 505 506 507 508 509 510
      continue;
    }

    // Delay rendering of the buttons, so that they can be rendered later in the
    // "operations" column.
    $operations_elements = array();
    foreach (element_children($widget) as $sub_key) {
511
      if (isset($widget[$sub_key]['#type']) && ($widget[$sub_key]['#type'] == 'submit' || $widget[$sub_key]['#type'] == 'link')) {
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
        hide($widget[$sub_key]);
        $operations_elements[] = &$widget[$sub_key];
      }
    }

    // Delay rendering of the "Display" option and the weight selector, so that
    // each can be rendered later in its own column.
    if ($element['#display_field']) {
      hide($widget['display']);
    }
    hide($widget['_weight']);

    // Render everything else together in a column, without the normal wrappers.
    $widget['#theme_wrappers'] = array();
    $information = drupal_render($widget);

    // Render the previously hidden elements, using render() instead of
    // drupal_render(), to undo the earlier hide().
    $operations = '';
    foreach ($operations_elements as $operation_element) {
      $operations .= render($operation_element);
    }
    $display = '';
    if ($element['#display_field']) {
      unset($widget['display']['#title']);
      $display = array(
        'data' => render($widget['display']),
        'class' => array('checkbox'),
      );
    }
    $widget['_weight']['#attributes']['class'] = array($weight_class);
    $weight = render($widget['_weight']);

    // Arrange the row with all of the rendered columns.
    $row = array();
    $row[] = $information;
    if ($element['#display_field']) {
      $row[] = $display;
    }
    $row[] = $weight;
552
    $row[] = $operations;
553 554 555 556 557 558
    $rows[] = array(
      'data' => $row,
      'class' => isset($widget['#attributes']['class']) ? array_merge($widget['#attributes']['class'], array('draggable')) : array('draggable'),
    );
  }

559 560 561 562
  $table = array('header' => $headers, 'rows' => $rows, 'attributes' => array('id' => $table_id));

  drupal_alter('media_widget_multiple', $table, $element);

563 564 565
  drupal_add_tabledrag($table_id, 'order', 'sibling', $weight_class);

  $output = '';
566
  $output = empty($rows) ? '' : theme('table', $table);
567 568 569 570
  $output .= drupal_render_children($element);
  return $output;
}

Devin Carlson's avatar
Devin Carlson committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
/**
 * Returns HTML for help text.
 *
 * @param $variables
 *   An associative array containing:
 *   - description: The normal description for this field, specified by the
 *     user.
 *
 * @ingroup themeable
 */
function theme_media_upload_help($variables) {
  $description = $variables['description'];

  $descriptions = array();

  if (strlen($description)) {
    $descriptions[] = $description;
  }

  return implode('<br />', $descriptions);
}

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
/**
 * Implements hook_field_formatter_info().
 *
 * Provides legacy support for the "Large filetype icon" file field formatter.
 * This was originally used when media entities contained file fields. The
 * current file entity architecture no longer needs this, but people may
 * have used this formatter for other file fields on their website.
 *
 * @todo Some day, remove this.
 */
function media_field_formatter_info() {
  $formatters = array(
    'media_large_icon' => array(
      'label' => t('Large filetype icon'),
      'field types' => array('file'),
608 609 610
      'settings' => array(
        'image_style' => '',
      ),
611
    ),
612
  );
613

614 615
  return $formatters;
}
616

617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
/**
 * Implements hook_field_formatter_settings_form().
 *
 * Legacy support for the "Large filetype icon" file field formatter.
 * @see media_field_formatter_info()
 */
function media_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];

  $image_styles = image_style_options(FALSE, PASS_THROUGH);
  $element['image_style'] = array(
    '#title' => t('Image style'),
    '#type' => 'select',
    '#default_value' => $settings['image_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $image_styles,
  );

  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 *
 * Legacy support for the "Large filetype icon" file field formatter.
 * @see media_field_formatter_info()
 */
function media_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];

  $summary = array();

  $image_styles = image_style_options(FALSE, PASS_THROUGH);
  // Unset possible 'No defined styles' option.
  unset($image_styles['']);
  // Styles could be lost because of enabled/disabled modules that defines
  // their styles in code.
  if (isset($image_styles[$settings['image_style']])) {
    $summary[] = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']]));
  }
  else {
    $summary[] = t('Original image');
  }

  return implode('<br />', $summary);
}

666 667 668 669 670 671 672 673 674 675 676 677 678 679
/**
 * Implements hook_field_formatter_view().
 *
 * Legacy support for the "Large filetype icon" file field formatter.
 * @see media_field_formatter_info()
 */
function media_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  if ($display['type'] == 'media_large_icon') {
    foreach ($items as $delta => $item) {
      $element[$delta] = array(
        '#theme' => 'media_formatter_large_icon',
        '#file' => (object) $item,
680
        '#style_name' => $display['settings']['image_style'],
681 682 683 684 685
      );
    }
  }

  return $element;
686
}