doTrustedCallback example


class DoTrustedCallbackTraitTest extends UnitTestCase {
  use DoTrustedCallbackTrait;

  /** * @covers ::doTrustedCallback * @dataProvider providerTestTrustedCallbacks */
  public function testTrustedCallbacks(callable $callback$extra_trusted_interface = NULL) {
    $return = $this->doTrustedCallback($callback[], '%s is not trusted', TrustedCallbackInterface::THROW_EXCEPTION, $extra_trusted_interface);
    $this->assertSame('test', $return);
  }

  /** * Data provider for ::testTrustedCallbacks(). */
  public function providerTestTrustedCallbacks() {
    $closure = function D) {
      return 'test';
    };

    

      elseif ($double_colon > 0) {
        $callback = explode('::', $callback, 2);
      }
    }
    $message = sprintf('Render %s callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', $callback_type, '%s');
    // Add \Drupal\Core\Render\Element\RenderCallbackInterface as an extra     // trusted interface so that:     // - All public methods on Render elements are considered trusted.     // - Helper classes that contain only callback methods can implement this     // instead of TrustedCallbackInterface.     return $this->doTrustedCallback($callback$args$message, TrustedCallbackInterface::THROW_EXCEPTION, RenderCallbackInterface::class);
  }

  /** * Add cache debug information to the render array. * * @param array $elements * The renderable array that must be wrapped with the cache debug output. * @param bool $is_cache_hit * A flag indicating that the cache is hit or miss. * @param array $pre_bubbling_elements * The renderable array for pre-bubbling elements. * @param float $render_time * The rendering time. * * @return array * The renderable array. */

  public static function callback(callable $callback, array $args, string $message$error_type = TrustedCallbackInterface::THROW_EXCEPTION, $extra_trusted_interface = NULL) {
    return (new static())->doTrustedCallback($callback$args$message$error_type$extra_trusted_interface);
  }

}

  public function preRenderComponent(array $element): array {
    $props = $element['#props'];
    $props_alter_callbacks = $element['#propsAlter'];
    // This callback can be used to prepare the context. For instance to replace     // tokens in the props.     $props = array_reduce(
      $props_alter_callbacks,
      fn(array $carry, callable $callback) => $this->doTrustedCallback(
        $callback,
        [$carry],
        '%s is not trusted',
      ),
      $props
    );
    $inline_template = $this->generateComponentTemplate(
      $element['#component'],
      $element['#slots'],
      $element['#slotsAlter'],
      $props,
    );
Home | Imprint | This part of the site doesn't use cookies.