Update renderPlain() to RendererInterface::renderInIsolation() as reported by PHPStan

Created on 16 July 2024, 4 months ago
Updated 14 September 2024, 2 months ago

Problem/Motivation

PHPStan is currently reporting the following deprecation for next minor and next major:

 ------ ----------------------------------------------------------------------- 
  Line   tests/src/Kernel/FormatterTest.php                                     
 ------ ----------------------------------------------------------------------- 
  141    Call to deprecated method renderPlain() of class                       
         Drupal\Core\Render\Renderer:                                           
         in drupal:10.3.0 and is removed from drupal:12.0.0. Use                
           \Drupal\Core\Render\RendererInterface::renderInIsolation() instead.  
 ------ ----------------------------------------------------------------------- 

Even though the deprecation won't cause a failure until D12 in 2026, I would like the see PHPStan passing cleanly.

Steps to reproduce

Check "phpstan (next minor)" and "phpstan (next major)" pipelines.

Proposed resolution

I would like to use DeprecationHelper, as described in Matt Glaman's blog. The trouble with this is it isn't available before Drupal 10.1. So we'd either need additional logic to support versions below 10.1, or we'd need to up our minimum version supported.

I will open a separate issue to discuss the minimum version.

Remaining tasks

Update module to support new class.

User interface changes

n/a

API changes

n/a

Data model changes

n/a

📌 Task
Status

Fixed

Version

3.0

Component

Code

Created by

🇮🇪Ireland lostcarpark

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @lostcarpark
  • Assigned to sarwan_verma
  • Merge request !21Fixed renderPlain issue → (Merged) created by Unnamed author
  • Pipeline finished with Failed
    4 months ago
    Total: 152s
    #226266
  • Issue was unassigned.
  • Status changed to Needs review 4 months ago
  • 🇮🇳India sarwan_verma

    Hi @lostcarpark,

    I have resolved the RendererInterface::renderInIsolation() issue and created MR!21.

    Kindly review and verify.

    Thanks!

  • 🇮🇪Ireland lostcarpark

    Thank you for your work on this.

    The tests are passing now for current and next_major.

    Unfortunately, they are failing for previous_minor and previous_major. This is because renderInIsolation was only introduced in Drupal 10.3.

    For previous_minor, we could use DeprecationHelper as follows:

    $output = DeprecationHelper::backwardsCompatibleCall(
        currentVersion: \Drupal::VERSION,
        deprecatedVersion: '10.3',
        currentCallable: fn() => \Drupal::service('renderer')->renderInIsolation($build),
        deprecatedCallable: fn() => \Drupal::service('renderer')->renderPlain($build),
    );
    

    Unfortunately, DeprecationHelper was only introduced in 10.1, so for the moment at least, we'll need additional measures to keep support for Drupal 9.5. I think we need to support this at least while automated tests run against it. I think we could handle all supported versions with:

    if (version_compare(\Drupal::VERSION, '10.1.0') >= 0) {
      // Drupal 10.1 or later, use DeprecationHelper.
      $output = DeprecationHelper::backwardsCompatibleCall(
        currentVersion: \Drupal::VERSION,
        deprecatedVersion: '10.3',
        currentCallable: fn() => \Drupal::service('renderer')->renderInIsolation($build),
        deprecatedCallable: fn() => \Drupal::service('renderer')->renderPlain($build),
      );
    }
    else {
      // Remove when we drop support for < Drupal 10.1.
      // @phpstan-ignore-next-line
      $output = \Drupal::service('renderer')->renderPlain($build);
    }
    

    Please update the fix and see if tests will pass.

  • 🇮🇪Ireland lostcarpark

    I learned a much cleaner fix from @ankitv18:

        if (method_exists(RendererInterface::class, 'renderPlain')) {
            // @phpstan-ignore-next-line as it is deprecated in D10.3 and removed from D12.
            $field_output_renderer = \Drupal::service('renderer')->renderPlain($field_output);
          }
          else {
            $field_output_renderer = \Drupal::service('renderer')->renderInIsolation($field_output);
          }
    
  • Status changed to Fixed 3 months ago
  • 🇮🇪Ireland lostcarpark

    Change merged as needed for D11 release.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024