You are here

TemplateController.php in Wysiwyg API template plugin 3.0.x

Same filename and directory in other branches
  1. 8.2 src/Controller/TemplateController.php

File

src/Controller/TemplateController.php
View source
<?php

namespace Drupal\wysiwyg_template\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\wysiwyg_template\Entity\Template;
use stdClass;
use Symfony\Component\HttpFoundation\Response;

/**
 * Default controller for the wysiwyg_template module.
 */
class TemplateController extends ControllerBase {

  /**
   * A list of available templates based on the entity type and bundle.
   *
   * @param string $entity_type
   *   The entity type.
   * @param string $bundle
   *   The bundle.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The template callback JS.
   *
   * @see https://www.drupal.org/node/2693221
   */
  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;
  }

}

Classes

Namesort descending Description
TemplateController Default controller for the wysiwyg_template module.