Skip to content

Instantly share code, notes, and snippets.

@rgpublic
Last active December 11, 2020 11:02
Show Gist options
  • Save rgpublic/ddab5b2c763f30574c8a6d5eaa9cd339 to your computer and use it in GitHub Desktop.
Save rgpublic/ddab5b2c763f30574c8a6d5eaa9cd339 to your computer and use it in GitHub Desktop.
Drupal fix UID basefield
#!/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
Copy link

daFish commented Dec 10, 2020

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

@rgpublic
Copy link
Author

@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.

@daFish
Copy link

daFish commented Dec 11, 2020

@rgpublic: Yes, I misread that totally. 🤦 You are absolutely right.

@rgpublic
Copy link
Author

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