Bolt\TemplateChooser::record PHP Method

record() public method

Refactor note: Using a FQCN for the hint here as a use statement causes a fatal in the unit tests… 'cause PHP and class_alias() versus namespaces.
public record ( Content $record, array $data = null ) : string
$record Bolt\Legacy\Content
$data array
return string
    public function record($record, $data = null)
    {
        // First candidate: global config.yml
        $template = $this->app['config']->get('general/record_template');
        // Second candidate: Theme-specific config.yml file.
        if ($this->app['config']->get('theme/record_template')) {
            $template = $this->app['config']->get('theme/record_template');
        }
        // Third candidate: a template with the same filename as the name of
        // the contenttype.
        if (isset($record->contenttype['singular_slug'])) {
            if ($this->app['render']->hasTemplate($record->contenttype['singular_slug'] . '.twig')) {
                $template = $record->contenttype['singular_slug'] . '.twig';
            }
        }
        // Fourth candidate: defined specificaly in the contenttype.
        if (isset($record->contenttype['record_template'])) {
            if ($this->app['render']->hasTemplate($record->contenttype['record_template'])) {
                $template = $record->contenttype['record_template'];
            }
        }
        // Fifth candidate: An entity has a templateselect field, and it's set.
        if (isset($record->contenttype['fields'])) {
            foreach ($record->contenttype['fields'] as $name => $field) {
                if ($field['type'] == 'templateselect' && $data !== null && !empty($data[$name])) {
                    $template = $data[$name];
                }
                if ($field['type'] == 'templateselect' && !empty($record[$name])) {
                    $template = $record[$name];
                }
            }
        }
        // Sixth candidate: A legacy Content record has a templateselect field, and it's set.
        if (isset($record->contenttype['fields'])) {
            foreach ($record->contenttype['fields'] as $name => $field) {
                if ($field['type'] == 'templateselect' && !empty($record->values[$name])) {
                    $template = $record->values[$name];
                }
            }
        }
        return $template;
    }

Usage Example

Example #1
0
 /**
  * Render the not found page if on frontend and http exception
  *
  * @param GetResponseForExceptionEvent $event
  */
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     $exception = $event->getException();
     if (!$exception instanceof HttpExceptionInterface || Zone::isBackend($event->getRequest())) {
         return;
     }
     if ($exception->getStatusCode() !== Response::HTTP_NOT_FOUND) {
         return;
     }
     // If $notFoundPage is referencing a template, render it and be done.
     if ($this->render->hasTemplate($this->notFoundPage)) {
         try {
             $this->renderNotFound($event, $this->notFoundPage, []);
         } catch (TwigErrorLoader $e) {
             // Template not found, fall though to see if we can render a
             // record, failing that let the exception handler take over
         }
     }
     // Next try for referencing DB content.
     $content = $this->storage->getContent($this->notFoundPage, ['returnsingle' => true]);
     if (!$content instanceof Content || empty($content->id)) {
         return;
     }
     $template = $this->templateChooser->record($content);
     $this->renderNotFound($event, $template, $content->getTemplateContext());
 }
All Usage Examples Of Bolt\TemplateChooser::record