Drupal 9 + Fivestar = ajax 500

In version 9 of Drupal, when editing the display of the fivestar rating field, the following error may appear - when you click on the field options gear (to change the rating style or edit the displayed value), nothing happens, and an error is written in the console

Drupal\Core\Security\UntrustedCallbackException: Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was Drupal\fivestar\Plugin\Field\FieldFormatter\StarsFormatter::previewsExpand. See https://www.drupal.org/node/2966725 in Drupal\Core\Render\Renderer->doTrustedCallback() (line 96 of /sites/site.com.ua/web/core/lib/Drupal/Core/Security/DoTrustedCallbackTrait.php).

To fix this error, apply the following patch

diff --git a/src/Plugin/Field/FieldFormatter/FivestarFormatterBase.php b/src/Plugin/Field/FieldFormatter/FivestarFormatterBase.php
index c79854a..d513d96 100644
--- a/src/Plugin/Field/FieldFormatter/FivestarFormatterBase.php
+++ b/src/Plugin/Field/FieldFormatter/FivestarFormatterBase.php
@@ -5,13 +5,14 @@ namespace Drupal\fivestar\Plugin\Field\FieldFormatter;
 use Drupal\Core\Field\FieldDefinitionInterface;
 use Drupal\Core\Field\FormatterBase;
 use Drupal\Core\Render\Element;
+use Drupal\Core\Security\TrustedCallbackInterface;
 use Drupal\fivestar\WidgetManager;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Base class for Fivestar field formatters.
  */
-abstract class FivestarFormatterBase extends FormatterBase {
+abstract class FivestarFormatterBase extends FormatterBase implements TrustedCallbackInterface {
 
   /**
    * The widget manager.
@@ -61,6 +62,13 @@ abstract class FivestarFormatterBase extends FormatterBase {
     );
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public static function trustedCallbacks() {
+    return ['previewsExpand'];
+  }
+
   /**
    * Prepares the widget's render element for rendering.
    *

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
The comment language code.