+ if (!preg_match('/^civicrm_value_/', $query->entity_table)) {
+ // A normal attachment directly on its entity.
+ return array($query->is_valid, $query->entity_table, $query->entity_id);
+ }
+
+ // Ex: Translate custom-field table ("civicrm_value_foo_4") to
+ // entity table ("civicrm_activity").
+ $tblIdx = \CRM_Utils_Array::index(array('table_name'), $this->getCustomFields());
+ if (isset($tblIdx[$query->entity_table])) {
+ return array($query->is_valid, $tblIdx[$query->entity_table]['entity_table'], $query->entity_id);
+ }
+ throw new \Exception('Failed to lookup entity table for custom field.');