- <?php
- * @file
- * Field module functionality for the File module.
- */
-
- * Implements hook_field_info().
- */
- function file_field_info() {
- return array(
- 'file' => array(
- 'label' => t('File'),
- 'description' => t('This field stores the ID of a file as an integer value.'),
- 'settings' => array(
- 'display_field' => 0,
- 'display_default' => 0,
- 'uri_scheme' => config_get('system.core', 'file_default_scheme'),
- ),
- 'instance_settings' => array(
- 'file_extensions' => 'txt',
- 'file_directory' => '',
- 'max_filesize' => '',
- 'description_field' => 0,
- ),
- 'default_widget' => 'file_generic',
- 'default_formatter' => 'file_default',
- 'default_token_formatter' => 'file_url_plain',
- ),
- );
- }
-
- * Implements hook_field_settings_form().
- */
- function file_field_settings_form($field, $instance, $has_data) {
- $defaults = field_info_field_settings($field['type']);
- $settings = array_merge($defaults, $field['settings']);
-
- $form['#attached']['js'][] = backdrop_get_path('module', 'file') . '/js/file.js';
-
- $form['display_field'] = array(
- '#type' => 'checkbox',
- '#title' => t('Enable <em>Display</em> field'),
- '#default_value' => $settings['display_field'],
- '#description' => t('This option allows users to choose if the uploaded files should be shown or hidden when viewing the content.'),
- );
- $form['display_default'] = array(
- '#type' => 'checkbox',
- '#title' => t('Files displayed by default'),
- '#default_value' => $settings['display_default'],
- '#description' => t('This option sets the default behavior for uploaded files for this field. Users are still allowed to change this setting individually for each file before saving their content.'),
- '#states' => array(
- 'visible' => array(
- 'input[name="field[settings][display_field]"]' => array('checked' => TRUE),
- ),
- ),
- );
-
- $scheme_options = array();
- foreach (file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE) as $scheme => $stream_wrapper) {
- $scheme_options[$scheme] = $stream_wrapper['name'];
- }
- $form['uri_scheme'] = array(
- '#type' => 'radios',
- '#title' => t('Upload destination'),
- '#options' => $scheme_options,
- '#default_value' => $settings['uri_scheme'],
- '#description' => t('Select where the final files should be stored. Private file storage has significantly more overhead than public files, but allows restricted access to files within this field.'),
- '#disabled' => $has_data,
- );
-
- return $form;
- }
-
- * Implements hook_field_instance_settings_form().
- */
- function file_field_instance_settings_form($field, $instance) {
- $settings = $instance['settings'];
-
- $form['file_directory'] = array(
- '#type' => 'textfield',
- '#title' => t('File directory'),
- '#default_value' => $settings['file_directory'],
- '#field_prefix' => $field['settings']['uri_scheme'] == 'public' ? config_get('system.core', 'file_public_path') . '/' : '',
- '#description' => t('Optional subdirectory within the upload destination where files will be stored. Do not include preceding or trailing slashes.'),
- '#element_validate' => array('_file_generic_settings_file_directory_validate'),
- '#weight' => 3,
- );
-
-
- $extensions = str_replace(' ', ', ', $settings['file_extensions']);
- $form['file_extensions'] = array(
- '#type' => 'textfield',
- '#title' => t('Allowed file extensions'),
- '#default_value' => $extensions,
- '#description' => t('Separate extensions with a space or comma and do not include the leading dot.'),
- '#element_validate' => array('_file_generic_settings_extensions'),
- '#weight' => 1,
- '#maxlength' => 512,
-
-
- '#required' => TRUE,
- );
-
- $form['max_filesize'] = array(
- '#type' => 'textfield',
- '#title' => t('Maximum upload size'),
- '#default_value' => $settings['max_filesize'],
- '#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current limit <strong>%limit</strong>).', array('%limit' => format_size(file_upload_max_size()))),
- '#size' => 10,
- '#element_validate' => array('_file_generic_settings_max_filesize'),
- '#weight' => 5,
- );
-
- $form['description_field'] = array(
- '#type' => 'checkbox',
- '#title' => t('Show <em>File display title</em> field'),
- '#description' => t('This text will be used in links to the uploaded file. When not available, the filename will be used.'),
- '#default_value' => isset($settings['description_field']) ? $settings['description_field'] : '',
- '#parents' => array('instance', 'settings', 'description_field'),
- '#weight' => 11,
- );
-
-
- if (array_key_exists($instance['widget']['type'], file_field_inline_fields_widgets())) {
- $widget = $instance['widget'];
- $settings = $widget['settings'];
-
- $description = t('These options allow extended file fields to be edited inline after a file has been uploaded into this field. The fields displayed will depend upon the type of file uploaded. Setting or changing a value in these fields will affect the file everywhere throughout the site. More fields may be added in the <a href="!file-types">File types configuration</a>.', array('!file-types' => url('admin/structure/file-types')));
- if ($instance['entity_type'] == 'file') {
- $description = t('This option is <strong>disabled</strong>. Inline file fields are disabled for file fields attached to a <em>file type</em>.');
- }
-
- $form['inline_fields'] = array(
- '#type' => 'fieldset',
- '#title' => t('Inline fields'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#description' => $description,
- '#weight' => 30,
- '#parents' => array('instance', 'widget', 'settings'),
- );
-
- $form['inline_fields']['inline_fields_option'] = array(
- '#type' => 'select',
- '#title' => t('Inline fields option'),
- '#options' => array(
- FILE_INLINE_FIELDS_ALL => t('All available fields'),
- FILE_INLINE_FIELDS_SOME => t('Only specific fields'),
- FILE_INLINE_FIELDS_NONE => t('None (disabled)'),
- ),
- '#default_value' => isset($settings['inline_fields_option']) ? $settings['inline_fields_option'] : 0,
- '#weight' => -9,
- '#disabled' => ($instance['entity_type'] == 'file'),
- );
-
- $options = array();
- $option_occurences = array();
- $instance_info = field_info_instances('file');
- $bundle_info = field_info_bundles('file');
- foreach ($instance_info as $bundle_name => $bundle_fields) {
- foreach ($bundle_fields as $field_name => $field_instance) {
- $options[$field_name] = $field_instance['label'];
- $option_occurences[$field_name][] = $bundle_info[$bundle_name]['label'];
- }
- }
-
-
- foreach ($options as $field_name => $field_label) {
- $options[$field_name] .= ' (' . implode(',', $option_occurences[$field_name]) . ')';
- }
-
- $form['inline_fields']['inline_fields'] = array(
- '#title' => t('Included inline fields'),
- '#type' => 'checkboxes',
- '#options' => $options,
- '#default_value' => isset($settings['inline_fields']) ? $settings['inline_fields'] : array(),
- '#weight' => 0,
- '#states' => array(
- 'visible' => array(
- 'select[name="instance[widget][settings][inline_fields_option]"]' => array('value' => 2),
- ),
- ),
- '#element_validate' => array('_file_generic_settings_inline_fields_validate'),
- );
- }
-
- return $form;
- }
-
- * Render API callback: Validates the 'Maximum upload size' field.
- *
- * Ensures that a size has been entered and that it can be parsed by
- * parse_size().
- *
- * This function is assigned as an #element_validate callback in
- * file_field_instance_settings_form().
- */
- function _file_generic_settings_max_filesize($element, &$form_state) {
- if (!empty($element['#value']) && !is_numeric(parse_size($element['#value']))) {
- form_error($element, t('The "!name" option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).', array('!name' => t($element['title']))));
- }
- }
-
- * Render API callback: Validates the allowed file extensions field.
- *
- * This function is assigned as an #element_validate callback in
- * file_field_instance_settings_form().
- *
- * This doubles as a convenience clean-up function and a validation routine.
- * Commas are allowed by the end-user, but ultimately the value will be stored
- * as a space-separated list for compatibility with file_validate_extensions().
- */
- function _file_generic_settings_extensions($element, &$form_state) {
- if (!empty($element['#value'])) {
- $extensions = preg_replace('/([, ]+\.?)/', ' ', trim(strtolower($element['#value'])));
- $extensions = array_filter(explode(' ', $extensions));
- $extensions = implode(' ', array_unique($extensions));
- if (!preg_match('/^([a-z0-9]+([.][a-z0-9])* ?)+$/', $extensions)) {
- form_error($element, t('The list of allowed extensions is not valid, be sure to exclude leading dots and to separate extensions with a comma or space.'));
- }
- else {
- form_set_value($element, $extensions, $form_state);
- }
- }
- }
-
- * Render API callback: Validates the file destination field.
- *
- * Removes slashes from the beginning and end of the destination value and
- * ensures that the file directory path is not included at the beginning of the
- * value.
- *
- * This function is assigned as an #element_validate callback in
- * file_field_instance_settings_form().
- */
- function _file_generic_settings_file_directory_validate($element, &$form_state) {
-
- $value = trim($element['#value'], '\\/');
- form_set_value($element, $value, $form_state);
- }
-
- * Render API callback: Validates the selected fields.
- *
- * @param array $element
- * The element being validated.
- * @param array $form_state
- * A keyed array containing the current state of the form.
- *
- * @see file_field_instance_settings_form()
- */
- function _file_generic_settings_inline_fields_validate(array $element, array &$form_state) {
- $value = array_values(array_filter($element['#value']));
- form_set_value($element, $value, $form_state);
- }
-
- * Implements hook_field_load().
- */
- function file_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
-
- $fids = array();
- foreach ($entities as $id => $entity) {
-
- foreach ($items[$id] as $delta => $item) {
- if (!empty($item['fid'])) {
- $fids[] = $item['fid'];
- }
- }
- }
- $files = file_load_multiple($fids);
-
- foreach ($entities as $id => $entity) {
- foreach ($items[$id] as $delta => $item) {
-
- if (empty($item['fid']) || !isset($files[$item['fid']])) {
- $items[$id][$delta] = NULL;
- }
- else {
- $items[$id][$delta] = array_merge((array) $files[$item['fid']], $item);
- }
- }
- }
- }
-
- * Implements hook_field_prepare_view().
- */
- function file_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
-
- foreach ($entities as $id => $entity) {
- foreach ($items[$id] as $delta => $item) {
- if (!file_field_displayed($item, $field)) {
- unset($items[$id][$delta]);
- }
- }
-
- $items[$id] = array_values($items[$id]);
- }
- }
-
- * Implements hook_field_insert().
- */
- function file_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
- $items = file_field_expand_items($items);
-
-
- foreach ($items as $item) {
- if ($file = file_load($item['fid'])) {
- file_usage_add($file, 'file', $entity_type, $entity->id());
- }
- }
- }
-
- * Implements hook_field_update().
- *
- * Checks for files that have been removed from the object.
- */
- function file_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
- $items = file_field_expand_items($items);
-
-
- if (!isset($entity->{$field['field_name']})) {
-
- return;
- }
- $id = $entity->id();
-
-
-
- if (!empty($entity->revision)) {
- foreach ($items as $item) {
- if ($file = file_load($item['fid'])) {
- file_usage_add($file, 'file', $entity_type, $id);
- }
- }
- return;
- }
-
-
- $current_fids = array();
- foreach ($items as $item) {
- $current_fids[] = $item['fid'];
- }
-
-
- $original = $entity->original;
- $original_fids = array();
- if (!empty($original->{$field['field_name']}[$langcode])) {
- foreach ($original->{$field['field_name']}[$langcode] as $original_item) {
- $original_fids[] = $original_item['fid'];
- if (isset($original_item['fid']) && !in_array($original_item['fid'], $current_fids)) {
-
- if ($file = file_load($original_item['fid'])) {
- file_usage_delete($file, 'file', $entity_type, $id);
- }
- }
- }
- }
-
-
- foreach ($items as $item) {
- if (!in_array($item['fid'], $original_fids)) {
- if ($file = file_load($item['fid'])) {
- file_usage_add($file, 'file', $entity_type, $id);
- }
- }
- }
- }
-
- * Implements hook_field_delete().
- */
- function file_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
- list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
-
-
- foreach ($items as $delta => $item) {
- if ($file = file_load($item['fid'])) {
- file_usage_delete($file, 'file', $entity_type, $id, 0);
- }
- }
- }
-
- * Implements hook_field_delete_revision().
- */
- function file_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) {
- foreach ($items as $delta => $item) {
-
- if ($file = file_load($item['fid'])) {
- file_usage_delete($file, 'file', $entity_type, $entity->id());
- }
- }
- }
-
- * Implements hook_field_is_empty().
- */
- function file_field_is_empty($item, $field) {
- return empty($item['fid']);
- }
-
- * Expands multiple file IDs within a single item into separate item arrays.
- *
- * When using a multiple-value file field, an individual $item['fid'] may
- * contain an array of file IDs. Before saving values, each $item['fid'] should
- * only contain an integer. This helper converts multiple file IDs within a
- * single item into separate items.
- *
- * Calling this function is only needed when new files may be added, such as on
- * insert or update operations.
- *
- * @param array $items
- *
- * @return array
- * The modified list of $items.
- */
- function file_field_expand_items(array $items) {
- foreach ($items as $i => $item) {
- if (is_array($item['fid'])) {
- $multiple_fids = $item['fid'];
- $new_items = array();
- foreach ($multiple_fids as $f => $fid) {
-
- if ($f === 0) {
- $items[$i]['fid'] = $fid;
- }
-
- else {
- $item['fid'] = $fid;
- $new_items[] = $item;
- }
- }
-
- array_splice($items, $i + 1, 0, $new_items);
- }
- }
-
- return $items;
- }
-
- * Determines whether a file should be displayed when outputting field content.
- *
- * @param $item
- * A field item array.
- * @param $field
- * A field array.
- *
- * @return
- * Boolean TRUE if the file will be displayed, FALSE if the file is hidden.
- */
- function file_field_displayed($item, $field) {
- if (!empty($field['settings']['display_field'])) {
- return (bool) $item['display'];
- }
- return TRUE;
- }
-
- * Implements hook_field_formatter_info().
- */
- function file_field_formatter_info() {
- return array(
- 'file_default' => array(
- 'label' => t('Generic file'),
- 'field types' => array('file'),
- 'description' => t('Create a simple link to the file. The link is prefixed by a file type icon and the name of the file is used as the link text'),
- ),
- 'file_table' => array(
- 'label' => t('Table of files'),
- 'field types' => array('file'),
- 'description' => t('Build a two-column table where the first column contains a generic link to the file and the second column displays the size of the file.'),
- ),
- 'file_url_plain' => array(
- 'label' => t('URL to file'),
- 'field types' => array('file'),
- 'description' => t('Display a plain text URL to the file.'),
- ),
- 'file_rendered' => array(
- 'label' => t('Rendered file'),
- 'description' => t('Display the file in a specific view mode'),
- 'field types' => array('file', 'image'),
- 'settings' => array(
- 'file_view_mode' => 'default',
- ),
- 'file formatter' => array(
- 'hidden' => TRUE,
- ),
- ),
- 'file_download_link' => array(
- 'label' => t('Download link'),
- 'description' => t('Displays a link that will force the browser to download the file.'),
- 'field types' => array('file', 'image'),
- 'settings' => array(
- 'text' => t('Download [file:name]'),
- ),
- ),
- 'file_audio' => array(
- 'label' => t('Audio'),
- 'description' => t('Render the file using an HTML5 audio tag.'),
- 'field types' => array('file'),
- 'settings' => array(
- 'controls' => TRUE,
- 'autoplay' => FALSE,
- 'loop' => FALSE,
- 'preload' => '',
- 'multiple_file_behavior' => 'tags',
- ),
- 'file formatter' => array(
- 'mime types' => array('audio/*'),
- ),
- ),
- 'file_video' => array(
- 'label' => t('Video'),
- 'description' => t('Render the file using an HTML5 video tag.'),
- 'field types' => array('file'),
- 'settings' => array(
- 'controls' => TRUE,
- 'autoplay' => FALSE,
- 'loop' => FALSE,
- 'muted' => FALSE,
- 'width' => NULL,
- 'height' => NULL,
- 'preload' => '',
- 'multiple_file_behavior' => 'tags',
- ),
- 'file formatter' => array(
- 'mime types' => array('video/*'),
- ),
- ),
- );
- }
-
- * Implements hook_field_formatter_settings_form().
- */
- function file_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
- $display = $instance['display'][$view_mode];
- $settings = $display['settings'];
- $element = array();
-
- if ($display['type'] == 'file_rendered') {
- $element['file_view_mode'] = array(
- '#title' => t('View mode'),
- '#type' => 'select',
- '#options' => file_view_mode_labels(),
- '#default_value' => $settings['file_view_mode'],
-
- );
- }
- elseif ($display['type'] == 'file_download_link') {
- $element['text'] = array(
- '#type' => 'textfield',
- '#title' => t('Link text'),
- '#description' => t('This field supports tokens.'),
- '#default_value' => $settings['text'],
- '#required' => TRUE,
- );
- }
- elseif ($display['type'] == 'file_audio') {
- $element['controls'] = array(
- '#title' => t('Show audio controls'),
- '#type' => 'checkbox',
- '#default_value' => $settings['controls'],
- );
- $element['autoplay'] = array(
- '#title' => t('Autoplay'),
- '#type' => 'checkbox',
- '#default_value' => $settings['autoplay'],
- );
- $element['loop'] = array(
- '#title' => t('Loop'),
- '#type' => 'checkbox',
- '#default_value' => $settings['loop'],
- );
- $element['preload'] = array(
- '#title' => t('Preload'),
- '#type' => 'select',
- '#default_value' => $settings['preload'],
- '#options' => backdrop_map_assoc(array('none', 'auto', 'metadata')),
- '#empty_option' => 'unspecified',
- );
- $element['multiple_file_behavior'] = array(
- '#title' => t('Display of multiple files'),
- '#type' => 'radios',
- '#options' => array(
- 'tags' => t('Use multiple @tag tags, each with a single source', array('@tag' => '<audio>')),
- 'sources' => t('Use multiple sources within a single @tag tag', array('@tag' => '<audio>')),
- ),
- '#default_value' => $settings['multiple_file_behavior'],
-
- '#access' => !empty($field),
- );
-
- }
- elseif ($display['type'] == 'file_video') {
- $element['controls'] = array(
- '#title' => t('Show video controls'),
- '#type' => 'checkbox',
- '#default_value' => $settings['controls'],
- );
- $element['autoplay'] = array(
- '#title' => t('Autoplay'),
- '#type' => 'checkbox',
- '#default_value' => $settings['autoplay'],
- );
- $element['loop'] = array(
- '#title' => t('Loop'),
- '#type' => 'checkbox',
- '#default_value' => $settings['loop'],
- );
- $element['muted'] = array(
- '#title' => t('Muted'),
- '#type' => 'checkbox',
- '#default_value' => $settings['muted'],
- );
- $element['width'] = array(
- '#type' => 'textfield',
- '#title' => t('Width'),
- '#default_value' => $settings['width'],
- '#size' => 5,
- '#maxlength' => 5,
- '#field_suffix' => t('pixels'),
- );
- $element['height'] = array(
- '#type' => 'textfield',
- '#title' => t('Height'),
- '#default_value' => $settings['height'],
- '#size' => 5,
- '#maxlength' => 5,
- '#field_suffix' => t('pixels'),
- );
- $element['preload'] = array(
- '#title' => t('Preload'),
- '#type' => 'select',
- '#default_value' => $settings['preload'],
- '#options' => backdrop_map_assoc(array('none', 'auto', 'metadata')),
- '#empty_option' => 'unspecified',
- );
- $element['multiple_file_behavior'] = array(
- '#title' => t('Display of multiple files'),
- '#type' => 'radios',
- '#options' => array(
- 'tags' => t('Use multiple @tag tags, each with a single source', array('@tag' => '<video>')),
- 'sources' => t('Use multiple sources within a single @tag tag', array('@tag' => '<video>')),
- ),
- '#default_value' => $settings['multiple_file_behavior'],
-
- '#access' => !empty($field),
- );
- }
-
- return $element;
- }
-
- * Implements hook_field_formatter_settings_summary().
- */
- function file_field_formatter_settings_summary($field, $instance, $view_mode) {
- $display = $instance['display'][$view_mode];
- $settings = $display['settings'];
- $summary = array();
-
- if ($display['type'] === 'file_rendered') {
- $view_mode_label = file_view_mode_label($settings['file_view_mode'], t('Unknown'));
- $summary[] = t('View mode: %mode', array('%mode' => $view_mode_label));
- }
- elseif ($display['type'] == 'file_download_link') {
- $summary[] = t('Link text: %text', array('%text' => $settings['text']));
- }
- elseif ($display['type'] === 'file_audio') {
- if (isset($settings['controls'])) {
- $summary[] = t('Controls: %controls', array('%controls' => $settings['controls'] ? 'visible' : 'hidden'));
- }
- if (isset($settings['autoplay'])) {
- $summary[] = t('Autoplay: %autoplay', array('%autoplay' => $settings['autoplay'] ? t('yes') : t('no')));
- }
- if (isset($settings['loop'])) {
- $summary[] = t('Loop: %loop', array('%loop' => $settings['loop'] ? t('yes') : t('no')));
- }
- if (!empty($settings['preload'])) {
- $summary[] = t('Preload: %preload', array('%preload' => $settings['preload']));
- }
- if (isset($settings['multiple_file_behavior'])) {
- $summary[] = t('Multiple files: %multiple', array('%multiple' => $settings['multiple_file_behavior']));
- }
- }
- elseif ($display['type'] === 'file_video') {
- if (isset($settings['controls'])) {
- $summary[] = t('Controls: %controls', array('%controls' => $settings['controls'] ? 'visible' : 'hidden'));
- }
- if (isset($settings['autoplay'])) {
- $summary[] = t('Autoplay: %autoplay', array('%autoplay' => $settings['autoplay'] ? t('yes') : t('no')));
- }
- if (isset($settings['loop'])) {
- $summary[] = t('Loop: %loop', array('%loop' => $settings['loop'] ? t('yes') : t('no')));
- }
- if (isset($settings['muted'])) {
- $summary[] = t('Muted: %muted', array('%muted' => $settings['muted'] ? t('yes') : t('no')));
- }
- if ($settings['width'] && $settings['height']) {
- $summary[] = t('Size: %width x %height', array('%width' => $settings['width'], '%height' => $settings['height']));
- }
- if (!empty($settings['preload'])) {
- $summary[] = t('Preload: %preload', array('%preload' => $settings['preload']));
- }
- if (isset($settings['multiple_file_behavior'])) {
- $summary[] = t('Multiple files: %multiple', array('%multiple' => $settings['multiple_file_behavior']));
- }
- }
-
- return implode('<br />', $summary);
- }
-
- * Implements hook_field_widget_info().
- */
- function file_field_widget_info() {
- $info = array(
- 'file_generic' => array(
- 'label' => t('File'),
- 'field types' => array('file'),
- 'settings' => array(
- 'progress_indicator' => 'throbber',
- ),
- 'behaviors' => array(
- 'multiple values' => FIELD_BEHAVIOR_CUSTOM,
- 'default value' => FIELD_BEHAVIOR_NONE,
- ),
- ),
- );
-
-
- foreach (file_field_inline_fields_widgets() as $widget_type => $widget) {
- if (isset($info[$widget_type]['settings'])) {
- $info[$widget_type]['settings'] += array(
- 'inline_fields_option' => 1,
- 'inline_fields' => array(),
- );
- }
- }
-
- return $info;
- }
-
- * Implements hook_field_widget_settings_form().
- */
- function file_field_widget_settings_form($field, $instance) {
- $widget = $instance['widget'];
- $settings = $widget['settings'];
-
- $form['progress_indicator'] = array(
- '#type' => 'radios',
- '#title' => t('Progress indicator'),
- '#options' => array(
- 'throbber' => t('Throbber'),
- 'bar' => t('Bar with progress meter'),
- ),
- '#default_value' => $settings['progress_indicator'],
- '#description' => t('The throbber display does not show the status of uploads but takes up less space. The progress bar is helpful for monitoring progress on large uploads.'),
- '#weight' => 16,
- );
-
- return $form;
- }
-
- * Implements hook_field_widget_form().
- */
- function file_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
-
- $defaults = array(
- 'fid' => NULL,
- 'display' => empty($field['settings']['display_default']) ? 0 : 1,
- 'description' => '',
- );
-
-
-
-
- $field_state = field_form_get_state($element['#field_parents'], $field['field_name'], $langcode, $form_state);
- if (isset($field_state['items'])) {
- $items = $field_state['items'];
- }
-
-
- $element_info = element_info('managed_file');
- $element += array(
- '#type' => 'managed_file',
- '#upload_location' => file_field_widget_uri($field, $instance),
- '#upload_validators' => file_field_widget_upload_validators($field, $instance),
- '#value_callback' => 'file_field_widget_value',
- '#process' => array_merge($element_info['#process'], array('file_field_widget_process')),
- '#progress_indicator' => $instance['widget']['settings']['progress_indicator'],
-
- '#extended' => TRUE,
- );
-
-
-
-
- if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && $field['cardinality'] > 1) {
- $limit = $field['cardinality'] - count($items);
- $element['#upload_validators']['file_validate_upload_limit'] = array($limit);
- }
-
- if ($field['cardinality'] == 1) {
-
- $element['#default_value'] = !empty($items) ? $items[0] : $defaults;
-
- if (empty($element['#default_value']['fid'])) {
- $element['#description'] = theme('file_upload_help', array('description' => $element['#description'], 'upload_validators' => $element['#upload_validators']));
- }
- $elements = array($element);
- }
- else {
-
- foreach ($items as $item) {
- $elements[$delta] = $element;
- $elements[$delta]['#default_value'] = $item;
- $elements[$delta]['#weight'] = $delta;
- if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED) {
- $elements[$delta]['#element_validate'] = array('file_field_widget_multiple_count_validate');
- }
- $delta++;
- }
-
-
- 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);
- $elements[$delta]['#multiple'] = TRUE;
- }
-
-
- $elements['#file_upload_delta'] = $delta;
- $elements['#theme'] = 'file_widget_multiple';
- $elements['#theme_wrappers'] = array('fieldset');
- $elements['#process'] = array('file_field_widget_process_multiple');
- $elements['#title'] = $element['#title'];
- $elements['#description'] = $element['#description'];
- $elements['#field_name'] = $element['#field_name'];
- $elements['#language'] = $element['#language'];
- $elements['#display_field'] = $field['settings']['display_field'];
-
-
-
-
- $elements['#file_upload_title'] = format_plural($field['cardinality'], 'Add a new file', 'Add new files');
- $elements['#file_upload_description'] = theme('file_upload_help', array(
- 'description' => '',
- 'upload_validators' => $elements[0]['#upload_validators'],
- 'cardinality' => $field['cardinality'],
- ));
- }
-
-
- if (array_key_exists($instance['widget']['type'], file_field_inline_fields_widgets())) {
- foreach (element_children($elements) as $key) {
- $elements[$key] = file_field_inline_fields_widget($elements[$key], $form_state, $instance);
- }
- }
-
- return $elements;
- }
-
- * Adds the additional file entity fields to the widget.
- *
- * @param array $element
- * The element being processed.
- * @param array $form_state
- * A keyed array containing the current state of the form.
- * @param array $instance
- * The field instance.
- *
- * @return array
- * The element with fields attached, or just the element if no fields to be
- * attached.
- */
- function file_field_inline_fields_widget(array $element, array &$form_state, array $instance) {
- $element['#process'] = array_merge($element['#process'], array('file_field_inline_fields_widget_process'));
-
-
- $file = (object) $element['#default_value'];
- if (empty($file->fid) || !isset($element['#entity_type']) || !isset($element['#field_name'])) {
- return $element;
- }
-
-
- if ($element['#entity_type'] === 'file') {
- return $element;
- }
-
-
- $widget_settings = $instance['widget']['settings'];
-
-
- if (!isset($widget_settings['inline_fields_option']) || $widget_settings['inline_fields_option'] == FILE_INLINE_FIELDS_NONE) {
- return $element;
- }
-
-
-
-
- if (!isset($file->type)) {
-
- $query = db_query('SELECT * FROM {file_managed} WHERE fid = :fid', array(
- ':fid' => $file->fid,
- ));
- $results = $query->fetchAssoc();
- if (!empty($results)) {
- foreach ($results as $key => $val) {
- $file->{$key} = $val;
- }
- }
- }
-
-
- $element['entity'] = array(
- '#type' => 'container',
- '#attributes' => array(
- 'class' => array('file-inline-fields'),
- ),
- '#indentation' => ($instance['widget']['type'] == 'file_generic') ? 2 : 0,
- '#weight' => 10,
- '#parents' => array('files', $file->fid),
- );
- $file = file_load($file->fid);
- field_attach_form('file', $file, $element['entity'], $form_state, $element['#language']);
-
-
- if (isset($widget_settings['inline_fields_option']) && $widget_settings['inline_fields_option'] == FILE_INLINE_FIELDS_SOME) {
- $disabled_fields = array_diff(element_children($element['entity']), $widget_settings['inline_fields']);
- foreach ($disabled_fields as $form_key) {
- $element['entity'][$form_key]['#access'] = FALSE;
- }
- }
-
- return $element;
- }
-
- * Retrieves the upload validators for a file field.
- *
- * @param $field
- * A field array.
- *
- * @return array
- * An array suitable for passing to file_save_upload() or the file field
- * element's '#upload_validators' property.
- */
- function file_field_widget_upload_validators($field, $instance) {
-
- $max_filesize = parse_size(file_upload_max_size());
- if (!empty($instance['settings']['max_filesize']) && parse_size($instance['settings']['max_filesize']) < $max_filesize) {
- $max_filesize = parse_size($instance['settings']['max_filesize']);
- }
-
- $validators = array();
-
-
- $validators['file_validate_size'] = array($max_filesize);
-
-
- if (!empty($instance['settings']['file_extensions'])) {
- $validators['file_validate_extensions'] = array($instance['settings']['file_extensions']);
- }
-
- return $validators;
- }
-
- * Determines the URI for a file field instance.
- *
- * @param $field
- * A field array.
- * @param $instance
- * A field instance array.
- * @param $data
- * An array of token objects to pass to token_replace().
- *
- * @return
- * A file directory URI with tokens replaced.
- *
- * @see token_replace()
- */
- function file_field_widget_uri($field, $instance, $data = array()) {
- $destination = trim($instance['settings']['file_directory'], '/');
-
-
- $destination = token_replace($destination, $data);
-
- return $field['settings']['uri_scheme'] . '://' . $destination;
- }
-
- * Render API callback: Retrieves the value for the file_generic field element.
- *
- * This function is assigned as a #value callback in file_field_widget_form().
- */
- function file_field_widget_value($element, $input, $form_state) {
- if ($input) {
-
-
- $field = field_widget_field($element, $form_state);
- if (empty($input['display'])) {
- $input['display'] = !empty($field['settings']['display_field']) ? 0 : 1;
- }
- }
-
-
- $return = file_managed_file_value($element, $input, $form_state);
-
-
- $return += array(
- 'fid' => NULL,
- 'display' => 1,
- 'description' => '',
- );
-
- return $return;
- }
-
-
- * Validation callback for managed file element on file field widgets.
- *
- * Checks if more files than allowed have been uploaded or added via image
- * library, truncates values to the limit and shows a warning message.
- * This validator is only used when cardinality not set to 1 or unlimited.
- *
- * @see file_field_widget_form()
- */
- function file_field_widget_multiple_count_validate($element, &$form_state, $form) {
- $parents = $element['#parents'];
- array_pop($parents);
- $values = backdrop_array_get_nested_value($form_state['values'], $parents);
- $item_count = count($values);
-
- $field = field_info_field($element['#field_name']);
- if ($item_count > $field['cardinality']) {
- $keep_items = array();
- $remove_item_names = array();
- foreach ($values as $delta => $value_array) {
- if ($delta < $field['cardinality']) {
- $keep_items[] = $value_array;
- }
- else {
- $file = file_load($value_array['fid']);
- $remove_item_names[] = $file->filename;
- }
- }
- backdrop_array_set_nested_value($form_state['values'], $parents, $keep_items);
- backdrop_set_message(
- t(
- 'Field %field can only hold @max values but there were @count. The following files have been omitted as a result: %list.',
- array(
- '%field' => $field['field_name'],
- '@max' => $field['cardinality'],
- '@count' => $item_count,
- '%list' => implode(', ', $remove_item_names),
- )
- ),
- 'warning'
- );
- }
- }
-
- * Render API callback: Processes a file_generic field element.
- *
- * Expands the file_generic type to include the description and display fields.
- *
- * This function is assigned as a #process callback in file_field_widget_form().
- */
- function file_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'] = 'file_widget';
-
-
- if (!empty($field['settings']['display_field'])) {
- $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',
- );
- }
-
-
- if (!empty($instance['settings']['description_field']) && $item['fid']) {
- $element['description'] = array(
- '#type' => 'textfield',
- '#title' => t('File display title'),
- '#description' => t('This text will be used in links to the uploaded file.'),
- '#value' => isset($item['description']) ? $item['description'] : '',
- '#maxlength' => 500,
- );
- }
-
-
-
- if ($field['cardinality'] != 1) {
- $parents = array_slice($element['#array_parents'], 0, -1);
- $new_path = 'file/ajax/' . implode('/', $parents) . '/' . $form['form_build_id']['#value'];
-
-
- $token = backdrop_hmac_base64($new_path, backdrop_get_private_key() . backdrop_get_hash_salt());
- $new_path .= '/' . $token;
- $field_element = backdrop_array_get_nested_value($form, $parents);
- $new_wrapper = $field_element['#id'] . '-ajax-wrapper';
- foreach (element_children($element) as $key) {
- if (isset($element[$key]['#ajax']['path'])) {
- $element[$key]['#ajax']['path'] = $new_path;
- $element[$key]['#ajax']['wrapper'] = $new_wrapper;
- }
- }
- unset($element['#prefix'], $element['#suffix']);
- }
-
-
-
-
-
- foreach (array('upload_button', 'remove_button') as $key) {
- $element[$key]['#submit'][] = 'file_field_widget_submit';
- $element[$key]['#limit_validation_errors'] = array(array_slice($element['#parents'], 0, -1));
- }
-
- return $element;
- }
-
- * Render API callback: Processes a file_generic field element.
- *
- * Expands the file_generic type to include the file entity fields.
- *
- * @param array $element
- * The element being processed.
- * @param array $form_state
- * A keyed array of the form state.
- * @param array $form
- * The parent form, passed by reference.
- *
- * @return array
- * The element that is processed.
- *
- * @see file_field_inline_fields_widget()
- */
- function file_field_inline_fields_widget_process(array $element, array &$form_state, array &$form) {
-
- if (empty($element['fid']['#value']) || !isset($element['#entity_type']) || !isset($element['#field_name'])) {
- return $element;
- }
-
-
- if ($element['#entity_type'] === 'file') {
- return $element;
- }
-
-
- if (array_search('file_field_inline_fields_submit', $form['#submit']) === FALSE) {
- $form['#submit'][] = 'file_field_inline_fields_submit';
- }
-
- return $element;
- }
-
- * Callback handler for inline fields submission.
- *
- * Saves the file entity field values.
- *
- * @param array $form
- * The form to be submitted.
- * @param array $form_state
- * A keyed array of the form state.
- *
- * @see file_field_inline_fields_widget_process()
- */
- function file_field_inline_fields_submit(array $form, array &$form_state) {
- if (!isset($form_state['values']['files'])) {
- return;
- }
-
-
- foreach ($form_state['values']['files'] as $fid => $file_values) {
- $file = file_load($fid);
- $file_form_state = array(
- 'values' => $file_values,
- );
- field_attach_submit('file', $file, $form, $file_form_state);
- file_save($file);
- }
- }
-
- * Render API callback: Processes a group of file_generic field elements.
- *
- * 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.
- *
- * This function is assigned as a #process callback in file_field_widget_form().
- */
- function file_field_widget_process_multiple($element, &$form_state, $form) {
- $element_children = element_children($element, TRUE);
- $count = count($element_children);
-
- foreach ($element_children as $delta => $key) {
- if ($key != $element['#file_upload_delta']) {
- $description = _file_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 {
-
-
- $element[$key]['#title'] = $element['#title'];
- $element[$key]['_weight'] = array(
- '#type' => 'hidden',
- '#default_value' => $delta,
- );
- }
- }
-
-
- $element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">';
- $element['#suffix'] = '</div>';
-
- return $element;
- }
-
- * Retrieves the file description from a field field element.
- *
- * This helper function is used by file_field_widget_process_multiple().
- *
- * @param $element
- * The element being processed.
- *
- * @return
- * A description of the file suitable for use in the administrative interface.
- */
- function _file_field_get_description_from_element($element) {
-
- if (!empty($element['#default_value']['description'])) {
- return $element['#default_value']['description'];
- }
-
- if (!empty($element['#default_value']['filename'])) {
- return $element['#default_value']['filename'];
- }
-
- return FALSE;
- }
-
- * Form submission handler for upload/remove button of file_field_widget_form().
- *
- * This runs in addition to and after file_managed_file_submit().
- *
- * @see file_managed_file_submit()
- * @see file_field_widget_form()
- * @see file_field_widget_process()
- */
- function file_field_widget_submit($form, &$form_state) {
-
-
-
-
-
- $parents = array_slice($form_state['triggering_element']['#parents'], 0, -2);
- backdrop_array_set_nested_value($form_state['input'], $parents, NULL);
-
- $button = $form_state['triggering_element'];
-
-
- $element = backdrop_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -1));
- $field_name = $element['#field_name'];
- $langcode = $element['#language'];
- $parents = $element['#field_parents'];
-
- $submitted_values = backdrop_array_get_nested_value($form_state['values'], array_slice($button['#parents'], 0, -2));
- foreach ($submitted_values as $delta => $submitted_value) {
- if (empty($submitted_value['fid'])) {
- unset($submitted_values[$delta]);
- }
- }
-
-
-
- $new_values = array();
- foreach ($submitted_values as $delta => $submitted_value) {
- if (is_array($submitted_value['fid'])) {
- foreach ($submitted_value['fid'] as $fid) {
- $new_value = $submitted_value;
- $new_value['fid'] = $fid;
- $new_values[] = $new_value;
- }
- }
- else {
- $new_values[] = $submitted_value;
- }
- }
-
-
- $submitted_values = array_values($new_values);
-
-
- backdrop_array_set_nested_value($form_state['values'], array_slice($button['#parents'], 0, -2), $submitted_values);
-
-
- $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);
- }
-
- * Implements hook_field_formatter_prepare_view().
- */
- function file_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
-
-
-
- foreach (array_keys($entities) as $id) {
- $items[$id] = array_filter($items[$id]);
- }
- }
-
- * Implements hook_field_formatter_view().
- */
- function file_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
- $element = array();
- $settings = isset($display['settings']) ? $display['settings'] : array();
-
- switch ($display['type']) {
- case 'file_default':
- foreach ($items as $delta => $item) {
- $element[$delta] = array(
- '#theme' => 'file_link',
- '#file' => (object) $item,
- );
- }
- break;
-
- case 'file_url_plain':
- foreach ($items as $delta => $item) {
- $element[$delta] = array('#markup' => empty($item['uri']) ? '' : file_create_url($item['uri']));
- }
- break;
-
- case 'file_table':
- if (!empty($items)) {
-
- $element[0] = array(
- '#theme' => 'file_formatter_table',
- '#items' => $items,
- );
- }
- break;
-
- case 'file_rendered':
- foreach ($items as $delta => $item) {
-
- static $recursive_render_depth = array();
- if (!empty($item)) {
-
- if (config_get('file.settings', 'protect_repeated_render')) {
-
- static $repeated_render_depth = array();
- $repeated_render_id = $entity_type . $field['field_name'] . $item['fid'];
- if (isset($repeated_render_depth[$repeated_render_id])) {
- $repeated_render_depth[$repeated_render_id]++;
- }
- else {
- $repeated_render_depth[$repeated_render_id] = 1;
- }
-
- if ($repeated_render_depth[$repeated_render_id] > 10) {
- watchdog(
- 'file_entity',
- 'Repeated rendering detected when rendering entity %entity_type: %entity_id, using the %field_name field. Aborting rendering.',
- array(
- '%entity_type' => 'file',
- '%entity_id' => $item['fid'],
- '%field_name' => $field['field_name'],
- ),
- WATCHDOG_ERROR
- );
- return $element;
- }
- }
-
- $file = file_load($item['fid']);
- if (isset($item['display'])) {
- $file->display = $item['display'];
- }
- if (isset($item['description'])) {
- $file->description = $item['description'];
- }
-
-
-
- $file->referencing_entity = $entity;
- $file->referencing_entity_type = $entity_type;
- $file->referencing_field = $field['field_name'];
-
-
-
- $element[$delta] = file_view($file, $settings['file_view_mode'], $langcode !== LANGUAGE_NONE ? $langcode : NULL);
- }
- else {
- $variables = array(
- '%referencing_entity_type' => $entity_type,
- '%field_name' => $field['field_name'],
- '%entity_type' => 'file',
- );
- $message = 'In the %referencing_entity_type, the %field_name field refers to a %entity_type which does not exist. Aborting the render for it.';
- watchdog('file', $message, $variables, WATCHDOG_ERROR);
- }
- }
- break;
-
- case 'file_download_link':
-
- $items = array_filter($items);
- foreach ($items as $delta => $item) {
- if (!empty($item['fid']) && ($file = file_load($item['fid'])) && file_access('download', $file)) {
- $element[$delta] = array(
- '#theme' => 'file_download_link',
- '#file' => $file,
- '#text' => $settings['text'],
- );
- }
- }
- break;
-
- case 'file_audio':
- $multiple_file_behavior = $settings['multiple_file_behavior'];
-
-
- $source_lists = array();
- if ($multiple_file_behavior == 'tags') {
- foreach ($items as $delta => $item) {
- if (file_get_mimetype_type($item) == 'audio') {
- $source_lists[$delta] = array($item);
- }
- }
- }
- else {
- foreach ($items as $delta => $item) {
- if (file_get_mimetype_type($item) == 'audio') {
- $source_lists[0][$delta] = $item;
- }
- }
- }
-
-
- foreach ($source_lists as $delta => $sources) {
- $element[$delta] = array(
- '#theme' => 'file_audio',
- '#files' => $sources,
- '#controls' => $settings['controls'],
- '#autoplay' => $settings['autoplay'],
- '#loop' => $settings['loop'],
- '#preload' => $settings['preload'],
- );
- }
- break;
-
- case 'file_video':
- $multiple_file_behavior = $settings['multiple_file_behavior'];
-
-
- $source_lists = array();
- if ($multiple_file_behavior == 'tags') {
- foreach ($items as $delta => $item) {
- if (file_get_mimetype_type($item) == 'video') {
- $source_lists[$delta] = array($item);
- }
- }
- }
- else {
- foreach ($items as $delta => $item) {
- if (file_get_mimetype_type($item) == 'video') {
- $source_lists[0][$delta] = $item;
- }
- }
- }
-
-
- foreach ($source_lists as $delta => $sources) {
- $element[$delta] = array(
- '#theme' => 'file_video',
- '#files' => $sources,
- '#controls' => $settings['controls'],
- '#autoplay' => $settings['autoplay'],
- '#loop' => $settings['loop'],
- '#muted' => $settings['muted'],
- '#width' => $settings['width'],
- '#height' => $settings['height'],
- '#preload' => $settings['preload'],
- );
- }
- break;
-
- }
-
- return $element;
- }
-
- * Get a list of all file field widgets that support inline fields.
- *
- * @param bool $reset
- * TRUE to reset the static cache, FALSE by default.
- *
- * @return array
- * An array of widgets that can support inline fields.
- */
- function file_field_inline_fields_widgets($reset = FALSE) {
- static $widgets;
-
- if (!isset($widgets) || $reset) {
- $widgets = array();
- foreach (module_implements('file_field_inline_fields_widgets') as $module) {
- $module_widgets = module_invoke($module, 'file_field_inline_fields_widgets');
- foreach ($module_widgets as $type => $widget) {
- $module_widgets[$type]['type'] = $type;
- $module_widgets[$type]['module'] = $module;
- }
- $widgets = array_merge($widgets, $module_widgets);
- }
- backdrop_alter('file_field_inline_fields_widgets', $widgets);
- }
-
- return $widgets;
- }
-
- * Implements hook_file_field_inline_fields_widgets().
- */
- function file_file_field_inline_fields_widgets() {
- $widgets = array();
- $widgets['file_generic'] = array(
- 'element_type' => 'managed_file',
- );
- $widgets['image_image'] = array(
- 'element_type' => 'managed_file',
- );
- return $widgets;
- }