You are here

public function EntitySubject::notify in Changed Fields API 8.3

File

src/EntitySubject.php, line 79

Class

EntitySubject
Implements content entity subject.

Namespace

Drupal\changed_fields

Code

public function notify() {
  if ($this->entity
    ->isNew()) {
    return;
  }
  foreach ($this->observers as $observer) {
    foreach ($observer
      ->getInfo() as $entity_type => $entity_bundles) {
      if ($this->entity
        ->getEntityTypeId() != $entity_type) {
        continue;
      }
      foreach ($entity_bundles as $bundle => $fields) {
        if ($this->entity
          ->bundle() != $bundle) {
          continue;
        }
        $changed_fields = [];
        foreach ($fields as $field_name) {

          // TODO: what if observer subscribed to un-existing fields?
          $old_value = $this->entity->original
            ->get($field_name)
            ->getValue();
          $new_value = $this->entity
            ->get($field_name)
            ->getValue();
          $field_definition = $this->entity
            ->get($field_name)
            ->getFieldDefinition();
          $result = $this->fieldComparatorPlugin
            ->compareFieldValues($field_definition, $old_value, $new_value);
          if (is_array($result)) {
            $changed_fields[$field_name] = $result;
          }
        }
        if (!empty($changed_fields)) {
          $this->changedFields = $changed_fields;
          $observer
            ->update($this);
        }
      }
    }
  }
}