Last active
February 9, 2022 07:52
-
-
Save thsutton/6878688 to your computer and use it in GitHub Desktop.
Hold your hand through the process of converting an existing Drupal 7 file field to use the private file system. Update the $FIELD variable (and, if required, the path and arguments for drush) and do what it tells you to.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# This script will hold your hand during the process of converting an existing Drupal 7 file field from public to private. | |
# | |
# http://twitter.com/thsutton | |
# http://www.linkedin.com/pub/thomas-sutton/55/391/350 | |
# http://thomas-sutton.id.au/ | |
set -eu | |
# | |
# CONFIGURATION | |
# | |
# Field machine name | |
FIELD="field_file" | |
# Path to drush | |
DRUSH="drush" | |
# | |
# PROCESS | |
# | |
# Check that there *is* a private file system configured. | |
if $DRUSH vget file_private_path --exact >/dev/null 2>&1; then | |
# @todo Check that the directory exists, is writable, etc. | |
true | |
else | |
echo | |
echo "[error] Please configure a private file system directory." > /dev/stderr | |
echo | |
exit; | |
fi | |
# Drop temporary tables, if they exist. | |
$($DRUSH sql-connect) -BNe "DROP TABLE IF EXISTS temp_field_data_${FIELD};" | |
$($DRUSH sql-connect) -BNe "DROP TABLE IF EXISTS temp_field_revision_${FIELD};" | |
# Create temporary tables. | |
$($DRUSH sql-connect) -BNe "CREATE TABLE temp_field_data_${FIELD} LIKE field_data_${FIELD};" | |
$($DRUSH sql-connect) -BNe "CREATE TABLE temp_field_revision_${FIELD} LIKE field_revision_${FIELD};" | |
# Copy data into temporary tables. | |
$($DRUSH sql-connect) -BNe "INSERT INTO temp_field_data_${FIELD} SELECT * FROM field_data_${FIELD};" | |
$($DRUSH sql-connect) -BNe "INSERT INTO temp_field_revision_${FIELD} SELECT * FROM field_revision_${FIELD};" | |
# Truncate the data tables. | |
$($DRUSH sql-connect) -BNe "DELETE FROM field_data_${FIELD} WHERE 1=1;" | |
$($DRUSH sql-connect) -BNe "DELETE FROM field_revision_${FIELD} WHERE 1=1;" | |
# Download and install the module. | |
if $DRUSH pm-info filefield_paths 2>/dev/null | egrep '^\s+Status\s+:' | grep -qi enabled; then | |
echo "filefield_paths already installed" | |
else | |
$DRUSH dl filefield_paths | |
$DRUSH en filefield_paths | |
fi | |
# Tell the user to configure the field to use private file system. | |
echo ; echo ; echo | |
echo "GO AND UPDATE ${FIELD} TO USE PRIVATE FILE SYSTEM" | |
echo ; echo ; echo | |
echo "Change made?: [Yn]" | |
while read complete; do | |
case "$complete" in | |
"y"|"Y"|"yes") | |
echo 'Good!' | |
break | |
;; | |
*) | |
echo 'Well hurry up!' | |
;; | |
esac | |
done | |
# Copy data back to original tables. | |
$($DRUSH sql-connect) -BNe "INSERT INTO field_data_${FIELD} SELECT * FROM temp_field_data_${FIELD};" | |
$($DRUSH sql-connect) -BNe "INSERT INTO field_revision_${FIELD} SELECT * FROM temp_field_revision_${FIELD};" | |
# Drop the temporary tables. | |
$($DRUSH sql-connect) -BNe "DROP TABLE temp_field_data_${FIELD};" | |
$($DRUSH sql-connect) -BNe "DROP TABLE temp_field_revision_${FIELD};" | |
echo ; echo ; echo | |
echo "Now edit ${FIELD} again and tick the 'Retroactive update' checkbox in the 'FILE (FIELD) PATH SETTINGS' fieldset." | |
echo ; echo ; echo |
Hi @thsutton, thanks for your script! I used it together with https://www.drupal.org/project/privatize and put together a drush script to move files for a particular field without needing the filefield_paths module.
https://www.drupal.org/node/2059619#comment-9924902
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi thsutton,
many thanks for sharing this script with us, it works like a charm.