You are here

function _tmgmt_extension_suit_get_job_item_hash in TMGMT Extension Suite 8.2

Same name and namespace in other branches
  1. 8.3 tmgmt_extension_suit.module \_tmgmt_extension_suit_get_job_item_hash()
  2. 8 tmgmt_extension_suit.module \_tmgmt_extension_suit_get_job_item_hash()

Calculates hash for a given job item depends on source data.

Parameters

\Drupal\tmgmt\JobItemInterface $job_item:

Return value

string

3 calls to _tmgmt_extension_suit_get_job_item_hash()
tmgmt_extension_suit_entity_update in ./tmgmt_extension_suit.module
Implements hook_entity_update().
tmgmt_extension_suit_tmgmt_job_item_presave in ./tmgmt_extension_suit.module
Implements hook_ENTITY_TYPE_presave().
tmgmt_extension_suit_update_8001 in ./tmgmt_extension_suit.install
Add missing job item hashes.

File

./tmgmt_extension_suit.module, line 111

Code

function _tmgmt_extension_suit_get_job_item_hash(JobItemInterface $job_item) {

  // Check if job exists at this moment. If we save job item into
  // cart it means that job doesn't exist for now and we can't get
  // source data for hash calculating.
  if ($job_item
    ->getJobId() == 0) {
    return '';
  }

  // Try to load the entity. If we deal with non-content types like strings,
  // we'll get an \Exception here and skip it for now.
  // @todo: Add support for strings
  try {
    $entity = Drupal::entityTypeManager()
      ->getStorage($job_item
      ->getItemType())
      ->load($job_item
      ->getItemId());
  } catch (\Exception $e) {
    return '';
  }
  if (!($entity instanceof ContentEntityInterface && $entity
    ->isTranslatable())) {
    return '';
  }
  $hash_data = Drupal::service('tmgmt.data')
    ->filterTranslatable($job_item
    ->getSourceData());
  return md5(json_encode($hash_data));
}