You are here

protected function PushIntent::getEntityChangedTime in CMS Content Sync 2.1.x

Same name and namespace in other branches
  1. 8 src/PushIntent.php \Drupal\cms_content_sync\PushIntent::getEntityChangedTime()
  2. 2.0.x src/PushIntent.php \Drupal\cms_content_sync\PushIntent::getEntityChangedTime()

Get the changed date of the entity. Not all entities provide the required attribute and even those aren't consistently saving it so this method takes care of these exceptions.

@todo Check if we should remove this as we're no longer using this changed date for deciding whether to push an entity or not (using hashes now).

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

Return value

int

1 call to PushIntent::getEntityChangedTime()
PushIntent::updateEntityStatusAfterSuccessfulPush in src/PushIntent.php

File

src/PushIntent.php, line 1067

Class

PushIntent
Class PushIntent.

Namespace

Drupal\cms_content_sync

Code

protected function getEntityChangedTime($entity) {
  $request_time = $this
    ->getRequestTime();
  $push = $request_time;
  if ($entity instanceof EntityChangedInterface) {
    $push = $entity
      ->getChangedTime();
    if ($entity instanceof TranslatableInterface) {
      foreach ($entity
        ->getTranslationLanguages(false) as $language) {
        $translation = $entity
          ->getTranslation($language
          ->getId());

        /**
         * @var \Drupal\Core\Entity\EntityChangedInterface $translation
         */
        if ($translation
          ->getChangedTime() > $push) {
          $push = $translation
            ->getChangedTime();
        }
      }
    }

    // Check if any bricks were updated during this request that this specific entity is referencing
    // Quick edit doesn't update the changed date of the node...... so we have to go and see manually if anything
    // changed by caching it....
    if ($push < $request_time && $this->isQuickEdited) {
      return $request_time;
    }
  }
  if (EntityHandlerPluginManager::isEntityTypeFieldable($entity
    ->getEntityTypeId())) {

    /**
     * @var \Drupal\Core\Entity\EntityFieldManager $entity_field_manager
     */
    $entity_field_manager = \Drupal::service('entity_field.manager');

    /**
     * @var \Drupal\Core\Field\FieldDefinitionInterface[] $fields
     */
    $fields = $entity_field_manager
      ->getFieldDefinitions($entity
      ->getEntityTypeId(), $entity
      ->bundle());

    // Elements that are inline edited in other forms like media elements edited within node forms don't get their
    // timestamp updated even though the file attributes may change like the focal point. So we are checking for file
    // reference fields and check if they were updated in the meantime.
    foreach ($fields as $key => $field) {
      if ('image' === $field
        ->getType()) {
        $data = $entity
          ->get($key)
          ->getValue();
        foreach ($data as $delta => $value) {
          if (empty($value['target_id'])) {
            continue;
          }
          $entityTypeManager = \Drupal::entityTypeManager();
          $storage = $entityTypeManager
            ->getStorage('file');
          $target_id = $value['target_id'];
          $reference = $storage
            ->load($target_id);
          if (!$reference) {
            continue;
          }
          $sub = $this
            ->getEntityChangedTime($reference);
          if ($sub > $push) {
            $push = $sub;
          }
        }
      }
    }
  }

  // File entities timestamp doesn't change when focal point is updated and crop entity doesn't provide a changed date.
  if ('file' === $entity
    ->getEntityTypeId()) {

    /**
     * @var \Drupal\file\FileInterface $entity
     */

    // Handle crop entities.
    $moduleHandler = \Drupal::service('module_handler');
    if ($moduleHandler
      ->moduleExists('crop') && $moduleHandler
      ->moduleExists('focal_point')) {
      if (Crop::cropExists($entity
        ->getFileUri(), 'focal_point')) {
        $crop = Crop::findCrop($entity
          ->getFileUri(), 'focal_point');
        if ($crop) {
          $info = EntityStatus::getInfoForEntity('file', $entity
            ->uuid(), $this->flow
            ->id(), $this->pool
            ->id());
          if ($info) {
            $position = $crop
              ->position();
            $last = $info
              ->getData('crop');
            if (empty($last) || $position['x'] !== $last['x'] || $position['y'] !== $last['y']) {
              $push = $this
                ->getRequestTime();
            }
          }
        }
      }
    }
  }
  return $push;
}