diff --git a/file_entity.admin.inc b/file_entity.admin.inc index 11dac93..828831f 100644 --- a/file_entity.admin.inc +++ b/file_entity.admin.inc @@ -169,7 +169,7 @@ function file_entity_admin_files_submit($form, &$form_state) { * Displays the file type admin overview page. */ function file_entity_list_types_page() { - $types = file_info_file_types(); + $types = file_type_get_types(); $entity_info = entity_get_info('file'); $field_ui = module_exists('field_ui'); $header = array( @@ -182,6 +182,7 @@ function file_entity_list_types_page() { $row = array(array('data' => theme('file_entity_file_type_overview', $info))); $path = isset($entity_info['bundles'][$type]['admin']['real path']) ? $entity_info['bundles'][$type]['admin']['real path'] : NULL; if ($field_ui) { + $row[] = array('data' => isset($path) ? l(t('edit file type'), $path . '/edit') : ''); $row[] = array('data' => isset($path) ? l(t('manage fields'), $path . '/fields') : ''); $row[] = array('data' => isset($path) ? l(t('manage display'), $path . '/display') : ''); } @@ -342,3 +343,85 @@ function theme_file_entity_file_display_order($variables) { return $output; } + +/** + * Admin screen for file type settings. + */ +function file_entity_file_type_form($form, &$form_state, $type) { + $type = file_type_get_type($type); + + $form['#file_type'] = $type; + + $form['label'] = array( + '#type' => 'textfield', + '#title' => t('Name'), + '#description' => t('This is the human readable name of the file type.'), + '#required' => TRUE, + '#default_value' => $type->label, + ); + $form['description'] = array( + '#type' => 'textarea', + '#title' => t('Description'), + '#description' => t('This is the description of the file type.'), + '#default_value' => $type->description, + ); + + $form['mimetypes'] = array( + '#type' => 'textarea', + '#title' => t('Mimetypes'), + '#description' => t('Enter one mimetype per line.'), + '#default_value' => implode("\n", $type->mimetypes), + ); + + include_once DRUPAL_ROOT . '/includes/file.mimetypes.inc'; + $mimetypes = file_mimetype_mapping(); + + $form['mimetype_mapping'] = array( + '#type' => 'fieldset', + '#title' => t('Mimetype mapping'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + $form['mimetype_mapping']['mapping'] = array( + '#theme' => 'item_list', + '#items' => $mimetypes['mimetypes'], + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Save'), + ); + + return $form; +} + +/** + * Validation handler for file type settings form. + */ +function file_entity_file_type_form_validate($form, &$form_state) { + $type = $form['#file_type']; + + $type->mimetypes = array_filter(array_map('trim', explode("\n", $form_state['values']['mimetypes']))); + + include_once DRUPAL_ROOT . '/includes/file.mimetypes.inc'; + $mimetypes = file_mimetype_mapping(); + + foreach ($type->mimetypes as $mimetype) { + if ($mimetype && !in_array($mimetype, $mimetypes['mimetypes'])) { + form_set_error('mimetypes', t('The mimetype %mimetype is not a valid mimetype.', array('%mimetype' => $mimetype))); + } + } +} + +/** + * Submit handler for file type settings form. + */ +function file_entity_file_type_form_submit($form, &$form_state) { + $type = $form['#file_type']; + + $type->label = $form_state['values']['label']; + $type->description = $form_state['values']['description']; + $type->mimetypes = array_filter(array_map('trim', explode("\n", $form_state['values']['mimetypes']))); + + file_type_save($type); +} diff --git a/file_entity.api.php b/file_entity.api.php index a7d9e8a..0f7bcf9 100644 --- a/file_entity.api.php +++ b/file_entity.api.php @@ -6,61 +6,35 @@ */ /** - * Define file types. + * Defines default file types. * - * @return - * An array whose keys are file type names and whose values are arrays - * describing the file type, with the following key/value pairs: - * - label: The human-readable name of the file type. - * - default view callback: (optional) The name of the function that returns a - * drupal_render() array for displaying the file. Used when there are no - * administrator configured file formatters, or none of the configured ones - * return a display. See hook_file_type_TYPE_default_view() for details. - * - description: (optional) A short description of the file type. - * - weight: (optional) A number defining the order in which the 'claim - * callback' function for this type is called relative to the claim - * callbacks of other defined types, when the type of a file needs to be - * determined. The type with the lowest weighted claim callback to return - * TRUE is assigned to the file. Also, on administrative pages listing file - * types, the types are ordered by weight. - * - admin: (optional) An array of information, to be added to the - * ['bundles'][TYPE]['admin'] entry for the 'file' entity type, thereby - * controlling the path at which Field UI pages are attached for this file - * type, and which users may access them. Defaults to attaching the Field UI - * pages to the admin/config/media/file-types/manage/TYPE path and requiring - * 'administer site configuration' permission. See hook_entity_info() for - * details about this array. This value can also be set to NULL to suppress - * Field UI pages from attaching at all for this file type. - * - * @see hook_file_type_info_alter() + * File types are implemented as CTools exportables, so modules can alter the + * defaults via hook_file_default_types_alter(), and the administrator can + * save overridden and custom types to the {file_type} database table. */ -function hook_file_type_info() { +function hook_file_default_types() { return array( - 'image' => array( + 'image' => (object) array( + 'api_version' => 1, + 'type' => 'image', 'label' => t('Image'), + 'description' => t("An Image is a two-dimensional picture that has a similar appearance to some subject, usually a physical object or a person."), + 'mimetypes' => array( + 'image/jpeg', + 'image/gif', + 'image/png', + ), ), ); } /** - * Perform alterations on file types. - * - * @param $info - * Array of information on file types exposed by hook_file_type_info() - * implementations. - */ -function hook_file_type_info_alter(&$info) { - // @todo Add example. -} - -/** - * @todo Add documentation. + * Performs alterations on default file types. * - * Note: This is not really a hook. The function name is manually specified via - * 'default view callback' in hook_file_type_info(), with this recommended - * callback name pattern. + * @see hook_file_default_types() */ -function hook_file_type_TYPE_default_view($file, $view_mode, $langcode) { +function hook_file_default_types_alter(&$types) { + $types['image']->mimetypes[] = 'image/svg+xml'; } /** diff --git a/file_entity.file_api.inc b/file_entity.file_api.inc index 428192b..ce048fa 100644 --- a/file_entity.file_api.inc +++ b/file_entity.file_api.inc @@ -6,61 +6,6 @@ */ /** - * Returns information about file types from hook_file_type_info(). - * - * @param $file_type - * (optional) A file type name. If ommitted, all file types will be returned. - * - * @return - * Either a file type description, as provided by hook_file_type_info(), or an - * array of all existing file types, keyed by file type name. - */ -function file_info_file_types($file_type = NULL) { - $info = &drupal_static(__FUNCTION__); - if (!isset($info)) { - $info = module_invoke_all('file_type_info'); - - // Add support for the standard file types until this can be fully - // abstracted out of Media module. - $info += array( - 'application' => array('label' => t('Application (multipurpose)')), - 'audio' => array('label' => t('Audio')), - 'image' => array('label' => t('Image')), - 'text' => array('label' => t('Text')), - 'video' => array('label' => t('Video')), - ); - - drupal_alter('file_type_info', $info); - uasort($info, '_file_entity_sort_weight_label'); - } - if ($file_type) { - if (isset($info[$file_type])) { - return $info[$file_type]; - } - } - else { - return $info; - } -} - -/** - * Determines the file type of a passed in file object. - * - * The file type is determined by extracting the 'first' part of the file's - * MIME type. For example, a PNG image with a MIME type of 'image/png' will - * have a file type of 'image'. - * - * @link http://www.iana.org/assignments/media-types/index.html IANA list of official MIME media types @endlink - */ -function file_get_type($file) { - // Ensure that a MIME type has been determined first. - if (empty($file->filemime)) { - $file->filemime = file_get_mimetype($file->uri); - } - return substr($file->filemime, 0, strpos($file->filemime, '/')); -} - -/** * Returns information about file formatters from hook_file_formatter_info(). * * @param $formatter_type @@ -90,10 +35,14 @@ function file_info_formatter_types($formatter_type = NULL) { } /** - * Clears the file info cache. + * Clears cached information about file types and file formatter types. */ function file_info_cache_clear() { - drupal_static_reset('file_info_file_types'); + // Clear the file type cache, managed by CTools. + ctools_include('export'); + ctools_export_load_object_reset('file_type'); + + // Clear the formatter type cache, managed by file_info_formatter_types(). drupal_static_reset('file_info_formatter_types'); } @@ -289,17 +238,8 @@ function file_view_file($file, $displays = 'full', $langcode = NULL) { } } - // If none of the configured formatters were able to display the file, attempt - // to display the file using the file type's default view callback. - if (!isset($element)) { - $file_type_info = file_info_file_types($file->type); - if (isset($file_type_info['default view callback']) && ($function = $file_type_info['default view callback']) && function_exists($function)) { - $element = $function($file, $view_mode, $langcode); - } - } - - // If a render element was returned by a formatter or the file type's default - // view callback, add some defaults to it and return it. + // If a render element was returned by a formatter, add some defaults to it + // and return it. if (isset($element)) { $element += array( '#file' => $file, @@ -311,6 +251,12 @@ function file_view_file($file, $displays = 'full', $langcode = NULL) { } /** + * @defgroup file_displays File displays API + * @{ + * Functions to load and save information about file displays. + */ + +/** * Returns an array of possible displays to use for a file type in a given view mode. * * It is common for a site to be configured with broadly defined file types @@ -413,6 +359,165 @@ function file_display_new($file_type, $view_mode, $formatter_name) { } /** + * @} End of "defgroup file_displays". + */ + +/** + * @defgroup file_types File types API + * @{ + * Functions to load and save information about file types. + */ + +/** + * Returns a list of all the available file types. + */ +function file_type_get_types($reset = FALSE) { + ctools_include('export'); + return ctools_export_crud_load_all('file_type', $reset); +} + +/** + * Returns the configuration object for the passed in file type. + */ +function file_type_get_type($type) { + ctools_include('export'); + return ctools_export_crud_load('file_type', $type); +} + +/** + * CTools exportables 'subrecords callback' to load {file_type} subrecords. + */ +function file_type_load_subrecords($types) { + foreach ($types as $type) { + $type->mimetypes = db_query('SELECT mimetype FROM {file_type_mimetypes} WHERE type = :type', array(':type' => $type->type))->fetchCol(); + } +} + +/** + * Updates an existing file type or creates a new one. + * + * This function can be called on its own, or via the CTools exportables + * 'save callback' for {file_type} objects. + */ +function file_type_save($type) { + // Get the old type object, so we know can issue the correct insert/update + // queries. + if (!empty($type->old_type) && $type->old_type != $type->type) { + $rename_bundle = TRUE; + $old_type = file_type_get_type($type->old_type); + } + else { + $rename_bundle = FALSE; + $old_type = file_type_get_type($type->type); + } + + // The type and label fields are required, but description is optional. + if (!isset($type->description)) { + $type->description = ''; + } + $fields = array( + 'type' => $type->type, + 'label' => $type->label, + 'description' => $type->description, + ); + + // Prepare the mimetype multiple insert query, but don't execute it until + // later in this function. + if (!empty($type->mimetypes)) { + $mimetype_insert = db_insert('file_type_mimetypes')->fields(array('type', 'mimetype')); + foreach ($type->mimetypes as $mimetype) { + $mimetype_insert->values(array('type' => $type->type, 'mimetype' => $mimetype)); + } + } + + // Update an existing type object, whether with a modified 'type' property or + // not. + if (isset($old_type)) { + if ($old_type->export_type & EXPORT_IN_DATABASE) { + db_update('file_type') + ->fields($fields) + ->condition('type', $old_type->type) + ->execute(); + db_delete('file_type_mimetypes') + ->condition('type', $old_type->type) + ->execute(); + } + else { + db_insert('file_type') + ->fields($fields) + ->execute(); + } + if (isset($mimetype_insert)) { + $mimetype_insert->execute(); + } + if ($rename_bundle) { + field_attach_rename_bundle('file', $old_type->type, $type->type); + } + module_invoke_all('file_type_update', $type); + $status = SAVED_UPDATED; + } + // Insert a new type object. + else { + db_insert('file_type') + ->fields($fields) + ->execute(); + if (isset($mimetype_insert)) { + $mimetype_insert->execute(); + } + field_attach_create_bundle('file', $type->type); + module_invoke_all('file_type_insert', $type); + $status = SAVED_NEW; + } + + // Clear the necessary caches. + file_info_cache_clear(); + + // Ensure the type has the correct export_type in case the $type parameter + // continues to be used by the calling function after this function completes. + if (empty($type->export_type)) { + $type->export_type = 0; + } + $type->export_type |= EXPORT_IN_DATABASE; + + return $status; +} + +/** + * Deletes a file type from the database. + * + * This function can be called on its own, or via the CTools exportables + * 'delete callback' for {file_type} objects. + * + * @param $type + * Either a loaded file type object or the machine-name of the type. + */ +function file_type_delete($type) { + $type_info = is_string($type) ? file_type_get_type($type) : $type; + $type = $type_info->type; + + db_delete('file_type') + ->condition('type', $type) + ->execute(); + db_delete('file_type_mimetypes') + ->condition('type', $type) + ->execute(); + + file_info_cache_clear(); + + // After deleting from the database, check if the type still exists as a + // code-provided default type. If not, consider the type fully deleted and + // invoke the needed hooks. + if (!file_type_get_type($type)) { + field_attach_delete_bundle('file', $type); + module_invoke_all('file_type_delete', $type_info); + } +} + +/** + * @} End of "defgroup file_types". + */ + +/** * Helper function to sort an array by the value of each item's 'weight' key, while preserving relative order of items that have equal weight. */ function _file_sort_array_by_weight(&$a) { diff --git a/file_entity.install b/file_entity.install index 3fe21a2..518b297 100644 --- a/file_entity.install +++ b/file_entity.install @@ -23,6 +23,10 @@ function file_entity_schema_alter(&$schema) { * Implements hook_schema(). */ function file_entity_schema() { + // Using an update helper function to track versions of these tables. + $schema['file_type'] = _file_entity_update_7105_schema_file_type(); + $schema['file_type_mimetypes'] = _file_entity_update_7105_schema_file_type_mimetypes(); + $schema['file_display'] = array( 'description' => 'Stores configuration options for file displays.', 'fields' => array( @@ -109,12 +113,6 @@ function file_entity_install() { db_add_field('file_managed', 'type', $spec, $indexes_new); } - // Update all files with empty types to use the first part of filemime. - db_update('file_managed') - ->expression('type', "SUBSTRING_INDEX(filemime, '/', 1)") - ->condition('type', '') - ->execute(); - // Set permissions. $roles = user_roles(); foreach ($roles as $rid => $role) { @@ -126,6 +124,9 @@ function file_entity_install() { * Implement hook_uninstall(). */ function file_entity_uninstall() { + foreach (file_type_get_types(TRUE) as $type) { + file_type_delete($type); + } db_drop_field('file_managed', 'type'); } @@ -246,3 +247,104 @@ function file_entity_update_7104() { } } } + +/** + * Add the {file_type} and {file_type_mimetypes} tables. + */ +function file_entity_update_7105() { + db_create_table('file_type', _file_entity_update_7105_schema_file_type()); + db_create_table('file_type_mimetypes', _file_entity_update_7105_schema_file_type_mimetypes()); +} + +/** + * {file_type} schema as of file_entity_update_7105(). + * + * If the {file_type} schema needs to change in the future, do not change this + * function. Instead, create a new function appropraite to the new update_N() + * implementation, and update file_entity_schema() to use it. + * + * @see http://drupal.org/node/150220 + */ +function _file_entity_update_7105_schema_file_type() { + return array( + 'description' => 'Stores the settings for file types.', + 'fields' => array( + 'type' => array( + 'description' => 'The machine name of the file type.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + ), + 'label' => array( + 'description' => 'The human readable name of the file type.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + 'translatable' => TRUE, + ), + 'description' => array( + 'description' => 'A brief description of this file type.', + 'type' => 'text', + 'not null' => TRUE, + 'default' => '', + 'size' => 'medium', + 'translatable' => TRUE, + ), + ), + 'primary key' => array('type'), + 'export' => array( + 'key' => 'type', + 'key name' => 'Type', + 'primary key' => 'type', + 'default hook' => 'file_default_types', + 'identifier' => 'file_type', + 'export type string' => 'ctools_type', + 'subrecords callback' => 'file_type_load_subrecords', + 'save callback' => 'file_type_save', + 'delete callback' => 'file_type_delete', + 'api' => array( + 'owner' => 'file_entity', + 'api' => 'file_type', + 'minimum_version' => 1, + 'current_version' => 1, + ), + ), + ); +} + +/** + * {file_type_mimetypes} schema as of file_entity_update_7105(). + * + * If the {file_type_mimetypes} schema needs to change in the future, do not + * change this function. Instead, create a new function appropraite to the new + * update_N() implementation, and update file_entity_schema() to use it. + * + * @see http://drupal.org/node/150220 + */ +function _file_entity_update_7105_schema_file_type_mimetypes() { + return array( + 'description' => 'Maps mimetypes to file types.', + 'fields' => array( + 'type' => array( + 'description' => 'The machine name of the file type.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + ), + 'mimetype' => array( + 'description' => 'Mimetypes mapped to this file type.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + ), + ), + 'indexes' => array( + array('file_type' => 'type'), + array('file_type_mimetype' => 'mimetype'), + ), + ); +} diff --git a/file_entity.module b/file_entity.module index cd35d9a..5bfd364 100644 --- a/file_entity.module +++ b/file_entity.module @@ -118,6 +118,15 @@ function file_entity_menu() { // router path). $file_type_argument = isset($bundle_info['admin']['bundle argument']) ? $bundle_info['admin']['bundle argument'] : $file_type; + // Add the 'File type settings' tab. + $items["$path/edit"] = array( + 'title' => 'Edit file type', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('file_entity_file_type_form', $file_type_argument), + 'type' => MENU_LOCAL_TASK, + 'file' => 'file_entity.admin.inc', + ) + $access; + // Add the 'Manage file display' tab. $items["$path/file-display"] = array( 'title' => 'Manage file display', @@ -189,6 +198,10 @@ function file_entity_theme() { 'render element' => 'elements', 'template' => 'file_entity', ), + 'file_entity_file_type_settings' => array( + 'variables' => array('type' => NULL), + 'file' => 'file_entity.admin.inc', + ), 'file_entity_file_type_overview' => array( 'variables' => array('label' => NULL, 'description' => NULL), 'file' => 'file_entity.admin.inc', @@ -206,9 +219,11 @@ function file_entity_theme() { /** * Implements hook_entity_info_alter(). * - * Extends the core file entity to be fieldable. Modules can define file types - * via hook_file_type_info(). For each defined type, create a bundle, so that - * fields can be configured per file type. + * Extends the core file entity to be fieldable. The file type is used as the + * bundle key. File types are implemented as CTools exportables, so modules can + * define default file types via hook_file_default_types(), and the + * administrator can override the default types or add custom ones via + * admin/config/media/file-types. */ function file_entity_entity_info_alter(&$entity_info) { $entity_info['file']['fieldable'] = TRUE; @@ -218,17 +233,15 @@ function file_entity_entity_info_alter(&$entity_info) { 'label' => t('Full'), 'custom settings' => FALSE, ); - foreach (file_info_file_types() as $type => $info) { - $info += array( - // Provide a default administration path for Field UI, but not if 'admin' - // has been explicitly set to NULL. + foreach (file_type_get_types() as $type) { + $entity_info['file']['bundles'][$type->type] = array( + 'label' => $type->label, 'admin' => array( 'path' => 'admin/config/media/file-types/manage/%', 'real path' => 'admin/config/media/file-types/manage/' . $type, 'bundle argument' => 5, ), ); - $entity_info['file']['bundles'][$type] = array_intersect_key($info, drupal_map_assoc(array('label', 'admin'))); $entity_info['file']['view callback'] = 'file_view_multiple'; } } @@ -273,9 +286,6 @@ function file_entity_file_presave($file) { $file->filemime = file_get_mimetype($file->uri); } - // Always update file type based on filemime. - $file->type = file_get_type($file); - field_attach_presave('file', $file); } @@ -725,6 +735,88 @@ function file_entity_get_hidden_stream_wrappers() { } /** + * Implements hook_ctools_plugin_api(). + */ +function file_entity_ctools_plugin_api($owner, $api) { + static $api_versions = array( + 'file_entity' => array( + 'file_type' => 1, + ), + ); + if (isset($api_versions[$owner][$api])) { + return array('version' => $api_versions[$owner][$api]); + } +} + +/** + * Implements hook_file_default_types(). + */ +function file_entity_file_default_types() { + $types = array(); + + // Image + $types['image'] = (object) array( + 'api_version' => 1, + 'type' => 'image', + 'label' => t('Image'), + 'description' => t("An Image is a two-dimensional picture that has a similar appearance to some subject, usually a physical object or a person."), + 'mimetypes' => array( + 'image/jpeg', + 'image/gif', + 'image/png', + ), + ); + + // Video + $types['video'] = (object) array( + 'api_version' => 1, + 'type' => 'video', + 'label' => t('Video'), + 'description' => t('Videos are a sequence of still images representing scenes in motion.'), + 'mimetypes' => array( + 'video/quicktime', + 'video/mp4', + 'video/x-msvideo', + 'video/ogg', + ), + ); + + // Audio + $types['audio'] = (object) array( + 'api_version' => 1, + 'type' => 'audio', + 'label' => t('Audio'), + 'description' => t('Audio files are an electrical representation of sound.'), + 'mimetypes' => array( + 'audio/mpeg', + 'audio/x-ms-wma', + 'audio/x-wav', + 'audio/ogg', + ), + ); + + // Document + $types['document'] = (object) array( + 'api_version' => 1, + 'type' => 'document', + 'label' => t('Document'), + 'description' => t('A Document is a work of writing intended to store and communicate information.'), + 'mimetypes' => array( + 'text/plain', + 'application/msword', + 'application/vnd.ms-excel', + 'application/pdf', + 'application/vnd.ms-powerpoint', + 'application/vnd.oasis.opendocument.text', + 'application/vnd.oasis.opendocument.spreadsheet', + 'application/vnd.oasis.opendocument.presentation', + ), + ); + + return $types; +} + +/** * A copy of theme_file_file_link() that makes the link point to file/[fid]. * * @see theme_file_file_link() diff --git a/file_entity.tokens.inc b/file_entity.tokens.inc index 4ace9c5..413f55a 100644 --- a/file_entity.tokens.inc +++ b/file_entity.tokens.inc @@ -66,16 +66,15 @@ function file_entity_tokens($type, $tokens, array $data = array(), array $option foreach ($tokens as $name => $original) { switch ($name) { case 'type': - if ($file_type = file_info_file_types($file->type)) { - $replacements[$original] = $sanitize ? check_plain($file_type['label']) : $file_type['label']; + if ($file_type = file_type_get_types($file->type)) { + $replacements[$original] = $sanitize ? check_plain($file_type->label) : $file_type->label; } break; } } // Chained token relationships. - if (($file_type_tokens = token_find_with_prefix($tokens, 'type')) && $file_type = file_info_file_types($file->type)) { - $file_type['type'] = $file->type; + if (($file_type_tokens = token_find_with_prefix($tokens, 'type')) && $file_type = file_type_get_types($file->type)) { $replacements += token_generate('file-type', $file_type_tokens, array('file_type' => $file_type), $options); } } @@ -87,21 +86,21 @@ function file_entity_tokens($type, $tokens, array $data = array(), array $option foreach ($tokens as $name => $original) { switch ($name) { case 'name': - $replacements[$original] = $sanitize ? check_plain($file_type['label']) : $file_type['label']; + $replacements[$original] = $sanitize ? check_plain($file_type->label) : $file_type->label; break; case 'machine-name': // This is a machine name so does not ever need to be sanitized. - $replacements[$original] = $file_type['type']; + $replacements[$original] = $file_type->type; break; case 'count': $query = db_select('file_managed'); - $query->condition('type', $file_type['type']); + $query->condition('type', $file_type->type); $query->addTag('file_type_file_count'); $count = $query->countQuery()->execute()->fetchField(); $replacements[$original] = (int) $count; break; case 'edit-url': - $replacements[$original] = url('admin/config/media/file-types/manage/' . $file_type['type'] . '/fields', $url_options); + $replacements[$original] = url('admin/config/media/file-types/manage/' . $file_type->type . '/fields', $url_options); break; } } diff --git a/tests/file_entity_test.module b/tests/file_entity_test.module index 7879703..c6b1dfd 100644 --- a/tests/file_entity_test.module +++ b/tests/file_entity_test.module @@ -5,96 +5,11 @@ * File Entity Test */ - /** * Implements hook_menu(). */ function file_entity_test_menu() { $items = array(); - $items['file-entity-test/file/add'] = array( - 'title' => 'Add file', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('file_entity_test_add_form'), - 'access arguments' => array('administer site configuration'), - 'file' => 'file_entity_test.pages.inc', - ); - $items['file-entity-test/file/%file'] = array( - 'title' => 'View file', - 'page callback' => 'file_entity_test_view_page', - 'page arguments' => array(2), - 'access arguments' => array('administer site configuration'), - 'file' => 'file_entity_test.pages.inc', - ); - $items['file-entity-test/file/%file/view'] = array( - 'title' => 'View', - 'type' => MENU_DEFAULT_LOCAL_TASK, - 'weight' => -10, - ); - $items['file-entity-test/file/%file/preview'] = array( - 'title' => 'Preview', - 'page callback' => 'file_entity_test_preview_page', - 'page arguments' => array(2), - 'access arguments' => array('administer site configuration'), - 'weight' => 0, - 'type' => MENU_LOCAL_TASK, - 'file' => 'file_entity_test.pages.inc', - ); - $items['file-entity-test/file/%file/edit'] = array( - 'title' => 'Edit', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('file_entity_test_edit_form', 2), - 'access arguments' => array('administer site configuration'), - 'weight' => 1, - 'type' => MENU_LOCAL_TASK, - 'file' => 'file_entity_test.pages.inc', - ); - return $items; } - -/** - * Implements hook_file_type_info(). - */ -function file_entity_test_file_type_info() { - return array( - 'file_entity_test' => array( - 'label' => t('Test'), - 'description' => t('A file type defined by the File Entity Test module. Used for testing only.'), - 'claim callback' => 'file_entity_test_file_type_file_entity_test_claim', - 'default view callback' => 'file_entity_test_file_type_file_entity_test_default_view', - 'weight' => 100, - ), - ); -} - -/** - * Implements hook_file_type_TYPE_claim(). - * - * Returns TRUE if the passed in file should be assigned the 'file_entity_test' - * file type. - */ -function file_entity_test_file_type_file_entity_test_claim($file) { - return TRUE; -} - -/** - * Implements hook_file_type_TYPE_default_view(). - */ -function file_entity_test_file_type_file_entity_test_default_view($file, $view_mode, $langcode) { - return array( - '#type' => 'link', - '#title' => $file->filename, - '#href' => file_create_url($file->uri), - ); -} - -/** - * Implements hook_entity_info_alter(). - */ -function file_entity_test_entity_info_alter(&$entity_info) { - $entity_info['file']['view modes']['file_entity_test_preview'] = array( - 'label' => t('Test Preview'), - 'custom settings' => TRUE, - ); -} diff --git a/tests/file_entity_test.pages.inc b/tests/file_entity_test.pages.inc index 9bea905..4b608f7 100644 --- a/tests/file_entity_test.pages.inc +++ b/tests/file_entity_test.pages.inc @@ -4,90 +4,3 @@ * @file * Test pages for the File Entity Test module. */ - -/** - * Form callback; upload a file. - */ -function file_entity_test_add_form($form, &$form_state) { - $form['file'] = array( - '#type' => 'managed_file', - '#required' => TRUE, - '#title' => 'File', - '#upload_location' => 'public://', - ); - $form['actions'] = array('#type' => 'actions'); - $form['actions']['submit'] = array( - '#type' => 'submit', - '#value' => t('Save'), - ); - - return $form; -} - -/** - * Form submit callback; save the uploaded file. - */ -function file_entity_test_add_form_submit($form, &$form_state) { - $file = file_load($form_state['values']['file']); - if (!$file->status) { - $file->status = FILE_STATUS_PERMANENT; - file_save($file); - } - drupal_set_message(t('Your file has been saved.')); - $form_state['redirect'] = 'file-entity-test/file/' . $file->fid; -} - -/** - * Page callback; view a file. - */ -function file_entity_test_view_page($file) { - return file_view($file, 'full'); -} - -/** - * Page callback; preview a file. - */ -function file_entity_test_preview_page($file) { - return file_view($file, 'file_entity_test_preview'); -} - -/** - * Form callback; edit a file. - */ -function file_entity_test_edit_form($form, &$form_state, $file) { - $form_state['file'] = $file; - field_attach_form('file', $file, $form, $form_state); - $form['file'] = file_view($file, 'file_entity_test_preview'); - - // Add internal file properties needed by - // file_entity_test_edit_form_validate(). - foreach (array('fid', 'type') as $key) { - $form[$key] = array('#type' => 'value', '#value' => $file->$key); - } - - $form['actions'] = array('#type' => 'actions'); - $form['actions']['submit'] = array( - '#type' => 'submit', - '#value' => t('Save'), - ); - - return $form; -} - -/** - * Form validation handler for the file edit form. - */ -function file_entity_test_edit_form_validate($form, &$form_state) { - entity_form_field_validate('file', $form, $form_state); -} - -/** - * Form submit handler for the file edit form - */ -function file_entity_test_edit_form_submit($form, &$form_state) { - $file = $form_state['file']; - entity_form_submit_build_entity('file', $file, $form, $form_state); - file_save($file); - drupal_set_message(t('Your changes to the file have been saved.')); - $form_state['redirect'] = 'file-entity-test/file/' . $file->fid; -}