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' => file_default_scheme(), ), 'instance_settings' => array( 'file_extensions' => 'txt', 'file_directory' => '', 'max_filesize' => '', 'description_field' => 0, ), 'default_widget' => 'file_generic', 'default_formatter' => 'file_default', 'field item class' => '\Drupal\file\Type\FileItem', ), ); } /** * 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']['library'][] = array('file', 'drupal.file'); $form['display_field'] = array( '#type' => 'checkbox', '#title' => t('Enable Display field'), '#default_value' => $settings['display_field'], '#description' => t('The display option allows users to choose if a file should be shown when viewing the content.'), ); $form['display_default'] = array( '#type' => 'checkbox', '#title' => t('Files displayed by default'), '#default_value' => $settings['display_default'], '#description' => t('This setting only has an effect if the display option is enabled.'), '#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'], '#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, ); // Make the extension list a little more human-friendly by comma-separation. $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, // By making this field required, we prevent a potential security issue // that would allow files of any type to be uploaded. '#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 %limit).', 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('Enable Description field'), '#default_value' => isset($settings['description_field']) ? $settings['description_field'] : '', '#description' => t('The description field allows users to enter a description about the uploaded file.'), '#parents' => array('instance', 'settings', 'description_field'), '#weight' => 11, ); 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) { // Strip slashes from the beginning and end of $widget['file_directory']. $value = trim($element['#value'], '\\/'); form_set_value($element, $value, $form_state); } /** * Implements hook_field_prepare_view(). */ function file_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) { // Remove files specified to not be displayed. $fids = array(); foreach ($entities as $id => $entity) { foreach ($items[$id] as $delta => $item) { if (!file_field_displayed($item, $field)) { unset($items[$id][$delta]); } elseif (!empty($item['fid'])) { // Load the files from the files table. $fids[] = $item['fid']; } } // Ensure consecutive deltas. $items[$id] = array_values($items[$id]); } $files = file_load_multiple($fids); foreach ($entities as $id => $entity) { foreach ($items[$id] as $delta => $item) { // If the file does not exist, mark the entire item as empty. if (empty($item['fid']) || !isset($files[$item['fid']])) { $items[$id][$delta] = NULL; } else { $items[$id][$delta] = array_merge($item, (array) $files[$item['fid']]); } } } } /** * Implements hook_field_insert(). */ function file_field_insert(EntityInterface $entity, $field, $instance, $langcode, &$items) { // Add a new usage of each uploaded file. foreach ($items as $item) { file_usage()->add(file_load($item['fid']), 'file', $entity->entityType(), $entity->id()); } } /** * Implements hook_field_update(). * * Checks for files that have been removed from the object. */ function file_field_update(EntityInterface $entity, $field, $instance, $langcode, &$items) { // On new revisions, all files are considered to be a new usage and no // deletion of previous file usages are necessary. if (!empty($entity->original) && $entity->getRevisionId() != $entity->original->getRevisionId()) { foreach ($items as $item) { file_usage()->add(file_load($item['fid']), 'file', $entity->entityType(), $entity->id()); } return; } // Build a display of the current FIDs. $current_fids = array(); foreach ($items as $item) { $current_fids[] = $item['fid']; } // Compare the original field values with the ones that are being saved. $original = $entity->original->getBCEntity(); $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)) { // Decrement the file usage count by 1. file_usage()->delete(file_load($original_item['fid']), 'file', $entity->entityType(), $entity->id()); } } } // Add new usage entries for newly added files. foreach ($items as $item) { if (!in_array($item['fid'], $original_fids)) { file_usage()->add(file_load($item['fid']), 'file', $entity->entityType(), $entity->id()); } } } /** * Implements hook_field_delete(). */ function file_field_delete(EntityInterface $entity, $field, $instance, $langcode, &$items) { // Delete all file usages within this entity. foreach ($items as $delta => $item) { file_usage()->delete(file_load($item['fid']), 'file', $entity->entityType(), $entity->id(), 0); } } /** * Implements hook_field_delete_revision(). */ function file_field_delete_revision(EntityInterface $entity, $field, $instance, $langcode, &$items) { foreach ($items as $delta => $item) { // Decrement the file usage count by 1. file_usage()->delete(file_load($item['fid']), 'file', $entity->entityType(), $entity->id()); } } /** * Implements hook_field_is_empty(). */ function file_field_is_empty($item, $field) { return empty($item['fid']); } /** * 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; } /** * Retrieves the upload validators for a file field. * * @param $field * A field array. * * @return * 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) { // Cap the upload size according to the PHP limit. $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(); // There is always a file size limit due to the PHP server limit. $validators['file_validate_size'] = array($max_filesize); // Add the extension check if necessary. 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 * \Drupal\Core\Utility\Token::replace(). * * @return * A file directory URI with tokens replaced. * * @see \Drupal\Core\Utility\Token::replace() */ function file_field_widget_uri($field, $instance, $data = array()) { $destination = trim($instance['settings']['file_directory'], '/'); // Replace tokens. $destination = Drupal::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 = 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'])) { $input['display'] = $field['settings']['display_field'] ? 0 : 1; } } // We depend on the managed file element to handle uploads. $return = file_managed_file_value($element, $input, $form_state); // Ensure that all the required properties are returned even if empty. $return += array( 'fids' => array(), 'display' => 1, 'description' => '', ); return $return; } /** * Validation callback for upload element on file widget. Checks if user has * uploaded more files than allowed. * * This validator is used only when cardinality not set to 1 or unlimited. */ function file_field_widget_multiple_count_validate($element, &$form_state, $form) { $parents = $element['#parents']; $values = NestedArray::getValue($form_state['values'], $parents); array_pop($parents); $current = count(element_children(NestedArray::getValue($form, $parents))) - 1; $field = field_info_field($element['#field_name']); $uploaded = count($values['fids']); $count = $uploaded + $current; if ($count > $field['cardinality']) { $keep = $uploaded - $count + $field['cardinality']; $removed_files = array_slice($values['fids'], $keep); $removed_names = array(); foreach ($removed_files as $fid) { $file = file_load($fid); $removed_names[] = $file->filename; } drupal_set_message( t( 'Field %field can only hold @max values but there were @count uploaded. The following files have been omitted as a result: %list.', array( '%field' => $field['field_name'], '@max' => $field['cardinality'], '@count' => $keep, '%list' => implode(', ', $removed_names), ) ), 'warning' ); $values['fids'] = array_slice($values['fids'], 0, $keep); NestedArray::setValue($form_state['values'], $element['#parents'], $values); } } /** * 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['fids'] = $element['fids']['#value']; $field = field_widget_field($element, $form_state); $instance = field_widget_instance($element, $form_state); $element['#theme'] = 'file_widget'; // Add the display field if enabled. if (!empty($field['settings']['display_field']) && $item['fids']) { $element['display'] = array( '#type' => empty($item['fids']) ? '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['fids']) { $config = config('file.settings'); $element['description'] = array( '#type' => $config->get('description.type'), '#title' => t('Description'), '#value' => isset($item['description']) ? $item['description'] : '', '#maxlength' => $config->get('description.length'), '#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 = 'file/ajax/' . implode('/', $parents) . '/' . $form['form_build_id']['#value']; $field_element = NestedArray::getValue($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 file_field_widget_form() requires the entire field, // not just the individual item, to be valid. 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 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 { // The title needs to be assigned to the upload 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'] = '