Ways for an extension to determine how (by which form/screen/API) a Contribution was created?
In hook_civicrm_pre, the params passed to your function include qfKey and entryURL &mdash; at least sometimes. The entry URL is in a format that's pretty obvious, but it seems like depending on URL parsing would make for brittle code. So what about qfKey?

qfKey is a form key based on form name, the current user session ID, a private key unique to the current user session, and optionally a sequence number to distinguish between multiple instances of the same form in the same session. The form name might give you the context information you need! But it is encrypted, so your extension can't just use a simple comparison operator on it.

To find out whether an entity was created in the context of a given form, you need to use CRM_Core_Key::validate(). For example:



function myextension_civicrm_pre($op, $objectName, $id, &amp;$params) {
  if (CRM_Core_Key::validate($params['qfKey'], 'CRM_Contribute_Form_Contribution', TRUE)) {
    // This entity is being created in the context of the back-office contribution form.
    // Do stuff.
  }
}