You are here

class NodeSubject in Changed Fields API 8.2

Same name and namespace in other branches
  1. 8 src/NodeSubject.php \Drupal\changed_fields\NodeSubject

Class NodeSubject.

Hierarchy

  • class \Drupal\changed_fields\NodeSubject implements \SplSubject

Expanded class hierarchy of NodeSubject

2 files declare their use of NodeSubject
changed_fields_basic_usage.module in examples/changed_fields_basic_usage/changed_fields_basic_usage.module
Contains changed_fields_basic_usage.module.
changed_fields_extended_field_comparator.module in examples/changed_fields_extended_field_comparator/changed_fields_extended_field_comparator.module
Contains changed_fields_extended_field_comparator.module.

File

src/NodeSubject.php, line 18
Contains NodeSubject.php.

Namespace

Drupal\changed_fields
View source
class NodeSubject implements SplSubject {

  /**
   * @var NodeInterface
   */
  private $node;

  /**
   * @var array
   */
  private $changedFields;

  /**
   * @var DefaultFieldComparator
   */
  private $fieldComparatorPlugin;

  /**
   * @var array
   */
  private $observers;

  /**
   * @param NodeInterface $node
   * @param string $fieldComparatorPluginId
   */
  public function __construct(NodeInterface $node, $fieldComparatorPluginId) {
    $this->node = $node;
    $this->changedFields = [];
    $this->fieldComparatorPlugin = \Drupal::service('plugin.manager.changed_fields.field_comparator')
      ->createInstance($fieldComparatorPluginId);
  }

  /**
   * {@inheritdoc}
   */
  public function attach(SplObserver $observer) {
    $this->observers[spl_object_hash($observer)] = $observer;
  }

  /**
   * {@inheritdoc}
   */
  public function detach(SplObserver $observer) {
    unset($this->observers[spl_object_hash($observer)]);
  }

  /**
   * {@inheritdoc}
   */
  public function notify() {
    foreach ($this->observers as $observer) {
      foreach ($observer
        ->getInfo() as $nodeType => $fields) {
        if (!$this->node
          ->isNew() && $this->node
          ->getType() == $nodeType) {
          $changedFields = [];
          foreach ($fields as $fieldName) {
            $oldValue = $this->node->original
              ->get($fieldName)
              ->getValue();
            $newValue = $this->node
              ->get($fieldName)
              ->getValue();
            $fieldDefinition = $this->node
              ->get($fieldName)
              ->getFieldDefinition();
            $result = $this->fieldComparatorPlugin
              ->compareFieldValues($fieldDefinition, $oldValue, $newValue);
            if (is_array($result)) {
              $changedFields[$fieldName] = $result;
            }
          }
          if (!empty($changedFields)) {
            $this->changedFields = $changedFields;
            $observer
              ->update($this);
          }
        }
      }
    }
  }

  /**
   * Returns node object.
   *
   * @return NodeInterface
   */
  public function getNode() {
    return $this->node;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
NodeSubject::$changedFields private property
NodeSubject::$fieldComparatorPlugin private property
NodeSubject::$node private property
NodeSubject::$observers private property
NodeSubject::attach public function
NodeSubject::detach public function
NodeSubject::getChangedFields public function Returns changed fields.
NodeSubject::getNode public function Returns node object.
NodeSubject::notify public function
NodeSubject::__construct public function