You are here

class HtmlLinkDetector in Lingotek Translation 3.7.x

Same name and namespace in other branches
  1. 4.0.x src/Plugin/RelatedEntitiesDetector/HtmlLinkDetector.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\HtmlLinkDetector
  2. 3.5.x src/Plugin/RelatedEntitiesDetector/HtmlLinkDetector.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\HtmlLinkDetector
  3. 3.6.x src/Plugin/RelatedEntitiesDetector/HtmlLinkDetector.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\HtmlLinkDetector
  4. 3.8.x src/Plugin/RelatedEntitiesDetector/HtmlLinkDetector.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\HtmlLinkDetector

@RelatedEntitiesDetector ( id = "html_link_detector", title =

Plugin annotation


@Translation("Get editor linked entities with html links"),
  description = @translation("Get editor linked entities with html links."),
  weight = 7,
)

Hierarchy

Expanded class hierarchy of HtmlLinkDetector

1 file declares its use of HtmlLinkDetector
HtmlLinkDetectorTest.php in tests/src/Unit/Plugin/RelatedEntitiesDetector/HtmlLinkDetectorTest.php

File

src/Plugin/RelatedEntitiesDetector/HtmlLinkDetector.php, line 22

Namespace

Drupal\lingotek\Plugin\RelatedEntitiesDetector
View source
class HtmlLinkDetector extends EditorDetectorBase {

  /**
   * A Symfony request instance
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * Entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The Drupal Path Validator service.
   *
   * @var \Drupal\Core\Path\PathValidatorInterface
   */
  protected $pathValidator;

  /**
   * {@inheritdoc}
   */
  protected $fieldTypes = [
    "text",
    "text_long",
    "text_with_summary",
  ];

  /**
   * NestedEntityReferences constructor.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param array $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The EntityRepositoryInterface service.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager.
   * @param \Drupal\lingotek\LingotekConfigurationServiceInterface $lingotek_configuration
   *   The Lingotek configuration service.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
   *   The Drupal Path Validator service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityRepositoryInterface $entity_repository, EntityFieldManagerInterface $entity_field_manager, LingotekConfigurationServiceInterface $lingotek_configuration, Request $request, EntityTypeManagerInterface $entity_type_manager, PathValidatorInterface $path_validator) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_repository, $entity_field_manager, $lingotek_configuration);
    $this->request = $request;
    $this->entityTypeManager = $entity_type_manager;
    $this->pathValidator = $path_validator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity.repository'), $container
      ->get('entity_field.manager'), $container
      ->get('lingotek.configuration'), $container
      ->get('request_stack')
      ->getCurrentRequest(), $container
      ->get('entity_type.manager'), $container
      ->get('path.validator'));
  }
  protected function extractEntitiesFromText($text) {

    // This method is adapted from \Drupal\entity_usage\Plugin\EntityUsage\Track\HtmlLink::parseEntitiesFromText().
    $dom = Html::load($text);
    $xpath = new \DOMXPath($dom);
    $entities = [];

    // Loop trough all the <a> elements that don't have the LinkIt attributes.
    $xpath_query = "//a[@href != '']";
    foreach ($xpath
      ->query($xpath_query) as $element) {

      /** @var \DOMElement $element */
      try {

        // Get the href value of the <a> element.
        $href = $element
          ->getAttribute('href');

        // Strip off the scheme and host, so we only get the path.
        $domain = $this->request
          ->getSchemeAndHttpHost() . $this->request
          ->getBasePath();
        if (($position = strpos($href, $domain)) === 0) {
          $href = str_replace($domain, '', $href);
        }
        $target_type = $target_id = NULL;

        // Check if the href links to an entity.
        $url = $this->pathValidator
          ->getUrlIfValidWithoutAccessCheck($href);
        if ($url && $url
          ->isRouted() && preg_match('/^entity\\./', $url
          ->getRouteName())) {

          // Ge the target entity type and ID.
          $route_parameters = $url
            ->getRouteParameters();
          $target_type = array_keys($route_parameters)[0];
          $target_id = $route_parameters[$target_type];
        }
        elseif (\preg_match('{^/?' . $this->publicFileDirectory . '/}', $href)) {

          // Check if we can map the link to a public file.
          $file_uri = preg_replace('{^/?' . $this->publicFileDirectory . '/}', 'public://', urldecode($href));
          $files = $this->entityTypeManager
            ->getStorage('file')
            ->loadByProperties([
            'uri' => $file_uri,
          ]);
          if ($files) {

            // File entity found.
            $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')) {

              // Normally the Linkit plugin handles when a element has this
              // attribute, but sometimes users may change the HREF manually and
              // leave behind the wrong UUID.
              $data_uuid = $element
                ->getAttribute('data-entity-uuid');

              // If the UUID is the same as found in HREF, then skip it because
              // it's LinkIt's job to register this usage.
              if ($data_uuid == $entity
                ->uuid()) {
                continue;
              }
            }
            $entities[$entity
              ->uuid()] = $target_type;
          }
        }
      } catch (\Exception $e) {

        // Do nothing.
      }
    }
    return $entities;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EditorDetectorBase::$entityFieldManager protected property The entity field manager.
EditorDetectorBase::$entityRepository protected property The EntityRepository service.
EditorDetectorBase::$lingotekConfiguration protected property The Lingotek configuration service.
EditorDetectorBase::$xpath protected property The XPath to find embedded content. 2
EditorDetectorBase::extract public function Extract nested and related content. Overrides RelatedEntitiesDetectorInterface::extract
HtmlLinkDetector::$entityTypeManager protected property Entity type manager service.
HtmlLinkDetector::$fieldTypes protected property The field types this detector applies to. Overrides EditorDetectorBase::$fieldTypes
HtmlLinkDetector::$pathValidator protected property The Drupal Path Validator service.
HtmlLinkDetector::$request protected property A Symfony request instance
HtmlLinkDetector::create public static function Creates an instance of the plugin. Overrides EditorDetectorBase::create
HtmlLinkDetector::extractEntitiesFromText protected function Extract entities referenced by a given text. Overrides EditorDetectorBase::extractEntitiesFromText
HtmlLinkDetector::__construct public function NestedEntityReferences constructor. Overrides EditorDetectorBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.