diff --git a/file_entity/file_entity.admin.inc b/file_entity/file_entity.admin.inc
index 18ff8b5..0bf0cbd 100644
--- a/file_entity/file_entity.admin.inc
+++ b/file_entity/file_entity.admin.inc
@@ -175,3 +175,51 @@ function theme_file_entity_file_display_order($variables) {
return $output;
}
+
+/**
+ * Confirmation form for rebuliding the file_managed table to include type
+ * in rows where there is no type.
+ */
+function file_entity_admin_rebuild_types_form($form, &$form_state) {
+ $total = _file_entity_type_default_files_count();
+ if ($total == 0) {
+ drupal_goto('admin');
+ }
+ $form['total'] = array('#type' => 'value', '#value' => $total);
+ return confirm_form($form, 'Update types for existing files', 'admin/config/media', 'This process is useful when installing File Entity on an existing site. It will scan through existing files and identify the file type if not yet defined.
Update types for ' . $total . ' files?');
+}
+
+/**
+ * @see file_entity_admin_rebuild_types_form().
+ */
+function file_entity_admin_rebuild_types_form_submit(&$form, &$form_state) {
+ $total = $form_state['values']['total'];
+ $batch = array(
+ 'title' => t('Rebuilding type information for ' . $total . ' files'),
+ 'operations' => array(
+ array('file_entity_admin_rebuild_types_batch_op', array($total)),
+ ),
+ 'finished' => 'file_entity_admin_rebuild_types_batch_complete',
+ 'file' => drupal_get_path('module', 'file_entity') . '/file_entity.admin.inc',
+ );
+ batch_set($batch);
+}
+
+/**
+ * Batch operation for fixing the file_managed table, adding type values
+ * where the no specific type exists.
+ */
+function file_entity_admin_rebuild_types_batch_op($total, &$context) {
+ $per_run = variable_get('file_entity__type_batch_update_per_run', 100);
+ $context['results'] = array_merge($context['results'], file_entity_type_rebuild(FALSE, $per_run));
+ $context['finished'] = count($context['results']) / $total;
+}
+/**
+ * Sets a message informing the user how many file records were updated.
+ */
+function file_entity_admin_rebuild_types_batch_complete($success, $results, $operations) {
+ if ($success) {
+ $message = format_plural(count($results), 'One file identified and given a type.', '@count files identified and given a type.');
+ }
+ drupal_set_message($message);
+}
\ No newline at end of file
diff --git a/file_entity/file_entity.file_api.inc b/file_entity/file_entity.file_api.inc
index 81d8d4b..891d352 100644
--- a/file_entity/file_entity.file_api.inc
+++ b/file_entity/file_entity.file_api.inc
@@ -290,3 +290,59 @@ function _file_sort_array_by_weight(&$a) {
$a[$key]['weight'] = $original_weight[$key];
}
}
+
+
+/**
+ * Adds a value for the type column in files_managed if it is "default"
+ *
+ * If $update_existing is TRUE, will update the type of files with an existing type value.
+ *
+ * @param boolean $update_existing
+ * @param integer $no_to_update
+ * @param integer $offset
+ *
+ * @return array
+ * A list of updated file ids
+ */
+function file_entity_type_rebuild($update_existing = FALSE, $no_to_update = NULL, $offset = 0) {
+ $results = array();
+
+ $query = db_select('file_managed', 'fm')
+ ->fields('fm', array('fid'));
+
+ if (!$update_existing) {
+ $query->condition('type', 'default');
+ }
+
+ if ($no_to_update) {
+ $query->range($offset, $no_to_update);
+ }
+ elseif ($offset) {
+ $query->range($offset);
+ }
+
+ $fids = $query->execute()->fetchCol();
+ foreach ($fids as $fid) {
+ $file = file_load($fid);
+ if (!$file->fid) {
+ throw new Exception('Unable to continue, file was not loaded.');
+ }
+ file_save($file);
+ $results[] = $fid;
+ }
+
+ return $results;
+}
+
+
+/**
+ * Helper function to get number of files that have no type set.
+ */
+function _file_entity_type_default_files_count() {
+ return db_select('file_managed', 'fm')
+ ->condition('type', 'default')
+ ->countQuery()
+ ->execute()
+ ->fetchField();
+}
+
diff --git a/file_entity/file_entity.install b/file_entity/file_entity.install
index f2ce42b..6339d24 100644
--- a/file_entity/file_entity.install
+++ b/file_entity/file_entity.install
@@ -1,5 +1,4 @@
'The type of this file.',
'type' => 'varchar',
'length' => 50,
- 'not null' => FALSE,
+ 'not null' => TRUE,
+ 'default' => 'default',
);
$schema['file_managed']['indexes']['file_type'] = array('type');
}
diff --git a/file_entity/file_entity.module b/file_entity/file_entity.module
index bf6e5bb..13533b8 100644
--- a/file_entity/file_entity.module
+++ b/file_entity/file_entity.module
@@ -38,6 +38,16 @@ function file_entity_menu() {
'title' => 'Manage file types',
'description' => 'Manage files used on your site.',
);
+
+ // For managing different types of media and the fields associated with them.
+ $items['admin/config/media/file-types/rebuild'] = array(
+ 'title' => 'Rebuild type information for files',
+ 'description' => 'In case there are files in file_managed without a type, this function rebuilds them',
+ 'page callback' => 'drupal_get_form',
+ 'page arguments' => array('file_entity_admin_rebuild_types_form'),
+ 'access arguments' => array('administer media'),
+ 'file' => 'file_entity.admin.inc',
+ );
// Attach a "Manage file display" tab to each file type in the same way that
// Field UI attaches "Manage fields" and "Manage display" tabs. Note that
@@ -133,6 +143,17 @@ function file_entity_entity_info_alter(&$entity_info) {
}
/**
+ * Implements hook_file_type_info();
+ */
+function file_entity_file_type_info() {
+ return array(
+ 'default' => array(
+ 'label' => t('Default'),
+ ),
+ );
+}
+
+/**
* Implements hook_field_extra_fields().
*
* Adds 'file' as an extra field, so that its display and form component can be
@@ -168,7 +189,7 @@ function file_entity_field_extra_fields() {
*/
function file_entity_file_presave($file) {
// If the file doesn't already have a type, attempt to assign it one.
- if (!isset($file->type) && ($type = file_get_type($file))) {
+ if (!isset($file->type) || ($file->type == 'default') && ($type = file_get_type($file))) {
$file->type = $type;
}
@@ -371,4 +392,4 @@ function _file_entity_view_mode_menu_access($file_type, $view_mode, $access_call
elseif (function_exists($access_callback)) {
return call_user_func_array($access_callback, $args);
}
-}
+}
\ No newline at end of file
diff --git a/includes/media.admin.inc b/includes/media.admin.inc
index 6e1f2cf..594b810 100644
--- a/includes/media.admin.inc
+++ b/includes/media.admin.inc
@@ -566,57 +566,4 @@ function media_admin_config_browser_pre_submit(&$form, &$form_state) {
media_variable_del('dialog_theme');
unset($form_state['values'][media_variable_name('dialog_theme')]);
}
-}
-
-/**
- * Confirmation form for rebuliding the file_managed table to include type
- * in rows where there is no type.
- */
-function media_admin_rebuild_types_form($form, &$form_state) {
- $total = media_type_invalid_files_count();
- if ($total == 0) {
- media_variable_del('show_file_type_rebuild_nag');
- // @TODO: Make this not sound stupid.
- drupal_set_message('All files in the system have been assigned types. Media installation complete.');
- drupal_goto('admin');
- }
- $form['total'] = array('#type' => 'value', '#value' => $total);
- return confirm_form($form, 'Update types for existing files', 'admin/config/media', 'This process is required when installing media on an existing site. Media needs to scan through existing files and identify the file type.
Update types for ' . $total . ' files?');
-}
-
-/**
- * @see media_admin_rebuild_types_form().
- */
-function media_admin_rebuild_types_form_submit(&$form, &$form_state) {
- $total = $form_state['values']['total'];
-
- $batch = array(
- 'title' => t('Rebuilding type information for ' . $total . ' files'),
- 'operations' => array(
- array('media_admin_rebuild_types_batch_op', array($total)),
- ),
- 'finished' => 'media_admin_rebuild_types_batch_complete',
- 'file' => drupal_get_path('module', 'media') . '/includes/media.admin.inc',
- );
- batch_set($batch);
-}
-
-/**
- * Batch operation for fixing the file_managed table for media, adding type values
- * where no value exists.
- */
-function media_admin_rebuild_types_batch_op($total, &$context) {
- $per_run = media_variable_get('media_type_batch_update_per_run', 100);
- $context['results'] = array_merge($context['results'], media_type_batch_update(FALSE, $per_run));
- $context['finished'] = count($context['results']) / $total;
-}
-/**
- * Sets a message informing the user how many file records were updated.
- */
-function media_admin_rebuild_types_batch_complete($success, $results, $operations) {
- if ($success) {
- $message = format_plural(count($results), 'One file identified and given a type.', '@count files identified and given a type.');
- media_variable_del('show_file_type_rebuild_nag');
- }
- drupal_set_message($message);
}
\ No newline at end of file
diff --git a/includes/media.types.inc b/includes/media.types.inc
index 59344ff..a434b7b 100644
--- a/includes/media.types.inc
+++ b/includes/media.types.inc
@@ -211,56 +211,3 @@ function media_media_format_form_prepare_alter(&$form, &$form_state, $file) {
break;
}
}
-
-/**
- * Returns the number of files that need to be converted to media.
- */
-function media_type_invalid_files_count() {
- return db_select('file_managed', 'fm')
- ->condition('type', NULL)
- ->countQuery()
- ->execute()
- ->fetchField();
-}
-
-/**
- * Adds a value for the type column in files_managed.
- *
- * If $update_existing is TRUE, will update the type of files with an existing type value.
- *
- * @param boolean $update_existing
- * @param integer $no_to_update
- * @param integer $offset
- *
- * @return array
- * A list of updated file ids
- */
-function media_type_batch_update($update_existing = FALSE, $no_to_update = NULL, $offset = 0) {
- $results = array();
-
- $query = db_select('file_managed', 'fm')
- ->fields('fm', array('fid'));
-
- if (!$update_existing) {
- $query->condition('type', NULL);
- }
-
- if ($no_to_update) {
- $query->range($offset, $no_to_update);
- }
- elseif ($offset) {
- $query->range($offset);
- }
-
- $fids = $query->execute()->fetchCol();
- foreach ($fids as $fid) {
- $file = file_load($fid);
- if (!$file->fid) {
- throw new Exception('Unable to continue, file was not loaded.');
- }
- file_save($file);
- $results[] = $fid;
- }
-
- return $results;
-}
diff --git a/media.install b/media.install
index b43a84e..3c5dc0f 100644
--- a/media.install
+++ b/media.install
@@ -22,18 +22,6 @@ function media_enable() {
$types = array();
- // Define the default type to be used if no other type is found. Give it a
- // high weight to ensure it runs last.
- $types['default'] = new StdClass();
- $types['default']->name = 'default';
- $types['default']->label = "Other";
- $types['default']->base = TRUE;
- $types['default']->weight = 1000;
- $types['default']->type_callback_args = array(
- 'match_type' => 'any',
- 'mimetypes' => array('/.*/'),
- );
-
// Define the common media types: image, audio, and video.
$types['image'] = new StdClass();
$types['image']->name = 'image';
@@ -78,14 +66,14 @@ function media_enable() {
foreach ($roles as $rid => $role) {
user_role_grant_permissions($rid, array('view media'));
}
-
- // Updates the type field for the first MEDIA_UPDATE_RECORDS_ON_INSTALL files.
- $invalid_files = media_type_invalid_files_count();
+
+ // Updates the type field for the first file_entity_UPDATE_RECORDS_ON_INSTALL files.
+ $invalid_files = _file_entity_type_default_files_count();
if ($invalid_files <= MEDIA_UPDATE_RECORDS_ON_INSTALL) {
- media_type_batch_update(FALSE, MEDIA_UPDATE_RECORDS_ON_INSTALL);
+ file_entity_type_rebuild(FALSE, MEDIA_UPDATE_RECORDS_ON_INSTALL);
}
- $invalid_files = media_type_invalid_files_count();
+ $invalid_files = _file_entity_type_default_files_count();
if ($invalid_files > 0) {
// Not all files could be converted. Display a persistant nag message on
// every page for the administrator, urging them to finish the process.
diff --git a/media.module b/media.module
index 4428a72..c20b617 100644
--- a/media.module
+++ b/media.module
@@ -79,15 +79,6 @@ function media_menu() {
'access arguments' => array('administer media'),
'file' => 'includes/media.admin.inc',
);
- // For managing different types of media and the fields associated with them.
- $items['admin/config/media/rebuild_types'] = array(
- 'title' => 'Rebuild type information for media',
- 'description' => 'In case there are files in file_managed w/o a type, this function rebuilds them',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('media_admin_rebuild_types_form'),
- 'access arguments' => array('administer media'),
- 'file' => 'includes/media.admin.inc',
- );
// Settings used for determining the type of media a file is.
// @todo Find a new home for this that integrates with the file_entity module.
@@ -433,8 +424,17 @@ function media_page_alter(&$page) {
// Prevent form submissions from creating duplicate messages.
&& ($_SERVER['REQUEST_METHOD'] == 'GET')
// Show on all the admin pages, except the batch and the rebuild form.
- && path_is_admin(current_path()) && (arg(0) != 'batch') && (current_path() != 'admin/config/media/rebuild_types')) {
- drupal_set_message(t('Media module install is not complete. Finish the install.', array('@type_rebuild_link' => url('admin/config/media/rebuild_types'))), 'warning', FALSE);
+ && path_is_admin(current_path()) && (arg(0) != 'batch') && (current_path() != 'admin/config/media/file-types/rebuild')) {
+ // Check to see if we're already done. If so, delete the variable and move on
+ $total = _file_entity_type_default_files_count();
+ if ($total == 0) {
+ media_variable_del('show_file_type_rebuild_nag');
+ // @TODO: Make this not sound stupid.
+ drupal_set_message('All files in the system have been assigned types. Media installation complete.');
+ drupal_goto('admin');
+ }
+ // If it isn't finished, show the message.
+ drupal_set_message(t('Media module install is not complete. Finish the install.', array('@type_rebuild_link' => url('admin/config/media/file-types/rebuild'))), 'warning', FALSE);
}
if (isset($_GET['render']) && $_GET['render'] == 'media-popup') {