You are here

class CFNodeSubject in Changed Fields API 7.2

Same name and namespace in other branches
  1. 7.3 src/CFNodeSubject.php \CFNodeSubject
  2. 7 includes/changed_fields.core.inc \CFNodeSubject

Class CFNodeSubject.

Hierarchy

Expanded class hierarchy of CFNodeSubject

File

includes/changed_fields.core.inc, line 107
File contains core classes and interfaces.

View source
class CFNodeSubject implements CFSubjectInterface {

  /**
   * Node object.
   *
   * @var \stdClass
   */
  private $node;

  /**
   * Info array.
   *
   * Array contains mapping for content types and fields to watch.
   *
   * @var array
   */
  private $info;

  /**
   * Field comparator object.
   *
   * @var CFDefaultFieldComparator
   */
  private $fieldComparator;

  /**
   * Array contains all registered observers.
   *
   * @var array
   */
  private $observers;

  /**
   * Node subject constructor.
   *
   * @param \stdClass $node
   *   Node object.
   * @param array $info
   *   Array contains mapping for content types and fields to watch.
   * @param CFDefaultFieldComparator $fieldComparator
   *   Field comparator object.
   */
  public function __construct(\stdClass $node, array $info, CFDefaultFieldComparator $fieldComparator) {
    $this->node = $node;
    $this->info = $info;
    $this->fieldComparator = $fieldComparator;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function checkNodeFields() {
    foreach ($this->info as $nodeType => $fields) {
      if (isset($this->node->original) && $this->node->type == $nodeType) {
        $this->node->changed_fields = array();
        foreach ($fields as $fieldName) {
          if ($fieldName == 'title') {
            $oldValue = $this->node->original->{$fieldName};
            $newValue = $this->node->{$fieldName};
            $fieldInfo['field_base'] = array(
              'type' => 'title',
            );
          }
          else {
            $oldValue = field_get_items('node', $this->node->original, $fieldName);
            $newValue = field_get_items('node', $this->node, $fieldName);
            $fieldInfo['field_base'] = field_info_field($fieldName);
          }
          $fieldInfo['field_instance'] = field_info_instance('node', $fieldName, $nodeType);
          $result = $this->fieldComparator
            ->runFieldComparison($fieldInfo, $oldValue, $newValue);
          if (is_array($result)) {
            $this->node->changed_fields[$fieldName] = $result;
          }
        }
        if (!empty($this->node->changed_fields)) {
          foreach ($this->observers as $observer) {
            $observer
              ->update($this);
          }
        }
      }
    }
  }

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

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

}

Members

Namesort descending Modifiers Type Description Overrides
CFNodeSubject::$fieldComparator private property Field comparator object.
CFNodeSubject::$info private property Info array.
CFNodeSubject::$node private property Node object.
CFNodeSubject::$observers private property Array contains all registered observers.
CFNodeSubject::addObserver public function Add observer to list. Overrides CFSubjectInterface::addObserver
CFNodeSubject::checkNodeFields public function Notify all registered observers if needed. Overrides CFSubjectInterface::checkNodeFields
CFNodeSubject::getChangedFields public function Returns changed fields. Overrides CFSubjectInterface::getChangedFields
CFNodeSubject::getNode public function Returns node object. Overrides CFSubjectInterface::getNode
CFNodeSubject::removeObserver public function Remove observer from list. Overrides CFSubjectInterface::removeObserver
CFNodeSubject::__construct public function Node subject constructor.