You are here

public function EntityUsage::registerUsage in Entity Usage 8.3

Same name and namespace in other branches
  1. 8.4 src/EntityUsage.php \Drupal\entity_usage\EntityUsage::registerUsage()
  2. 8.2 src/EntityUsage.php \Drupal\entity_usage\EntityUsage::registerUsage()

Register a new usage record.

Note that this method will honor the settings defined on the configuration page, hence potentially ignoring the register if the settings for the called combination are to not track this usage. Also, the hook hook_entity_usage_block_tracking() will be invoked, so other modules will have an opportunity to block this record before it is written to DB.

Parameters

int|string $target_id: The target entity ID.

string $target_type: The target entity type.

int|string $source_id: The source entity ID.

string $source_type: The source entity type.

string $source_langcode: The source entity language code.

string $source_vid: The source entity revision ID.

Overrides EntityUsageInterface::registerUsage

File

src/EntityUsage.php, line 81

Class

EntityUsage
Defines the entity usage base class.

Namespace

Drupal\entity_usage

Code

public function registerUsage($target_id, $target_type, $source_id, $source_type, $source_langcode, $source_vid) {

  // Check if target entity type is enabled, all entity types are enabled by
  // default.
  $enabled_target_entity_types = $this->config
    ->get('track_enabled_target_entity_types');
  if (is_array($enabled_target_entity_types) && !in_array($target_type, $enabled_target_entity_types, TRUE)) {
    return;
  }

  // Allow modules to block this operation.
  $context = [
    'target_id' => $target_id,
    'target_type' => $target_type,
    'source_id' => $source_id,
    'source_type' => $source_type,
    'source_langcode' => $source_langcode,
    'source_vid' => $source_vid,
  ];
  $abort = $this->moduleHandler
    ->invokeAll('entity_usage_block_tracking', $context);

  // If at least one module wants to block the tracking, bail out.
  if (in_array(TRUE, $abort, TRUE)) {
    return;
  }

  // Entities can have string IDs. We support that by using different columns
  // on each case.
  $target_id_column = $this
    ->isInt($target_id) ? 'target_id' : 'target_id_string';
  $source_id_column = $this
    ->isInt($source_id) ? 'source_id' : 'source_id_string';

  // Since this tries to blindly write a new record, do nothing if the record
  // already exists. We could probably avoid the try/catch if #2922755 lands.
  try {
    $this->connection
      ->insert($this->tableName)
      ->fields([
      $target_id_column => $target_id,
      'target_type' => $target_type,
      $source_id_column => $source_id,
      'source_type' => $source_type,
      'source_langcode' => $source_langcode,
      'source_vid' => $source_vid ?: 0,
    ])
      ->execute();
  } catch (IntegrityConstraintViolationException $e) {
    if (strpos($e
      ->getMessage(), 'Duplicate entry') !== FALSE) {
      return;
    }
    throw $e;
  }
  $event = new EntityUsageEvent($target_id, $target_type, $source_id, $source_type, $source_langcode, $source_vid);
  $this->eventDispatcher
    ->dispatch(Events::USAGE_REGISTER, $event);
}