Commit ae7c8a62 authored by catch's avatar catch
Browse files

Issue #3497935 by prudloff, smustgrave: Renderer::getCurrentRenderContext()...

Issue #3497935 by prudloff, smustgrave: Renderer::getCurrentRenderContext() triggers a TypeError when there is no current request

(cherry picked from commit e7bb6157)
parent f1bd4c55
Loading
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -652,6 +652,11 @@ public function executeInRenderContext(RenderContext $context, callable $callabl
   */
  protected function getCurrentRenderContext() {
    $request = $this->requestStack->getCurrentRequest();

    if (is_null($request)) {
      return NULL;
    }

    return static::$contextCollection[$request] ?? NULL;
  }

+18 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

namespace Drupal\Tests\Core\Render;

use Drupal\Core\Render\RenderContext;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
@@ -1063,6 +1064,23 @@ public static function providerTestAddCacheableDependency() {
    ];
  }

  /**
   * @covers ::hasRenderContext
   */
  public function testHasRenderContext(): void {
    // Tests with no render context.
    $this->assertFalse($this->renderer->hasRenderContext());

    // Tests in a render context.
    $this->renderer->executeInRenderContext(new RenderContext(), function () {
      $this->assertTrue($this->renderer->hasRenderContext());
    });

    // Test that the method works with no current request.
    $this->requestStack->pop();
    $this->assertFalse($this->renderer->hasRenderContext());
  }

}

class TestAccessClass implements TrustedCallbackInterface {