class NodeSubject in Changed Fields API 8
Same name and namespace in other branches
- 8.2 src/NodeSubject.php \Drupal\changed_fields\NodeSubject
Class NodeSubject.
Hierarchy
- class \Drupal\changed_fields\NodeSubject implements SubjectInterface
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'
1 service uses NodeSubject
File
- src/
NodeSubject.php, line 15 - Contains NodeSubject.php.
Namespace
Drupal\changed_fieldsView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
NodeSubject:: |
private | property | ||
NodeSubject:: |
private | property | ||
NodeSubject:: |
private | property | ||
NodeSubject:: |
private | property | ||
NodeSubject:: |
private | property | ||
NodeSubject:: |
private | property | ||
NodeSubject:: |
public | function |
Add observer to list. Overrides SubjectInterface:: |
|
NodeSubject:: |
public | function |
Notify all registered observers if needed. Overrides SubjectInterface:: |
|
NodeSubject:: |
public | function |
Returns changed fields. Overrides SubjectInterface:: |
|
NodeSubject:: |
public | function |
Returns node object. Overrides SubjectInterface:: |
|
NodeSubject:: |
public | function | ||
NodeSubject:: |
public | function |
Remove observer from list. Overrides SubjectInterface:: |
|
NodeSubject:: |
public | function |