Skip to content

Instantly share code, notes, and snippets.

@jpstacey
Created June 20, 2017 19:27
Show Gist options
  • Save jpstacey/b2250a1652e68800df84c4453b375883 to your computer and use it in GitHub Desktop.
Save jpstacey/b2250a1652e68800df84c4453b375883 to your computer and use it in GitHub Desktop.
Drush field clone equivalent for Drupal 8
#!/bin/bash
# @file
# Untested: use at your own risk!
# Entity type, bundle name, field name.
# Different entity types are currently unsupported (will always use "node").
IFS='.' read -r -a from <<< "$1"
IFS='.' read -r -a to <<< "$2"
# Support Linux & OSX < 10.11
temp_dir=`mktemp -d 2>/dev/null || mktemp -d -t 'drush-field-clone'`
# Export config.
drush config-export --destination=$temp_dir
# Copy storage first, if it doesn't exist.
storage_from=$temp_dir/field.storage.node.${from[2]}.yml
storage_to=$temp_dir/field.storage.node.${to[2]}.yml
if [ -e $storage_to ]; then
echo "$storage_to already exists; will ignore; continuing."
else
sed -e '/^uuid:.*/d' \
-e "s/^id: node.${from[2]}/id: node.${to[2]}/" \
-e "s/^field_name: ${from[2]}/field_name: ${to[2]}/" \
$storage_from > $storage_to
fi
# Copy field next, quitting if it already exists.
field_from=$temp_dir/field.field.node.${from[1]}.${from[2]}.yml
field_to=$temp_dir/field.field.node.${to[1]}.${to[2]}.yml
if [ -e $field_to ]; then
echo "$field_to already exists; cannot overwrite; exiting."
exit 1
else
sed -e '/^uuid:.*/d' \
-e "s/^id: node.${from[1]}.${from[2]}/id: node.${from[1]}.${to[2]}/" \
-e "s/^ - field.storage.node.${from[2]}/ - field.storage.node.${to[2]}/" \
-e "s/^field_name: ${from[2]}/field_name: ${to[2]}/" \
$field_from > $field_to
fi
# Import config and await user confirmation.
drush config-import --source=$temp_dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment