Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By funkytraffic on
After upgrading from drupal 6 to 7 I want to turn a public file field into a private. Since it contains data the system does not allow this.
If I go to field settings it says: "There is data for this field in the database. The field settings can no longer be changed."
Is there a way to do this?
Comments
I think that this is a
I think that this is a legitimate question.
With an image field which already had numerous entries (data uploaded through it) I was able to change from public to private...
One can also understand if the files themselves don't move and some database search-and-replace is needed for previous uploads, but it would be nice if there was a workaround for making public filefields private.
Subscribe! Have an urgent
Subscribe!
Have an urgent need to convert public file field to private. Is there any solution?
yeah wondering the same,
yeah wondering the same, "private / public" option is disabled for the field .. how to move from one to another?
ok, nevermind, figured it
ok, nevermind, figured it out.
just in case someone is looking to do this, here are the steps:
1. backup your site and db
2. copy the table for your field to make a backup (in my case i copied: field_data_field_attachments to field_data_field_attachments_bk)
3. empty table field_data_field_attachments
4. go to drupal and the option to change the file system should be available, so change it
5. copy all data from field_data_field_attachments_bk back to field_data_field_attachments
6. go to drupal, select your content type, select your file field, in file path settings select retroactive update
7. click save and it will move all your files to the right place in file system and update your db to the new path
done.
I belive apart from the
I belive apart from the respective data table we need to also take a backup for the revision table in the above mention case field_revision_field_attachments
Need Drupal help?
Reach me
Backend Frontend and DevOps.
Required module
Excellent advice. Make sure you have http://drupal.org/project/filefield_paths installed.
Additional Details
Incorporating the various bits of knowledge in this thread with some knowhow, here's my recipe for making this happen:
1. make a back up of the file field data tables
drush sqlq "CREATE TABLE field_data_field_[FIELD NAME]_bak AS SELECT * FROM field_data_field_[FIELD NAME];"
drush sqlq "CREATE TABLE field_revision_field_[FIELD NAME]_bak AS SELECT * FROM field_revision_field_[FIELD NAME];"
2. empty file field data tables
drush sqlq "TRUNCATE TABLE field_data_field_[FIELD NAME];"
drush sqlq "TRUNCATE TABLE field_revision_field_[FIELD NAME];"
3. in the file field settings in drupal the option to change that field to private is now available; do so and save the field
4. copy all data from the temp backup tables back in to the real tables
drush sqlq "INSERT INTO field_data_field_[FIELD NAME] SELECT * FROM field_data_field_[FIELD NAME]_bak;"
drush sqlq "INSERT INTO field_revision_field_[FIELD NAME] SELECT * FROM field_revision_field_[FIELD NAME]_bak;"
5. enable the filefield_paths module (if it’s not already)
drush en -y filefield_paths
6. in the “FILE (FIELD) PATH SETTINGS” area of the file field settings in drupal, select the “Retroactive update” option and save; it will move all the files to the right place in file system and update the db to the new path (which may take a little while)
7. delete the temp storage tables
drush sqlq "DROP TABLE field_data_field_[FIELD NAME]_bak;"
drush sqlq "DROP TABLE field_revision_field_[FIELD NAME]_bak;"
Emanaton thanks for sharing
I switched my datatables path on Drupal commons 7 site back to public as d7 commons wasn't allow members of a community to view the downloads. I ran your code but my links are still showing system/files. I tried to set the path with the following but it seems D7 doesn't use 'files' table.
UPDATE files SET filepath = REPLACE(filepath,'private/','/sites/default/files');
Can you assist. Much appreciated.
Emanaton thanks for sharing
I switched my datatables path on Drupal commons 7 site back to public as d7 commons wasn't allow members of a community to view the downloads. I ran your code but my links are still showing system/files. I tried to set the path with the following but it seems D7 doesn't use 'files' table.
UPDATE files SET filepath = REPLACE(filepath,'private/','/sites/default/files');
Can you assist. Much appreciated.
Here's a small script that I
Here's a small script that I wrote to help automate the process (useful if you have to change more than one field)
Alternatively, here's the GitHub gist for the script.
I am assuming this requires
I am assuming this requires the File Field Paths module for the "Retroactive Upgrade" option?
Metatag should be in core.
Same issue but different
In Drupal 6 I was too inpatient to wait for the Drupal 7's feature of both public and private. I used the .htaccess to rewrite queries to system/files. This way I could determine which folders need to be private.
But how do you migrate that to Drupal 7? It took me the following steps:
drush vset file_default_scheme "private"
drush sqlq "UPDATE variable f, variable t SET t.value=f.value WHERE f.name='file_public_path' AND t.name='file_private_path' "
drush sqlq "UPDATE file_managed SET uri=REPLACE(uri,'public://','private://') where uri LIKE 'public://%'
/admin/structure/types/manage/<your-content-type>/fields/<your-attachment-field>/field-settings</your-attachment-field></your-content-type>
SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006 Options None Options +FollowSymLinks <ifmodule mod_rewrite.c=""> RewriteEngine On RewriteBase / RewriteRule ^(.*)$ index.php?q=system/files/$1 [L] </ifmodule>
<ifmodule mod_rewrite.c=""> RewriteEngine off </ifmodule>
+1
Many thanks for this procedure....
... and you do need to flip both file data and file revision tables.
For clarity, the retrospective update option is a checkbox in the main edit page for the field, within a File (Field) Paths collapsible container.
R.
Renaming the URLs of files which have been moved to priv folder
So the problem is that we are able to move all the files to the private folder but all the previous nodes still contain the old URLs of those files.
Is there a method/module which can enable us to change those URLs?
Thx for any response.
If I understand it correctly,
If I understand it correctly, the approach with the filefield_paths module stated above doesn't work at the moment until the following issue gets fixed.
https://www.drupal.org/node/2895035
Filefield paths seems to work
I tried the approach described above and the retroactive update worked. I used Filefield Paths 7.x-1.0. The bug report you linked appears to refer to the dev version, which is newer.
works like a charm
highly recommended
Solution for drupal 8
Solution for drupal 8
$storage = \Drupal\field\Entity\FieldStorageConfig::loadByName('(Type OF Entity)', '(Name Of field)');
$storage->setSetting('uri_scheme', 'private');
$storage->save();
* Type Of entity :- media, paragraph, file etc
* Name Of field :- field_media_image etc
Hi, i´m new at this and got
Hi, i´m new at this and got the same problem... how or where do i write that? settings.php?