You are here

public function TemplateController::listJson in Wysiwyg API template plugin 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Controller/TemplateController.php \Drupal\wysiwyg_template\Controller\TemplateController::listJson()

A list of available templates based on the entity type and bundle.

Parameters

string $entity_type: The entity type.

string $bundle: The bundle.

Return value

\Symfony\Component\HttpFoundation\Response The template callback JS.

See also

https://www.drupal.org/node/2693221

1 string reference to 'TemplateController::listJson'
wysiwyg_template.routing.yml in ./wysiwyg_template.routing.yml
wysiwyg_template.routing.yml

File

src/Controller/TemplateController.php, line 28

Class

TemplateController
Default controller for the wysiwyg_template module.

Namespace

Drupal\wysiwyg_template\Controller

Code

public function listJson($entity_type, $bundle) : Response {
  $templates = [
    // @todo Support images.
    'imagesPath' => FALSE,
  ];
  foreach (Template::loadByTypeAndBundle($entity_type, $bundle) as $template) {
    $json_template = new stdClass();
    $json_template->title = $template
      ->label();

    // @todo Images.
    // @see https://www.drupal.org/node/2692469
    $json_template->description = $template
      ->getDescription();
    $json_template->html = $template
      ->getBody();
    $templates['templates'][] = $json_template;
  }
  $templates = json_encode($templates);
  $script = <<<EOL
CKEDITOR.addTemplates( 'default', {<span class="php-variable">$templates</span>});
EOL;
  $response = new Response($script);
  $response->headers
    ->set('Content-Type', 'text/javascript');
  return $response;
}