Jarves\ContentRender::renderContent PHP Method

renderContent() public method

Build HTML for given content.
public renderContent ( Jarves\Model\ContentInterface $content, array $parameters = [] ) : string
$content Jarves\Model\ContentInterface
$parameters array
return string
    public function renderContent(ContentInterface $content, $parameters = array())
    {
        $type = $content->getType() ?: 'text';
        if ('stopper' === $type) {
            return '';
        }
        $title = sprintf('Content %d [%s]', $content->getId(), $type);
        $this->stopwatch->start($title, 'Jarves');
        $typeRenderer = $this->getTypeRenderer($type);
        if (!$typeRenderer) {
            $this->stopwatch->stop($title);
            throw new TypeNotFoundException(sprintf('Type renderer for `%s` not found. [%s] %s', $type, json_encode($content), json_encode(array_keys($this->types))));
        }
        $typeRenderer->setContent($content);
        $typeRenderer->setParameters($parameters);
        $html = $typeRenderer->render();
        $data['content'] = $content->toArray(TableMap::TYPE_CAMELNAME);
        $data['parameter'] = $parameters;
        $data['html'] = $html;
        $this->eventDispatcher->dispatch('core/render/content/pre', new GenericEvent($data));
        $unsearchable = false;
        if (!is_array($content->getAccessFromGroups()) && $content->getAccessFromGroups() != '' || is_array($content->getAccessFromGroups()) && count($content->getAccessFromGroups()) > 0 || $content->getAccessFrom() > 0 && $content->getAccessFrom() > time() || $content->getAccessTo() > 0 && $content->getAccessTo() < time() || $content->getUnsearchable()) {
            $unsearchable = true;
        }
        if ($html) {
            if ($content->getTemplate() == '' || $content->getTemplate() == '-') {
                if ($unsearchable && $data['html']) {
                    $result = '<!--unsearchable-begin-->' . $data['html'] . '<!--unsearchable-end-->';
                } else {
                    $result = $data['html'];
                }
            } else {
                $result = $this->templating->render($content->getTemplate(), $data);
                if ($unsearchable && $result) {
                    $result = '<!--unsearchable-begin-->' . $result . '<!--unsearchable-end-->';
                }
            }
        }
        $argument = array(&$result, $data);
        $this->eventDispatcher->dispatch('core/render/content', new GenericEvent($argument));
        $this->stopwatch->stop($title);
        return $result;
    }

Usage Example

Beispiel #1
0
 /**
  * @ApiDoc(
  *  section="Administration",
  *  description="Returns a renderer content element as preview for Jarves page editor"
  * )
  *
  * @Rest\QueryParam(name="template", requirements=".+", strict=true,
  *      description="The template/view to be used for this content")
  *
  * @Rest\QueryParam(name="type", requirements=".+", strict=true, description="The content type")
  *
  * @Rest\QueryParam(name="nodeId", requirements="[0-9]+",
  *      description="The node id in which context this content should be rendered")
  * @Rest\QueryParam(name="domainId", requirements="[0-9]+",
  *      description="The domain id in which context this content should be rendered")
  * @Rest\RequestParam(name="content", requirements=".*", description="The actual content")
  *
  * @Rest\Post("/admin/content/preview")
  *
  * @param ParamFetcher $paramFetcher
  *
  * @return array
  */
 public function getContentPreviewAction(ParamFetcher $paramFetcher)
 {
     $template = $paramFetcher->get('template');
     $type = $paramFetcher->get('type');
     $content = $paramFetcher->get('content');
     $nodeId = $paramFetcher->get('nodeId');
     $domainId = $paramFetcher->get('domainId');
     //todo, check if $template is defined as content template
     $contentObject = new Content();
     $contentObject->setType($type);
     $contentObject->setTemplate($template);
     $contentObject->setContent($content);
     if ($domainId) {
         $domain = $this->pageStack->getDomain($domainId);
         $this->pageStack->setCurrentDomain($domain);
     }
     if ($nodeId) {
         $page = $this->pageStack->getPage($nodeId);
         $this->pageStack->setCurrentPage($page);
     }
     return $this->contentRender->renderContent($contentObject, ['preview' => true]);
 }