You are here

class NodeSubject in Changed Fields API 8

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

Class NodeSubject.

Hierarchy

Expanded class hierarchy of NodeSubject

1 file declares its use of NodeSubject
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.
1 string reference to 'NodeSubject'
changed_fields.services.yml in ./changed_fields.services.yml
changed_fields.services.yml
1 service uses NodeSubject
changed_fields.node_subject in ./changed_fields.services.yml
Drupal\changed_fields\NodeSubject

File

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

Namespace

Drupal\changed_fields
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NodeSubject::$changedFields private property
NodeSubject::$fieldComparatorPlugin private property
NodeSubject::$info private property
NodeSubject::$node private property
NodeSubject::$observers private property
NodeSubject::$pluginManager private property
NodeSubject::addObserver public function Add observer to list. Overrides SubjectInterface::addObserver
NodeSubject::checkNodeFields public function Notify all registered observers if needed. Overrides SubjectInterface::checkNodeFields
NodeSubject::getChangedFields public function Returns changed fields. Overrides SubjectInterface::getChangedFields
NodeSubject::getNode public function Returns node object. Overrides SubjectInterface::getNode
NodeSubject::initFieldComparatorPlugin public function
NodeSubject::removeObserver public function Remove observer from list. Overrides SubjectInterface::removeObserver
NodeSubject::__construct public function