Commit a7f1204e authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Issue #3289899: Automated Drupal 10 compatibility fixes

parent 89a8cbb0
Loading
Loading
Loading
Loading
+12 −8
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@

namespace Drupal\svg_embed\Plugin\Filter;

use DOMXPath;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
@@ -33,6 +32,8 @@ class SvgEmbed extends FilterBase implements ContainerFactoryPluginInterface {
  protected $entityTypeManager;

  /**
   * The processor class for SVGs.
   *
   * @var \Drupal\svg_embed\SvgEmbedProcessInterface
   */
  protected $svgEmbedProcess;
@@ -47,7 +48,9 @@ class SvgEmbed extends FilterBase implements ContainerFactoryPluginInterface {
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\svg_embed\SvgEmbedProcessInterface $svg_embed_process
   *   The processor class from this module.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, SvgEmbedProcessInterface $svg_embed_process) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
@@ -77,14 +80,14 @@ class SvgEmbed extends FilterBase implements ContainerFactoryPluginInterface {
    // Do we have at least one SVG reference in the $text?
    if (stripos($text, 'data-entity-type="file"') !== FALSE) {
      $dom = Html::load($text);
      $xpath = new DOMXPath($dom);
      $xpath = new \DOMXPath($dom);
      $processed_uuids = [];
      $patterns = [];

      // Identify the SVG nodes.
      /** @var \DOMNode $node */
      foreach ($xpath->query('//*[@data-entity-type="file" and @data-entity-uuid]') as $node) {
        /** @noinspection PhpPossiblePolymorphicInvocationInspection */
        if (method_exists($node, 'getAttribute')) {
          $uuid = $node->getAttribute('data-entity-uuid');
          // Only process the first occurrence of each file UUID.
          if (!isset($processed_uuids[$uuid])) {
@@ -92,6 +95,7 @@ class SvgEmbed extends FilterBase implements ContainerFactoryPluginInterface {
          }
          $patterns[(string) $node] = $uuid;
        }
      }
      $text = Html::serialize($dom);
      foreach ($patterns as $pattern => $uuid) {
        $text = str_replace($pattern, $processed_uuids[$uuid], $text);
+27 −21
Original line number Diff line number Diff line
@@ -7,27 +7,31 @@ use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\file\Entity\File;
use SimpleXMLElement;

/**
 * Class SvgEmbedProcess.
 * Processor for SVGs.
 *
 * @package Drupal\svg_embed
 */
class SvgEmbedProcess implements SvgEmbedProcessInterface {

  /**
   * The entity manager object.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * A database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;
@@ -36,8 +40,11 @@ class SvgEmbedProcess implements SvgEmbedProcessInterface {
   * SvgEmbedProcess constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager object.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler object.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, Connection $connection) {
    $this->entityTypeManager = $entity_type_manager;
@@ -48,7 +55,7 @@ class SvgEmbedProcess implements SvgEmbedProcessInterface {
  /**
   * {@inheritdoc}
   */
  public function translate($uuid, $langcode): string {
  public function translate(string $uuid, string $langcode): string {
    $xml = $this->loadFile($uuid);

    if ($this->moduleHandler->moduleExists('locale')) {
@@ -63,44 +70,44 @@ class SvgEmbedProcess implements SvgEmbedProcessInterface {
  }

  /**
   * Load an SVG file.
   *
   * @param string $uuid
   *   The file's UUID.
   *
   * @return \SimpleXMLElement
   *   The file as an XML object.
   */
  private function loadFile($uuid): SimpleXMLElement {
  private function loadFile(string $uuid): \SimpleXMLElement {
    $text = '';
    try {
      /** @var File $file */
      if ($file = $this->entityTypeManager
        ->getStorage('file')
        ->loadByProperties(['uuid' => $uuid])) {
      /** @var \Drupal\file\Entity\File $file */
      $file = $this->entityTypeManager->getStorage('file')->loadByProperties(['uuid' => $uuid]);
      if ($file) {
        $text = file_get_contents($file->getFileUri());
      }
    }
    catch (InvalidPluginDefinitionException $e) {
      // TODO: log this exception.
    }
    catch (PluginNotFoundException $e) {
      // TODO: log this exception.
    catch (InvalidPluginDefinitionException | PluginNotFoundException $e) {
      // @todo log this exception.
    }
    return new SimpleXMLElement($text);
    return new \SimpleXMLElement($text);
  }

  /**
   * Helper function called recursively to translate all strings in an SVG file.
   *
   * @param SimpleXMLElement $xml
   *   the SVG graphic code
   * @param \SimpleXMLElement $xml
   *   The SVG graphic code.
   * @param string $langcode
   *   the language code to which we need to translate
   *   The language code to which we need to translate.
   */
  protected function embedTranslate($xml, $langcode): void {
  protected function embedTranslate(\SimpleXMLElement $xml, string $langcode): void {
    foreach ($xml as $child) {
      $this->embedTranslate($child, $langcode);
      if (isset($child->text) || isset($child->tspan)) {
        if (isset($child->text->tspan)) {
          $text = $child->text->tspan;
        }
        /** @noinspection NotOptimalIfConditionsInspection */
        elseif (isset($child->tspan)) {
          $text = $child->tspan;
        }
@@ -117,7 +124,6 @@ class SvgEmbedProcess implements SvgEmbedProcessInterface {
          if (!empty($string)) {
            $query = $this->connection->select('locales_source', 's');
            $query->leftJoin('locales_target', 't', 's.lid = t.lid');
            /** @noinspection NullPointerExceptionInspection */
            $translation = $query->fields('t', ['translation'])
              ->condition('s.source', $string)
              ->condition('s.textgroup', 'svg_embed')
+8 −2
Original line number Diff line number Diff line
@@ -3,17 +3,23 @@
namespace Drupal\svg_embed;

/**
 * Interface SvgEmbedProcessInterface.
 * Interface for the Processor.
 *
 * @package Drupal\svg_embed
 */
interface SvgEmbedProcessInterface {

  /**
   * A method to translate SVG elements.
   *
   * @param string $uuid
   *   The file object UUId.
   * @param string $langcode
   *   The file's language.
   *
   * @return string
   *   The text translation.
   */
  public function translate($uuid, $langcode): string;
  public function translate(string $uuid, string $langcode): string;

}