You are here

public function EntityChangedFieldsTrait::checkUpdatedFields in Brightcove Video Connect 8

Same name and namespace in other branches
  1. 8.2 src/EntityChangedFieldsTrait.php \Drupal\brightcove\EntityChangedFieldsTrait::checkUpdatedFields()
  2. 3.x src/EntityChangedFieldsTrait.php \Drupal\brightcove\EntityChangedFieldsTrait::checkUpdatedFields()

Check for updated fields.

Ideally it should be called from the entity's preSave() method before the parent's preSave() call.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: Entity storage.

2 calls to EntityChangedFieldsTrait::checkUpdatedFields()
BrightcoveCmsEntity::preSave in src/Entity/BrightcoveCmsEntity.php
Acts on an entity before the presave hook is invoked.
BrightcoveTextTrack::preSave in src/Entity/BrightcoveTextTrack.php
Acts on an entity before the presave hook is invoked.

File

src/EntityChangedFieldsTrait.php, line 63

Class

EntityChangedFieldsTrait
Provides a trait to identify changed entity fields.

Namespace

Drupal\brightcove

Code

public function checkUpdatedFields(EntityStorageInterface $storage) {

  // Collect object getters.
  $methods = [];
  foreach (get_class_methods($this) as $key => $method) {

    // Create a matchable key for the get methods.
    if (preg_match('/^(?:get|is)[\\w\\d_]+$/i', $method)) {
      $methods[strtolower($method)] = $method;
    }
  }

  // Check fields if they were updated and mark them if changed.
  if (!empty($this
    ->id())) {

    /** @var \Drupal\brightcove\Entity\BrightcoveVideo $original_entity */
    $original_entity = $storage
      ->loadUnchanged($this
      ->id());
    if ($original_entity
      ->getChangedTime() != $this
      ->getChangedTime()) {

      /** @var \Drupal\Core\Field\FieldItemList $field */
      foreach ($this
        ->getFields() as $name => $field) {
        $getter = $this
          ->getGetterName($name, $methods);

        // If the getter is available for the field then compare the two
        // field and if changed mark it.
        if (!is_null($getter) && $this
          ->{$getter}() != $original_entity
          ->{$getter}()) {
          $this->changedFields[$name] = TRUE;
        }
      }
    }
  }
  else {
    foreach ($this
      ->getFields() as $name => $field) {
      if (!is_null($this
        ->getGetterName($name, $methods))) {
        $this->changedFields[$name] = TRUE;
      }
    }
  }
}