You are here

class EntitySubject in Changed Fields API 8.3

Implements content entity subject.

Hierarchy

  • class \Drupal\changed_fields\EntitySubject implements \Drupal\changed_fields\SplSubject

Expanded class hierarchy of EntitySubject

2 files declare their use of EntitySubject
changed_fields_basic_usage.module in examples/changed_fields_basic_usage/changed_fields_basic_usage.module
This is the Changed Fields Basic Usage example module.
changed_fields_extended_field_comparator.module in examples/changed_fields_extended_field_comparator/changed_fields_extended_field_comparator.module
This is the Changed Fields Extended Field Comparator example module.

File

src/EntitySubject.php, line 10

Namespace

Drupal\changed_fields
View source
class EntitySubject implements \SplSubject {

  /**
   * Content entity to look at.
   *
   * @var Drupal\Core\Entity\ContentEntityInterface
   */
  protected $entity;

  /**
   * Map of names of changed fields with values.
   *
   * @var array
   */
  protected $changedFields;

  /**
   * Instance of field comparator plugin.
   *
   * @var Drupal\changed_fields\Plugin\FieldComparator\DefaultFieldComparator
   */
  protected $fieldComparatorPlugin;

  /**
   * Array of attached observers.
   *
   * @var array
   */
  protected $observers;

  /**
   * Constructs content entity subject.
   *
   * @param Drupal\Core\Entity\ContentEntityInterface $entity
   *   Content entity to look at.
   * @param string $field_comparator_plugin_id
   *   Field comparator plugin id.
   */
  public function __construct(ContentEntityInterface $entity, $field_comparator_plugin_id = 'default_field_comparator') {
    $this->entity = $entity;
    $this->changedFields = [];
    $this->fieldComparatorPlugin = \Drupal::service('plugin.manager.changed_fields.field_comparator')
      ->createInstance($field_comparator_plugin_id);
  }

  /**
   * {@inheritdoc}
   */
  public function attach(\SplObserver $observer) {
    if (!$observer instanceof ObserverInterface) {
      throw new \InvalidArgumentException('Observer must implement ObserverInterface interface.');
    }
    $this->observers[spl_object_hash($observer)] = $observer;
  }

  /**
   * {@inheritdoc}
   */
  public function detach(\SplObserver $observer) {
    if (!$observer instanceof ObserverInterface) {
      throw new \InvalidArgumentException('Observer must implement ObserverInterface interface.');
    }
    unset($this->observers[spl_object_hash($observer)]);
  }

  /**
   * {@inheritdoc}
   */
  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);
          }
        }
      }
    }
  }

  /**
   * Returns entity object.
   *
   * @return Drupal\Core\Entity\ContentEntityInterface
   *   Content entity to look at.
   */
  public function getEntity() {
    return $this->entity;
  }

  /**
   * Returns changed fields.
   *
   * @return array
   *   Map of names of changed fields with values.
   */
  public function getChangedFields() {
    return $this->changedFields;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntitySubject::$changedFields protected property Map of names of changed fields with values.
EntitySubject::$entity protected property Content entity to look at.
EntitySubject::$fieldComparatorPlugin protected property Instance of field comparator plugin.
EntitySubject::$observers protected property Array of attached observers.
EntitySubject::attach public function
EntitySubject::detach public function
EntitySubject::getChangedFields public function Returns changed fields.
EntitySubject::getEntity public function Returns entity object.
EntitySubject::notify public function
EntitySubject::__construct public function Constructs content entity subject.