public function ContentHubEntityEmbedHandler::getReferencedEntities in Acquia Content Hub 8
Parse entities from html text.
Return value
array Array with entities.
File
- src/
ContentHubEntityEmbedHandler.php, line 63
Class
- ContentHubEntityEmbedHandler
- Content Hub Entity Embed Handler Class.
Namespace
Drupal\acquia_contenthubCode
public function getReferencedEntities() {
// Check if field not set properly.
if ($this->field == NULL) {
return [];
}
$text = $this->field
->getString();
$result = [];
if (strpos($text, 'data-entity-type') !== FALSE && (strpos($text, 'data-entity-uuid') !== FALSE || strpos($text, 'data-entity-id') !== FALSE)) {
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
foreach ($xpath
->query('//drupal-entity[@data-entity-type and (@data-entity-uuid or @data-entity-id)]') as $node) {
/** @var \DOMElement $node */
$entity_type = $node
->getAttribute('data-entity-type');
$entity = NULL;
try {
// Load the entity either by UUID (preferred) or ID.
$id = NULL;
$entity = NULL;
if ($id = $node
->getAttribute('data-entity-uuid')) {
$entity = \Drupal::entityTypeManager()
->getStorage($entity_type)
->loadByProperties([
'uuid' => $id,
]);
$entity = current($entity);
}
else {
$id = $node
->getAttribute('data-entity-id');
$entity = \Drupal::entityTypeManager()
->getStorage($entity_type)
->load($id);
}
if ($entity) {
$result[$entity
->uuid()] = $entity;
}
} catch (\Exception $e) {
watchdog_exception('content_hub', $e);
}
}
}
return $result;
}