Last active
December 11, 2020 11:02
-
-
Save rgpublic/ddab5b2c763f30574c8a6d5eaa9cd339 to your computer and use it in GitHub Desktop.
Drupal fix UID basefield
This file contains 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/bash | |
list=`drush ev 'echo implode("\n", Drupal::service("config.factory")->listAll("core.base_field_override."))' | \ | |
grep -Po "([^\.]+\.[^\.]+)(?=\.uid$)"` | |
changed=false | |
while IFS= read -r item; do | |
parts=(${item//./ }) | |
type=${parts[0]}; | |
bundle=${parts[1]}; | |
entity_class=`drush ev "echo \Drupal::entityTypeManager()->getDefinition('$type')->getClass()"` | |
if [ "$entity_class" == "" ]; then | |
echo "Could not determine entity class for type '$type'"; | |
continue; | |
fi | |
key="core.base_field_override.$type.$bundle.uid" | |
current_value=`drush cget --format=string "$key" "default_value_callback" 2>/dev/null` | |
if [ "$current_value" == "$entity_class::getCurrentUserId" ]; then | |
drush cset -y "$key" "default_value_callback" "$entity_class::getDefaultEntityOwner" >/dev/null 2>&1 | |
echo "Content-type '$type', bundle '$bundle' has been fixed successfully."; | |
changed=true; | |
else | |
echo "Content-type '$type', bundle '$bundle' is already fixed."; | |
fi | |
done <<< "$list" | |
if [ "$changed" == true ]; then | |
echo "Clearing cache to apply changes..."; | |
drush cr | |
else | |
echo "Nothing has been changed."; | |
fi | |
echo "Finished :-)"; |
@daFish: No ::getCurrentUserId is the old, previous value. Perhaps you are confusing this with ::getDefaultEntityOwner. Or, put differently, I don't ask: Is the current value already the destination value? But instead I ask: Is the current value the expected source value? I did that to protect against inadvertently changing entities where the value is intentionally sth. totally different.
@rgpublic: Yes, I misread that totally. 🤦 You are absolutely right.
BTW: I've added automatic cache clearing to keep people from mistakenly thinking that the script didn't work. You need to clear the cache for the changes to have any effect.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is the comparison in line 23 correct? If both values are equal there is nothing to do so it should be
if [ "$current_value" != "$entity_class::getCurrentUserId" ]; then