ContentHubEntityEmbedHandler.php in Acquia Content Hub 8
File
src/ContentHubEntityEmbedHandler.php
View source
<?php
namespace Drupal\acquia_contenthub;
use Drupal\Component\Utility\Html;
class ContentHubEntityEmbedHandler {
protected $field;
public function __construct($field = NULL) {
$this->field = $field;
}
public function getReferencedUuids($text) {
$result = [];
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
if (strpos($text, 'data-entity-uuid') !== FALSE) {
foreach ($xpath
->query('//drupal-entity[@data-entity-type and @data-entity-uuid]') as $node) {
$uuid = NULL;
if ($uuid = $node
->getAttribute('data-entity-uuid')) {
$result[] = $uuid;
}
}
}
return $result;
}
public function getReferencedEntities() {
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) {
$entity_type = $node
->getAttribute('data-entity-type');
$entity = NULL;
try {
$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;
}
public function isProcessable() {
if ($this->field == NULL) {
return FALSE;
}
$field_type = $this->field
->getFieldDefinition()
->getType();
$types = [
'text_with_summary',
'string_long',
'text_long',
];
return in_array($field_type, $types);
}
}