You are here

public function CFNodeSubject::notify in Changed Fields API 7.3

File

src/CFNodeSubject.php, line 59

Class

CFNodeSubject
Class CFNodeSubject.

Code

public function notify() {
  foreach ($this->observers as $observer) {
    foreach ($observer
      ->getInfo() as $nodeType => $fields) {
      if (isset($this->node->original) && $this->node->type == $nodeType) {
        $this->node->changed_fields = [];
        foreach ($fields as $fieldName) {
          if ($fieldName == 'title') {
            $oldValue = $this->node->original->{$fieldName};
            $newValue = $this->node->{$fieldName};
            $fieldInfo['field_base'] = [
              'type' => 'title',
            ];
          }
          else {
            $oldValue = field_get_items('node', $this->node->original, $fieldName);
            $newValue = field_get_items('node', $this->node, $fieldName);
            $fieldInfo['field_base'] = field_info_field($fieldName);
          }
          $fieldInfo['field_instance'] = field_info_instance('node', $fieldName, $nodeType);
          $result = $this->fieldComparator
            ->runFieldComparison($fieldInfo, $oldValue, $newValue);
          if (is_array($result)) {
            $this->node->changed_fields[$fieldName] = $result;
          }
        }
        if (!empty($this->node->changed_fields)) {
          $observer
            ->update($this);
        }
      }
    }
  }
}