You are here

class JsCallback in JS Callback Handler 8.3

Defines a JsCallback annotation object.

Hierarchy

Expanded class hierarchy of JsCallback

2 classes are annotated with JsCallback
Content in src/Plugin/Js/Content.php
Plugin annotation @JsCallback( id = "js.content", allowed_methods = { "GET" }, csrf_token = FALSE, )
Email in js_callback_examples/src/Plugin/Js/Email.php
Plugin annotation @JsCallback( id = "js_callback_examples.email", parameters = { "user" = { "type" = "entity:user" }, }, )

File

src/Annotation/JsCallback.php, line 14

Namespace

Drupal\js\Annotation
View source
class JsCallback extends Plugin {

  /**
   * The HTTP request methods allowed.
   *
   * This must be an array of string values. If the request does not match any
   * of the allowed methods defined by the callback, it will be rejected.
   *
   * @var array
   */
  public $allowed_methods = [];

  /**
   * Captures any printed output from the callback.
   *
   * Normally a callback should return its content, not print it. By default
   * this property is enabled and will discard any printed output.
   *
   * @var bool
   *
   * @see hook_js_captured_content_alter
   */
  public $capture_output = TRUE;

  /**
   * Generates a token to prevent CSRF attacks for authenticated users.
   *
   * If the callback is only accessible to authenticated users, it is strongly
   * recommended that this is not disabled, otherwise your site could
   * potentially be susceptible to CSRF attacks.
   *
   * If the callback needs to support both anonymous and authenticated users,
   * then this should be disable and the responsibility of checking request
   * validity falls to the callback itself.
   *
   * @var bool
   */
  public $csrf_token = TRUE;

  /**
   * Dynamically loads arguments for a callback.
   *
   * An associative array of key/value pairs where parameter name is the key
   * and a callback is the value. The callback will be passed a single
   * argument, the value of the passed parameter.
   *
   * The JS module automatically detects any "PARAMETER_load" functions that
   * exists based on if "process request" is enabled and the function
   * explicitly specifies that parameter in it's callback function signature.
   *
   * For example: if one of the parameters passed is "node" and the callback
   * function signature defines a $node argument, then a "node_load" function
   * will be invoked (if the function exists). Optionally, to disable the
   * automatic load callback for a parameter, you can set the callback to
   * FALSE or to disable all automatic "load arguments" processing you may
   * set FALSE for the entire "load arguments" property.
   *
   * @todo Fix this documentation to reference route option parameters.
   *
   * @var array
   */
  public $parameters = [];

  /**
   * The class or service that sends the result of the callback to the browser.
   *
   * This response object, whether a stand alone class or service, must be an
   * instance of \Drupal\js\JsResponse.
   *
   * Defaults to \Drupal\js\JsResponse.
   *
   * @var string
   *
   * @see \Drupal\js\JsResponse
   */
  public $response = '\\Drupal\\js\\JsResponse';

  /**
   * The human readable title of the callback.
   *
   * @var \Drupal\Core\Annotation\Translation
   */
  public $title;

  /** @var  \Drupal\Core\ParamConverter\ParamConverterManager */
  protected $paramConvertManager;

  /**
   * {@inheritdoc}
   */
  public function __construct($values) {
    parent::__construct($values);
    $this->paramConvertManager = \Drupal::service('paramconverter_manager');
    $this
      ->setAllowedMethods();
    $this
      ->setParameterConverters();
  }

  /**
   * Sets default allowed methods.
   */
  public function setAllowedMethods() {
    if (!$this->definition['allowed_methods']) {
      $this->definition['allowed_methods'] = [
        'POST',
        'GET',
      ];
    }
  }

  /**
   * Sets parameter converters.
   */
  public function setParameterConverters() {
    if (!$this->definition['parameters']) {
      return;
    }

    // Create an empty "route" so we can add the necessary param converts to it.
    $route = new Route('');
    $route
      ->setOption('parameters', $this->definition['parameters']);

    // Create a route collection for the parameter convert manager.
    $collection = new RouteCollection();
    $collection
      ->add('', $route);
    $this->paramConvertManager
      ->setRouteParameterConverters($collection);

    // Retrieve the filled out parameters.
    $this->definition['parameters'] = $route
      ->getOption('parameters');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JsCallback::$allowed_methods public property The HTTP request methods allowed.
JsCallback::$capture_output public property Captures any printed output from the callback.
JsCallback::$csrf_token public property Generates a token to prevent CSRF attacks for authenticated users.
JsCallback::$paramConvertManager protected property @var \Drupal\Core\ParamConverter\ParamConverterManager
JsCallback::$parameters public property Dynamically loads arguments for a callback.
JsCallback::$response public property The class or service that sends the result of the callback to the browser.
JsCallback::$title public property The human readable title of the callback.
JsCallback::setAllowedMethods public function Sets default allowed methods.
JsCallback::setParameterConverters public function Sets parameter converters.
JsCallback::__construct public function Constructs a Plugin object. Overrides Plugin::__construct
Plugin::$definition protected property The plugin definition read from the class annotation. 1
Plugin::get public function Gets the value of an annotation. Overrides AnnotationInterface::get 5
Plugin::getClass public function Gets the class of the annotated class. Overrides AnnotationInterface::getClass
Plugin::getId public function Gets the unique ID for this annotated class. Overrides AnnotationInterface::getId
Plugin::getProvider public function Gets the name of the provider of the annotated class. Overrides AnnotationInterface::getProvider
Plugin::parse protected function Parses an annotation into its definition.
Plugin::setClass public function Sets the class of the annotated class. Overrides AnnotationInterface::setClass
Plugin::setProvider public function Sets the name of the provider of the annotated class. Overrides AnnotationInterface::setProvider