You are here

changed_fields_extended_field_comparator.module in Changed Fields API 8.3

This is the Changed Fields Extended Field Comparator example module.

File

examples/changed_fields_extended_field_comparator/changed_fields_extended_field_comparator.module
View source
<?php

/**
 * @file
 * This is the Changed Fields Extended Field Comparator example module.
 */
use Drupal\changed_fields\EntitySubject;
use Drupal\changed_fields_extended_field_comparator\ExtendedFieldComparatorObserver;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;

/**
 * Implements hook_entity_presave().
 */
function changed_fields_extended_field_comparator_entity_presave(EntityInterface $entity) {
  if ($entity instanceof ContentEntityInterface) {

    // Create EntitySubject object that will check entity fields
    // by DefaultFieldComparator.
    $entity_subject = new EntitySubject($entity, 'extended_field_comparator');

    // Add your observer object to EntitySubject.
    $entity_subject
      ->attach(new ExtendedFieldComparatorObserver());

    // Check if entity fields have been changed.
    $entity_subject
      ->notify();
  }
}