You are here

NodeSubject.php in Changed Fields API 8

Same filename and directory in other branches
  1. 8.2 src/NodeSubject.php

Contains NodeSubject.php.

File

src/NodeSubject.php
View source
<?php

/**
 * @file
 * Contains NodeSubject.php.
 */
namespace Drupal\changed_fields;

use Drupal\node\NodeInterface;

/**
 * Class NodeSubject.
 */
class NodeSubject implements SubjectInterface {

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

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

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

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

  /**
   * @var FieldComparatorPluginManager
   */
  private $pluginManager;

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

  /**
   * @param array $info
   * @param FieldComparatorPluginManager $pluginManager
   */
  public function __construct(array $info, FieldComparatorPluginManager $pluginManager) {
    $this->info = $info;
    $this->pluginManager = $pluginManager;
    $this->changedFields = array();
  }

  /**
   * {@inheritdoc}
   */
  public function addObserver(ObserverInterface $observer) {
    $this->observers[$observer
      ->getId()] = $observer;
  }

  /**
   * {@inheritdoc}
   */
  public function removeObserver(ObserverInterface $observer) {
    if (isset($this->observers[$observer
      ->getId()])) {
      unset($this->observers[$observer
        ->getId()]);
    }
  }

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

  /**
   * @param string $fieldComparatorPluginId
   */
  public function initFieldComparatorPlugin($fieldComparatorPluginId) {
    $this->fieldComparatorPlugin = $this->pluginManager
      ->createInstance($fieldComparatorPluginId);
  }

  /**
   * {@inheritdoc}
   */
  public function getNode() {
    return $this->node;
  }

  /**
   * {@inheritdoc}
   */
  public function getChangedFields() {
    return $this->changedFields;
  }

}

Classes

Namesort descending Description
NodeSubject Class NodeSubject.