Drupal 7 file attachment issues after upgrade

Drupal

In Drupal 6 I used the public file method, combined with the Private Upload module, for which there is no Drupal 7 version. After the upgrade I wanted to use Drupal's private method. After the migration I entered the file locations under admin/config/media/file-system:

Upload destination public/private

Then I proceeded to admin/structure/types to enter the values for each data type. However, in the File attachments field settings section a warning was displayed: These settings apply to the File attachments field everywhere it is used. Because the field already has data, some settings can no longer be changed. The radio buttons for private/public were grayed out:

Upload destination public/private

That makes sense, since many attachments were present from D6. It doesn't mean that all private attachments from Drupal 6 were now public, but new attachments would have been. Here is what I did to work around that. It didn't make me proud but it was relatively quick and easy.

First I temporarily modified the Drupal core file modules/field_ui/field_ui.admin.inc and replaced the $has_data = field_has_data($field); on lines 1583 and 1911 with $has_data = false;, since this is what prevents the change. This enabled me to change the attachment field from public to private.

<?php
 
// See if data already exists for this field.
  // If so, prevent changes to the field settings.
 
$has_data = false; //field_has_data($field)
 
if ($has_data) {
   
$form['field']['#description'] = '<div class="messages error">' .
     
t('There is data for this field in the database. The field settings can
      no longer be changed.'
) . '</div>' . $form['field']['#description'];
  }
?>

(Modifying core files is considered bad practice but this was never intended as a permanent solution.)

After this, I was able to select private. Then I created a test post with an attachment to see how it would be entered in the file_managed table. It was entered as private://test.txt whereas Drupal 6 stored it as public://private/ (where private is the private path relative to the file directory).

Using phpMyAdmin I exported the table file_managed to a local file. Using a text editor I replaced all occurrences of public://private/ with private:// and zipped and saved the file.

Using phpMyAdmin I truncated the table file_managed. You can do this by entering the SQL query: truncate file_managed. Then I imported the saved file. Finally, I restored the file modules/field_ui/field_ui.admin.inc from the original Drupal 7 module.

Comments

file.field.inc line 65 $has_data =>false

Thanks!