Commit 791670b5 authored by Pierre Dureau's avatar Pierre Dureau
Browse files

fix: #3488260 Stop logging an error when component not found

By: geek-merlin
By: kensae
By: longwave
By: dcam
By: nod_
By: pdureau
parent a4b46f33
Loading
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -1975,7 +1975,6 @@ services:
  Drupal\Core\Template\Loader\ComponentLoader:
    arguments:
      - '@plugin.manager.sdc'
      - '@logger.channel.default'
    tags:
      - { name: twig.loader, priority: 5 }
  Drupal\Core\EventSubscriber\CsrfExceptionSubscriber: ~
+1 −40
Original line number Diff line number Diff line
@@ -5,8 +5,6 @@
use Drupal\Component\Discovery\YamlDirectoryDiscovery;
use Drupal\Core\Render\Component\Exception\ComponentNotFoundException;
use Drupal\Core\Theme\ComponentPluginManager;
use Drupal\Core\Utility\Error;
use Psr\Log\LoggerInterface;
use Twig\Error\LoaderError;
use Twig\Loader\LoaderInterface;
use Twig\Source;
@@ -21,47 +19,11 @@ class ComponentLoader implements LoaderInterface {
   *
   * @param \Drupal\Core\Theme\ComponentPluginManager $pluginManager
   *   The plugin manager.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger.
   */
  public function __construct(
    protected ComponentPluginManager $pluginManager,
    protected LoggerInterface $logger,
  ) {}

  /**
   * Finds a template in the file system based on the template name.
   *
   * @param string $name
   *   The template name.
   * @param bool $throw
   *   TRUE to throw an exception when the component is not found. FALSE to
   *   return NULL if the component cannot be found.
   *
   * @return string|null
   *   The path to the component.
   *
   * @throws \Twig\Error\LoaderError
   *   Thrown if a template matching $name cannot be found and $throw is TRUE.
   */
  protected function findTemplate(string $name, bool $throw = TRUE): ?string {
    $path = $name;
    try {
      $component = $this->pluginManager->find($name);
      $path = $component->getTemplatePath();
    }
    catch (ComponentNotFoundException $e) {
      if ($throw) {
        throw new LoaderError($e->getMessage(), $e->getCode(), $e);
      }
    }
    if ($path || !$throw) {
      return $path;
    }

    throw new LoaderError(sprintf('Unable to find template "%s" in the components registry.', $name));
  }

  /**
   * {@inheritdoc}
   */
@@ -73,8 +35,7 @@ public function exists($name): bool {
      $this->pluginManager->find($name);
      return TRUE;
    }
    catch (ComponentNotFoundException $e) {
      Error::logException($this->logger, $e);
    catch (ComponentNotFoundException) {
      return FALSE;
    }
  }
+1 −5
Original line number Diff line number Diff line
@@ -11,7 +11,6 @@
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Psr\Log\LoggerInterface;

/**
 * Unit tests for the component loader class.
@@ -56,10 +55,7 @@ public function testIsFresh(): void {

    $component_manager = $this->prophesize(ComponentPluginManager::class);
    $component_manager->find('sdc_test:loader-test')->willReturn($component);
    $component_loader = new ComponentLoader(
      $component_manager->reveal(),
      $this->createMock(LoggerInterface::class),
    );
    $component_loader = new ComponentLoader($component_manager->reveal());

    // Assert the component is fresh, as it changed before the current time.
    $this->assertTrue($component_loader->isFresh('sdc_test:loader-test', $current_time), 'Twig and YAML files were supposed to be fresh');