EntitySubject.php in Changed Fields API 8.3
File
src/EntitySubject.php
View source
<?php
namespace Drupal\changed_fields;
use Drupal\Core\Entity\ContentEntityInterface;
class EntitySubject implements \SplSubject {
protected $entity;
protected $changedFields;
protected $fieldComparatorPlugin;
protected $observers;
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);
}
public function attach(\SplObserver $observer) {
if (!$observer instanceof ObserverInterface) {
throw new \InvalidArgumentException('Observer must implement ObserverInterface interface.');
}
$this->observers[spl_object_hash($observer)] = $observer;
}
public function detach(\SplObserver $observer) {
if (!$observer instanceof ObserverInterface) {
throw new \InvalidArgumentException('Observer must implement ObserverInterface interface.');
}
unset($this->observers[spl_object_hash($observer)]);
}
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) {
$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);
}
}
}
}
}
public function getEntity() {
return $this->entity;
}
public function getChangedFields() {
return $this->changedFields;
}
}