Drupal 8: Call to undefined method Drupal\Core\Session\AccountProxy::get() when using cv with a nonexistent --user specified
To reproduce:
- Install Drupal 8/9 + civi
- Call a
cv
command as a non-existent cms user, e.g.cv api --user=admimisrator job.fetch_activities
- Error
Call to undefined method Drupal\Core\Session\AccountProxy::get()
There's two places involved, and I'm not sure yet where the best place to fix it is. Maybe both of them.
Place | Words |
---|---|
CRM_Utils_System_Drupal8::getUserIDFromUserObject() | CRM_Core_BAO_UFMatch::synchronize() calls this indirectly from getBestUFID(). The getUserIDFromUserObject() function assumes that the parameter is a \Drupal\user\Entity\User , but in this case it isn't, hence it has no get() function. It's a \Drupal\Core\Session\AccountProxy which in this state represents an uninitialized user. |
cv src/Utils/BootTrait::_boot_full() | It calls \CRM_Core_Config::singleton()->userSystem->loadUser() but then ignores its return value. It appears all the CMSes return FALSE if it can't load, so it should probably check that before calling ensureUserContact(), which calls CRM_Core_BAO_UFMatch::synchronize(). |
In drupal 7 it's also not the intended error message, but you don't notice it's unintentional because at least the error there gives you a hint: Trying to get property 'uid' of non-object
. The intended error message appears to be Failed to determine contactID for user=admimisrator
, which is a better message, and then also it would avoid inadvertent consequences from trying to synchronize.