TaxonomyTermMatch.php in Acquia Content Hub 8.2
File
modules/acquia_contenthub_subscriber/src/EventSubscriber/LoadLocalEntity/TaxonomyTermMatch.php
View source
<?php
namespace Drupal\acquia_contenthub_subscriber\EventSubscriber\LoadLocalEntity;
use Acquia\ContentHubClient\CDF\CDFObject;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\LoadLocalEntityEvent;
use Drupal\Component\Uuid\Uuid;
use Drupal\Core\Entity\EntityInterface;
use Drupal\depcalc\DependencyStack;
use Drupal\depcalc\DependentEntityWrapper;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class TaxonomyTermMatch implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
AcquiaContentHubEvents::LOAD_LOCAL_ENTITY => [
[
'onLoadLocalEntity',
7,
],
],
];
}
public function onLoadLocalEntity(LoadLocalEntityEvent $event) {
$object = $event
->getCdf();
if (!$this
->isSupported($object)) {
return;
}
if ($event
->getStack()
->hasDependency($object
->getUuid())) {
return;
}
$vocabulary_name = $this
->getVocabularyName($object);
$storage = $this
->getVocabularyStorage();
if (!($vocabulary = $storage
->load($vocabulary_name))) {
return;
}
$parents = $this
->extractParentAttribute($object);
$label = $this
->getTermLabel($object);
foreach ($parents as $parent) {
$term = $this
->findTaxonomyTerm($label, $vocabulary_name, $parent, $event
->getStack());
if (empty($term)) {
continue;
}
$this
->addDependency($event, $object, $term);
$event
->setEntity($term);
}
}
protected function isSupported(CDFObject $cdf_object) : bool {
$type = $cdf_object
->getAttribute('entity_type');
return $type
->getValue()[CDFObject::LANGUAGE_UNDETERMINED] === 'taxonomy_term';
}
protected function getTermLabel(CDFObject $cdf_object) : ?string {
$term_language = $cdf_object
->getMetadata()['default_language'];
$label = $cdf_object
->getAttribute('label')
->getValue();
return $label[$term_language] ?? $label[CDFObject::LANGUAGE_UNDETERMINED] ?? NULL;
}
protected function getVocabularyName(CDFObject $cdf_object) : ?string {
$bundle = $cdf_object
->getAttribute('bundle')
->getValue();
return $bundle[CDFObject::LANGUAGE_UNDETERMINED] ?? NULL;
}
protected function extractParentAttribute(CDFObject $object) {
$parent_attribute = $object
->getAttribute('parent');
if (empty($parent_attribute)) {
return [
'0',
];
}
$attribute_value = $parent_attribute
->getValue();
if (empty($attribute_value[CDFObject::LANGUAGE_UNDETERMINED])) {
return [
'0',
];
}
return $attribute_value[CDFObject::LANGUAGE_UNDETERMINED];
}
protected function findTaxonomyTerm(?string $label, ?string $vocabulary, string $parent, DependencyStack $stack) {
if (!$label || !$vocabulary) {
return NULL;
}
if (Uuid::isValid($parent)) {
$parent_term = $stack
->getDependency($parent);
if (!$parent_term) {
throw new \Exception(sprintf("Taxonomy term %s could not be found in the dependency stack during DataTamper.", $parent));
}
$parent = $parent_term
->getId();
}
$terms = $this
->getTermStorage()
->loadByProperties([
'name' => $label,
'vid' => $vocabulary,
'parent' => $parent,
]);
if ($parent && empty($terms)) {
return NULL;
}
return array_shift($terms);
}
protected function addDependency(LoadLocalEntityEvent $event, CDFObject $object, EntityInterface $entity) : void {
$wrapper = new DependentEntityWrapper($entity);
$wrapper
->setRemoteUuid($object
->getUuid());
$event
->getStack()
->addDependency($wrapper);
}
protected function getVocabularyStorage() {
return \Drupal::entityTypeManager()
->getStorage('taxonomy_vocabulary');
}
protected function getTermStorage() {
return \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
}
}