Docs: Search, comments and previews

Search, comments and previews

Site search, comments and drafts: searchResults, askBox, comments, commentsBlock, isPreview and previewed.

The examples use the $esc shortcut from the overview.

Print a search box or link only while search is on: searchEnabled() (in Actions and sections) is true. The /search address is routed only while search is on, and a form posting there on an install with search off lands on a 404.

searchResults() #

The search page's state: {state, query, page, rows, prev, next, ask, query_max, start} (the snippet is M, the rest P). query_max and start are new in 1.18.0.

<?php $r = $this->searchResults(); ?>
<h1>Results for <?= $esc($r['query']) ?></h1>
<?php $this->askBox(); ?>
<ol start="<?= (int) $r['start'] ?>">
<?php foreach ($r['rows'] as $row): ?>
    <li><a href="<?= $esc($row['url']) ?>"><?= $esc($row['title']) ?></a> <p><?= $row['snippet'] ?></p></li>
<?php endforeach; ?>
</ol>
  • Your own search page is optional. Core ships views/search.php for /search and floors it for every theme. Ship your own and it wins.
  • state is empty (nothing asked yet), preparing (the index has not finished its first build), results, throttled or paused (maintenance).
  • query is what was searched and page the page shown. prev and next are the neighbouring pages' URLs or null. There is no total.
  • Each row is {title, url, snippet, kind, date}. title is plain text. url is a root-relative path. snippet is already escaped, with the matched words in <mark>. kind is the source (page, post, forum_topic, or a module's own). date is Y-m-d H:i:s or null.
  • query_max is the longest query the index reads, for the box's maxlength. It is there on every page, so a search box in a 404 page or a header can read $this->searchResults()['query_max'].
  • start is the number of the page's first result, for an <ol start>.

askBox() #

Prints the AI answer block. A visitor can ask for an AI answer to the query they searched, when the site owner turns answers on in System ▸ AI settings. Your page shows that block only once it calls askBox(), best right after your results heading. It prints nothing unless an answer can be had, pushes its own script and stylesheet through the asset channel, and stays hidden without JavaScript. Leave it out and your page offers no answers. Do not build the block yourself from searchResults()['ask']: the token, the disclosure naming the AI provider and the script that renders the answer as plain text belong together.

Comments #

commentsBlock($kind, $id) #

Prints the comments component for a page or post.

<?php $this->commentsBlock('post', (int) $post['post_id']); ?>
<?php $this->commentsBlock('page', (int) ($page['page_id'] ?? 0)); ?>

Comments are kept by the Comments module. commentsBlock() prints nothing while that module is off. The Blog's own post view calls commentsBlock() after the article, and the starter theme's page view calls it after the page's content. If your theme ships its own post view or page view, its posts or pages show comments only once it calls it too.

It prints nothing in a preview, while no module keeps comments, while the item's comments are switched off (a page takes them only on a Layout chosen on the Comments page's Settings tab), or when the visitor may not read them. It pushes core/assets/comments.css through the asset channel. The markup is the comments component. Ship components/comments.php and yours wins over core's floor. It receives $comments, $kind and $id.

comments($kind, $id) #

A thread for a page or post as {state, total, items, more_url, form, notice, view_as, images_failed} (names P, body M), or null. It is what $comments holds in the component.

  • state is open (the form is there), closed, signin (a guest: link to the sign-in address), verify (confirm the email address first) or denied.
  • total counts every visible comment. items holds the first top comments, oldest first, each followed by its first replies. Later pages count top comments only, so a thread is never split. Each item is {id, anchor, author: {name, url, user_id, picture_url, profile_url, badge}, body, date, parent, depth, reply_to, reactions, more_replies, more_replies_link, images}.
  • Escape each author's name and url (url is a root-relative path or null). user_id, picture_url and profile_url are new in 1.19.2 and are raw: escape picture_url and profile_url too. Both are '' when the person has no profile this visitor may open. url falls back to profile_url, so the name links where the picture does. Draw the face with the person-avatar component. Echo each body as it is. date is Y-m-d H:i:s UTC.
  • Indent a reply by its depth (0 or 1, never more) and name reply_to when it is set.
  • author.badge (new in 1.20.0) is the staff badge the author's role shows, as plain text. Escape it, and draw nothing when it is empty.
  • reactions (new in 1.20.0) is the comment's reaction row, or null. Hand it as it is to the reactions component, with inert from view_as, which is true under View as (the chips show and do nothing): $this->component('reactions', ['reactions' => $item['reactions'], 'inert' => !empty($comments['view_as'])]).
  • images (new in 1.21.0) is the comment's pictures, oldest first: [{id, url, small_url, width, height}]. Print them after the body and escape both url and small_url (small_url carries &s=1). Core's pictures component draws them.
  • images_failed (new in 1.21.0) is true after ?images=failed. Say it with t('images.failed').
  • Give the section id="comments" and each comment id="<anchor>" (comment-<id>): a member lands there after posting.
  • Show more_url as a link to the rest when it is not null, and notice (held or posted) after a post.
  • A top comment may carry more_replies and more_replies_link (new in 1.20.0). Each is a root-relative path or null. Draw "Show more replies" only when both are set.
  • If you draw the thread yourself, keep this shape, or the loaded replies have nowhere to go. Each comment is an <li data-comment-id="<id>">. A top comment's replies sit inside it in an <ol class="cms-comments__replies">. "Show more replies" is that list's last item: an <li data-comment-more-replies="<more_replies>"> holding an <a href="<more_replies_link>">. Core draws this in core/components/comment-items.php.
  • The Comments module's own component loads the script that fetches the next replies in place. If your components/comments.php replaces it, that script is not loaded and the link opens the page that shows them, which works without JavaScript.
  • A theme that overrides the module's views/comments-replies.php answers with the <li>s alone: no header, footer or page-container.

Draw the form only from form, which is there only while state is open: {action, csrf, guest, fields, stamp, trap, captcha, captcha_provider, notify, replies, reply, max_length, images}.

  • Send csrf as csrf_token and the text as body.
  • form.images (new in 1.21.0) is how many pictures one comment may hold, or 0 (always 0 for a guest). Only above 0, draw the pictures-field component with it as max, and send the form as multipart/form-data.
  • A comment form of your own must also draw $restriction (new in 1.21.0) through the restriction-notice component. Without it, a member who is restricted from commenting is told nothing.
  • For a guest (form.guest true, csrf an empty string), draw the guest fields only from form.fields ({name: 'required', email: 'required'|'optional'}). Put form.stamp in a hidden input named stamp. Add an empty input named form.trap that is visually hidden, with aria-hidden="true", tabindex="-1" and autocomplete="off".
  • Call captchaBox('comment') when form.captcha_provider is set. It is recaptcha_v2, recaptcha_v3 or turnstile, else null. form.captcha is the v2 site key only while v2 runs, so a component that draws the v2 box from it alone fails under the other two providers. See Sign-in, forms and consent.
  • Offer "Email me replies" only when form.notify is true. Offer Reply (and send form.reply as parent) only when form.replies is true. Limit the body to form.max_length when it is not null.
  • A guest's thread stays cacheable. A member's form carries the session token and takes the render out of the cache, as does a render that read ?reply= or ?comment=.
  • Print the per-comment controls with actions('comments.comment', $item) (see Actions and sections).
  • Report is not one of those controls. Print it with the report component, as the last item of the comment's row of actions: $this->component('report', ['report' => $this->report('comments.comment', (int) $item['id']), 'noun' => 'comment']). See report() in Actions and sections. Since 1.20.0 core draws that row as a <div>, not a <p>, and Report is no longer in a menu at the comment's top right. A theme whose own components/report.php drew only the menu version shows its Report at the end of that row.

Previews #

isPreview() and previewed($kind) #

isPreview() is whether an authorised draft preview is being rendered. It is for draft previews only: when staff browse the site in an unpublished theme (Site ▸ Themes), it stays false and comments still print. previewed($kind) is the draft row a preview shows in place of the stored one (page, post, menu or theme_options), or null on every other render. It is for a module's front route; one that does not ask renders as before.

<?php if ($this->isPreview()): ?><p>Draft preview</p><?php endif; ?>

A reviewer can preview a draft page or post on your theme. Core marks the changed top-level blocks of the body with data-draft-change="changed" or "added" and styles them in its own preview.css. Style [data-draft-change] yourself if you want your look. The preview blocks inline scripts and external images, so keep your page working without them.