Developer information

Last updated on
2 April 2018

Interacting with the API

If you don't want to have local tasks and the default usage list page available to your users, you can consume the information registered by this module programmatically as well.

To do so, you only need to refer to the helper methods:

\Drupal::service('entity_usage.usage')->listSources($entity);

and 

\Drupal::service('entity_usage.usage')->listTargets($entity);

to retrieve the data registered by this module in your code.

Blocking a usage from being tracked

If you have a very specific use case that is not covered by the options that can be configured in the settings page, you can implement the hook:

hook_entity_usage_block_tracking()

to block a specific usage from being tracked. For instance, this would allow you to let all Nodes to be tracked as source, except for those of type "Article". More information about this hook can be found on its documentation.

Writing your own tracking methods

Tracking methods are plugins, so if you want to provide your own additional tracking system, you only need to implement a plugin of type @EntityUsageTrack, which will be used on all CRUD entity operations.

In the 2.x branch the heavy work for the plugin behavior is now handled by the base class, and plugin implementations now only need to:

  1. Extend \Drupal\entity_usage\EntityUsageTrackBase
  2. Define which field types they are able to track on their annotation
  3. Implement the method  \Drupal\entity_usage\EntityUsageTrackInterface::getTargetEntities(), which should retrieve the target entity identifyer from a FieldItem value.

Examples on how to implement this plugin type can be found in src/Plugin/EntityUsage/Track

Help improve this page

Page status: No known problems

You can: