View source
<?php
namespace Drupal\entity_usage\Plugin\EntityUsage\Track;
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\entity_usage\EntityUsage;
use Symfony\Component\DependencyInjection\ContainerInterface;
class HtmlLink extends TextFieldEmbedBase {
protected $pathValidator;
protected $publicFileDirectory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityUsage $usage_service, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, EntityRepositoryInterface $entity_repository, PathValidatorInterface $path_validator, StreamWrapperInterface $public_stream) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $usage_service, $entity_type_manager, $entity_field_manager, $config_factory, $entity_repository);
$this->pathValidator = $path_validator;
$this->publicFileDirectory = method_exists($public_stream, 'getDirectoryPath') ? $public_stream
->getDirectoryPath() : '';
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_usage.usage'), $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('config.factory'), $container
->get('entity.repository'), $container
->get('path.validator'), $container
->get('stream_wrapper.public'));
}
public function parseEntitiesFromText($text) {
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
$entities = [];
$xpath_query = "//a[@href != '']";
foreach ($xpath
->query($xpath_query) as $element) {
try {
$href = $element
->getAttribute('href');
$site_domains = $this->config
->get('site_domains') ?: [];
foreach ($site_domains as $site_domain) {
$host_pattern = '{^https?://' . str_replace('.', '\\.', $site_domain) . '/}';
if (\preg_match($host_pattern, $href)) {
$href = preg_replace($host_pattern, '/', $href);
break;
}
}
$target_type = $target_id = NULL;
$url = $this->pathValidator
->getUrlIfValidWithoutAccessCheck($href);
if ($url && $url
->isRouted() && preg_match('/^entity\\./', $url
->getRouteName())) {
$route_parameters = $url
->getRouteParameters();
$target_type = array_keys($route_parameters)[0];
$target_id = $route_parameters[$target_type];
}
elseif (\preg_match('{^/?' . $this->publicFileDirectory . '/}', $href)) {
$file_uri = preg_replace('{^/?' . $this->publicFileDirectory . '/}', 'public://', urldecode($href));
$files = $this->entityTypeManager
->getStorage('file')
->loadByProperties([
'uri' => $file_uri,
]);
if ($files) {
$target_type = 'file';
$target_id = array_keys($files)[0];
}
}
if ($target_type && $target_id) {
$entity = $this->entityTypeManager
->getStorage($target_type)
->load($target_id);
if ($entity) {
if ($element
->hasAttribute('data-entity-uuid')) {
$data_uuid = $element
->getAttribute('data-entity-uuid');
if ($data_uuid == $entity
->uuid()) {
continue;
}
}
$entities[$entity
->uuid()] = $target_type;
}
}
} catch (\Exception $e) {
}
}
return $entities;
}
}