You are here

public function EntityUsage::registerUsage in Entity Usage 8.4

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

Register or update a usage record.

If called with $count >= 1, the record matching the other parameters will be updated (or created if it doesn't exist). If called with $count <= 0, the record will be deleted.

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.

string $method: The method used to relate source entity with the target entity. Normally the plugin id.

string $field_name: The name of the field in the source entity using the target entity.

int $count: (optional) The number of references to add to the object. Defaults to 1.

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, $method, $field_name, $count = 1) {

  // 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,
    'method' => $method,
    'field_name' => $field_name,
    'count' => $count,
  ];
  $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';

  // If $count is 0, we want to delete the record.
  if ($count <= 0) {
    $this->connection
      ->delete($this->tableName)
      ->condition($target_id_column, $target_id)
      ->condition('target_type', $target_type)
      ->condition($source_id_column, $source_id)
      ->condition('source_type', $source_type)
      ->condition('source_langcode', $source_langcode)
      ->condition('source_vid', $source_vid)
      ->condition('method', $method)
      ->condition('field_name', $field_name)
      ->execute();
  }
  else {
    $this->connection
      ->merge($this->tableName)
      ->keys([
      $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,
      'method' => $method,
      'field_name' => $field_name,
    ])
      ->fields([
      'count' => $count,
    ])
      ->execute();
  }
  $event = new EntityUsageEvent($target_id, $target_type, $source_id, $source_type, $source_langcode, $source_vid, $method, $field_name, $count);
  $this->eventDispatcher
    ->dispatch(Events::USAGE_REGISTER, $event);
}