Skip to content

Relationship Statistics

CRM maintains per-contact counts of relationships by type in the relationship_statistics field on contacts. Statistics update when relationships are created, updated, or deleted without saving a new contact revision.

Service

Property Value
Service ID crm.relationship_statistics
Class Drupal\crm\Service\RelationshipStatisticsService
Interface Drupal\crm\Service\RelationshipStatisticsInterface

Key methods

  • increment(ContactInterface $contact, string $type_key) — increase count for a type
  • decrement(ContactInterface $contact, string $type_key) — decrease count for a type
  • recalculateAll(int $batch_size = 100) — rebuild all contact statistics

Statistics are written directly to crm_contact__relationship_statistics (and revision table when applicable), similar to comment statistics on nodes.

Hooks and maintenance

RelationshipHooks updates statistics on relationship insert, update, and delete. If counts drift after bulk imports or manual database changes, recalculate:

ddev drush crm:recalculate-statistics

Optional batch size:

ddev drush crm:recalculate-statistics --batch-size=50

Alias: crm-rs.

Views integration

  • Filter: RelationshipStatisticsType — filter contacts by relationship type counts
  • Field: RelationshipStatisticsFormatter — display statistics on contact views

See Views integration.

Field type

RelationshipStatisticsItem stores type key and count pairs on the contact entity. The computed data is exposed to Views through ContactViewsData.