You are here

function tmgmt_extension_suit_entity_update in TMGMT Extension Suite 8

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

Implements hook_entity_update().

File

./tmgmt_extension_suit.module, line 131

Code

function tmgmt_extension_suit_entity_update(EntityInterface $entity) {
  if ($entity instanceof ContentEntityInterface && $entity
    ->isTranslatable() && Drupal::config('tmgmt_extension_suit.settings')
    ->get('do_track_changes')) {
    $entity = $entity
      ->getUntranslated();

    // Get job ids that contains current entity.
    $select = Drupal::database()
      ->select('tmgmt_job', 'tj');
    $select
      ->join('tmgmt_job_item', 'tji', 'tji.tjid = tj.tjid');
    $select
      ->addField('tj', 'tjid');
    $select
      ->condition('tj.state', [
      JobInterface::STATE_ACTIVE,
      JobInterface::STATE_REJECTED,
      JobInterface::STATE_FINISHED,
    ], 'IN');
    $select
      ->condition('tji.state', [
      JobItemInterface::STATE_ABORTED,
    ], 'NOT IN');
    $select
      ->condition('tji.item_type', $entity
      ->getEntityTypeId());
    $select
      ->condition('tji.item_id', $entity
      ->id());
    $job_ids = $select
      ->execute()
      ->fetchCol();

    // Set job state as active. Set all child job items state as active.
    // Reset all child data. Also we need to update job item hash because
    // source entity is updated.
    $upload_entities = [];
    foreach ($job_ids as $job_id) {
      if ($job = Job::load($job_id)) {
        $is_reopen_needed = FALSE;
        foreach ($job
          ->getItems() as $item) {
          $old_hash = $item
            ->get('tes_source_content_hash')
            ->getValue();
          $old_hash = isset($old_hash[0]['value']) ? $old_hash[0]['value'] : '';
          $hash = _tmgmt_extension_suit_get_job_item_hash($item);

          // Source entity is updated. Mark job item as active.
          if ($old_hash !== $hash) {

            // Reset an old data array with old entity field values.
            $item
              ->resetData();

            // Update current job item hash.
            $item
              ->set('tes_source_content_hash', $hash);

            // We don't use JobItem::setState() because it doesn't always call
            // JobItem::save(), and so the behaviour might be inconsistent.
            $item
              ->set('state', JobItemInterface::STATE_ACTIVE);
            $item
              ->save();
            $is_reopen_needed = TRUE;
          }
        }

        // Reopen parent job.
        if ($is_reopen_needed) {

          // Job::save() method invokes inside of Job::setState()
          // method. So there is no need to call save() directly.
          $job
            ->setState(JobInterface::STATE_ACTIVE);

          // Put job into upload queue.
          $upload_entities[] = $job
            ->id();
          Drupal::service('tmgmt_extension_suit.utils.queue_unique_item')
            ->addItem('tmgmt_extension_suit_upload', [
            'id' => (int) $job
              ->id(),
          ]);
          $translator_plugin = $job
            ->getTranslatorPlugin();
          if ($translator_plugin instanceof ExtendedTranslatorPluginInterface) {
            Drupal::getContainer()
              ->get('logger.channel.tmgmt_extension_suit')
              ->info(t('File upload queued (track entity changes). Job id: @job_id, file name: @name.', [
              '@name' => $translator_plugin
                ->getFileName($job),
              '@job_id' => $job
                ->id(),
            ]));
          }
        }
      }
    }
    if (!empty($upload_entities)) {
      \Drupal::moduleHandler()
        ->invokeAll('tmgmt_extension_suit_updated_entity_jobs', [
        $upload_entities,
      ]);
    }
  }
}