You are here

changed_fields_basic_usage.module in Changed Fields API 8.3

This is the Changed Fields Basic Usage example module.

File

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

/**
 * @file
 * This is the Changed Fields Basic Usage example module.
 */
use Drupal\changed_fields\EntitySubject;
use Drupal\changed_fields_basic_usage\BasicUsageObserver;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;

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

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

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

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

Functions