CorrespondingReference.php in Corresponding Entity References 8.4
File
src/Entity/CorrespondingReference.php
View source
<?php
namespace Drupal\cer\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\cer\CorrespondingReferenceOperations;
class CorrespondingReference extends ConfigEntityBase implements CorrespondingReferenceInterface {
public $id;
public $label;
public $first_field;
public $second_field;
public $bundles;
public $enabled;
public function id() {
return $this->id;
}
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
return $this;
}
public function getLabel() {
return $this->label;
}
public function setLabel($label) {
$this->label = $label;
return $this;
}
public function getFirstField() {
return $this->first_field;
}
public function setFirstField($firstField) {
$this->first_field = $firstField;
return $this;
}
public function getSecondField() {
return $this->second_field;
}
public function setSecondField($secondFIeld) {
$this->second_field = $secondFIeld;
return $this;
}
public function getBundles() {
return $this->bundles;
}
public function setBundles(array $bundles) {
$this->bundles = $bundles;
return $this;
}
public function isEnabled() {
return $this->enabled;
}
public function setEnabled($enabled) {
$this->enabled = $enabled;
return $this;
}
public function getCorrespondingFields() {
$first = $this
->getFirstField();
$second = $this
->getSecondField();
$correspondingFields = [];
if (!empty($first)) {
$correspondingFields[$first] = $first;
}
if (!empty($second)) {
$correspondingFields[$second] = $second;
}
return $correspondingFields;
}
public function hasCorrespondingFields(FieldableEntityInterface $entity) {
$hasCorrespondingFields = FALSE;
foreach ($this
->getCorrespondingFields() as $field) {
if ($entity
->hasField($field)) {
$hasCorrespondingFields = TRUE;
break;
}
}
return $hasCorrespondingFields;
}
public function synchronizeCorrespondingFields(FieldableEntityInterface $entity, $deleted = FALSE) {
if (!$this
->isValid($entity)) {
return;
}
foreach ($this
->getCorrespondingFields() as $fieldName) {
if (!$entity
->hasField($fieldName)) {
continue;
}
$differences = $this
->calculateDifferences($entity, $fieldName, $deleted);
$correspondingField = $this
->getCorrespondingField($fieldName);
\Drupal::moduleHandler()
->alter('cer_differences', $entity, $differences, $correspondingField);
foreach ($differences as $operation => $entities) {
foreach ($entities as $correspondingEntity) {
if ($correspondingEntity) {
$this
->synchronizeCorrespondingField($entity, $correspondingEntity, $correspondingField, $operation);
}
}
}
}
}
public function isValid(FieldableEntityInterface $entity) {
$bundles = $this
->getBundles();
$entityTypes = array_keys($bundles);
$entityType = $entity
->getEntityTypeId();
if (!in_array($entityType, $entityTypes)) {
return FALSE;
}
if (!in_array($entity
->bundle(), $bundles[$entityType]) && !in_array('*', $bundles[$entityType])) {
return FALSE;
}
if (!$this
->hasCorrespondingFields($entity)) {
return FALSE;
}
return TRUE;
}
public function getCorrespondingField($fieldName) {
$fields = $this
->getCorrespondingFields();
if (count($fields) == 1) {
return $fieldName;
}
unset($fields[$fieldName]);
return array_shift($fields);
}
public function synchronizeCorrespondingField(FieldableEntityInterface $entity, FieldableEntityInterface $correspondingEntity, $correspondingFieldName, $operation = NULL) {
if (is_null($operation)) {
$operation = CorrespondingReferenceOperations::ADD;
}
if (!$correspondingEntity
->hasField($correspondingFieldName)) {
return;
}
$field = $correspondingEntity
->get($correspondingFieldName);
$values = $field
->getValue();
$index = NULL;
foreach ($values as $idx => $value) {
if ($value['target_id'] == $entity
->id()) {
if ($operation == CorrespondingReferenceOperations::ADD) {
return;
}
$index = $idx;
}
}
$set = FALSE;
switch ($operation) {
case CorrespondingReferenceOperations::REMOVE:
if (!is_null($index)) {
unset($values[$index]);
$set = TRUE;
}
break;
case CorrespondingReferenceOperations::ADD:
$values[] = [
'target_id' => $entity
->id(),
];
$set = TRUE;
break;
}
if ($set) {
$field
->setValue($values);
$correspondingEntity
->save();
}
}
protected function calculateDifferences(FieldableEntityInterface $entity, $fieldName, $deleted = FALSE) {
$original = isset($entity->original) ? $entity->original : NULL;
$differences = [
CorrespondingReferenceOperations::ADD => [],
CorrespondingReferenceOperations::REMOVE => [],
];
if (!$entity
->hasField($fieldName)) {
return $differences;
}
$entityField = $entity
->get($fieldName);
if ($deleted) {
foreach ($entityField as $fieldItem) {
$differences[CorrespondingReferenceOperations::REMOVE][] = $fieldItem->entity;
}
return $differences;
}
if (empty($original)) {
foreach ($entityField as $fieldItem) {
$differences[CorrespondingReferenceOperations::ADD][] = $fieldItem->entity;
}
return $differences;
}
$originalField = $original
->get($fieldName);
foreach ($entityField as $fieldItem) {
if (!$this
->entityHasValue($original, $fieldName, $fieldItem->target_id)) {
$differences[CorrespondingReferenceOperations::ADD][] = $fieldItem->entity;
}
}
foreach ($originalField as $fieldItem) {
if (!$this
->entityHasValue($entity, $fieldName, $fieldItem->target_id)) {
$differences[CorrespondingReferenceOperations::REMOVE][] = $fieldItem->entity;
}
}
return $differences;
}
protected function entityHasValue(FieldableEntityInterface $entity, $fieldName, $id) {
if (!$entity
->hasField($fieldName)) {
return FALSE;
}
foreach ($entity
->get($fieldName) as $fieldItem) {
if ($fieldItem->target_id == $id) {
return TRUE;
}
}
return FALSE;
}
}