diff --git a/src/RadioactivityReferenceUpdater.php b/src/RadioactivityReferenceUpdater.php index fefb63f..aa201f1 100644 --- a/src/RadioactivityReferenceUpdater.php +++ b/src/RadioactivityReferenceUpdater.php @@ -3,9 +3,11 @@ namespace Drupal\radioactivity; use Drupal\Component\Datetime\TimeInterface; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\TypedData\TranslatableInterface; use Drupal\radioactivity\Entity\Radioactivity; /** @@ -86,14 +88,45 @@ class RadioactivityReferenceUpdater implements RadioactivityReferenceUpdaterInte /** * {@inheritdoc} */ - public function updateReferenceFields(FieldableEntityInterface $entity): bool { + public function updateReferenceFields(ContentEntityInterface $entity): bool { + if (!$entity instanceof FieldableEntityInterface) { + return FALSE; + } + + if ($entity instanceof TranslatableInterface) { + $languages = $entity->getTranslationLanguages(); + $entityIsUpdated = FALSE; + + foreach ($languages as $language) { + $entityIsUpdated |= $this->doUpdateReferenceFields($entity->getTranslation($language->getId())); + } + } + else { + $entityIsUpdated = $this->doUpdateReferenceFields($entity); + } + + return $entityIsUpdated; + } + + /** + * Adds missing radioactivity entities to radioactivity reference fields. + * + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * The entity to use. + * + * @return bool + * True if the entity has been updated. + */ + protected function doUpdateReferenceFields(ContentEntityInterface $entity): bool { + $entityIsUpdated = FALSE; // Ignore entities that do not contain a radioactivity reference field. $fieldNames = $this->getReferenceFields($entity->getEntityTypeId(), $entity->bundle()); - $entityIsUpdated = FALSE; foreach ($fieldNames as $fieldName) { + /** @var \Drupal\radioactivity\RadioactivityReferenceFieldItemList $radioactiveReferenceField */ $radioactiveReferenceField = $entity->get($fieldName); + if ($radioactiveReferenceField->isEmpty()) { // Create a radioactivity entity as target. @@ -135,6 +168,7 @@ class RadioactivityReferenceUpdater implements RadioactivityReferenceUpdaterInte ->getKey('bundle'); $query = $entityStorage->getQuery(); + $query->accessCheck(FALSE); if ($bundleKey) { $query->condition($bundleKey, $bundle); } diff --git a/src/RadioactivityReferenceUpdaterInterface.php b/src/RadioactivityReferenceUpdaterInterface.php index bd65726..305e300 100644 --- a/src/RadioactivityReferenceUpdaterInterface.php +++ b/src/RadioactivityReferenceUpdaterInterface.php @@ -2,6 +2,7 @@ namespace Drupal\radioactivity; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\FieldableEntityInterface; /** @@ -39,12 +40,12 @@ interface RadioactivityReferenceUpdaterInterface { * * Note that the entity may be (re-)saved during this process. * - * @param \Drupal\Core\Entity\FieldableEntityInterface $entity + * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity to use. * * @return bool * True if the entity has been updated. */ - public function updateReferenceFields(FieldableEntityInterface $entity): bool; + public function updateReferenceFields(ContentEntityInterface $entity): bool; } diff --git a/tests/src/Unit/RadioactivityReferenceUpdaterTest.php b/tests/src/Unit/RadioactivityReferenceUpdaterTest.php index 4ad1780..aea9d98 100644 --- a/tests/src/Unit/RadioactivityReferenceUpdaterTest.php +++ b/tests/src/Unit/RadioactivityReferenceUpdaterTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\radioactivity\Unit; use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Core\Language\LanguageInterface; use Drupal\node\NodeInterface; use Drupal\radioactivity\RadioactivityReferenceUpdater; use Drupal\Tests\UnitTestCase; @@ -191,6 +192,7 @@ class RadioactivityReferenceUpdaterTest extends UnitTestCase { ]) ->disableOriginalConstructor() ->getMock(); + $fieldItemList = $this->createMock(FieldItemListInterface::class); $fieldItemList->expects($this->any()) ->method('isEmpty') @@ -202,7 +204,12 @@ class RadioactivityReferenceUpdaterTest extends UnitTestCase { $fieldItemList->expects($entityIsUpdated ? $this->once() : $this->never()) ->method('setValue'); + $language = $this->prophesize(LanguageInterface::class); + $language->getId()->willReturn($langcode); + $entity = $this->prophesize(NodeInterface::class); + $entity->getTranslationLanguages()->willReturn([$language]); + $entity->getTranslation($langcode)->willReturn($entity); $entity->getEntityTypeId()->willReturn('node'); $entity->bundle()->willReturn('article'); $entity->get('field_rar')->willReturn($fieldItemList);