rainlab/blog-plugin

Preview blog button

Closed this issue · 4 comments

Hi, in this update #316 already have preview blog feature before publishing, but at the backend there is no button to redirect to review page, how can I add preview button to automatically redirect to preview page?

Thanks!

Why the preview button is hidden? I check in _post_toolbar.htm and there I can see the preview button is will always hidden because of this code class="btn btn-primary oc-icon-crosshairs <?php if (!false): ?>hide<?php endif ?>" ?

Hi @mikysetiawan

This was a planned feature that never materialized, but don't worry, this feature is included in the release of Tailor. For now, you just need to navigate to the blog page by hand to preview posts.

I hope this helps!

Hi @daftspunk.

The 'Preview' button is now visible (I'm using OctoberCMS v3.0.67). I guess because the hide class has been renamed into hidden, or something like that?

Anyways, I've seen this button and after desperately searching for the reason behind the missing link in the source code, I added this functionality on my own (within my plugin). I overrode the _post_toolbar.htm file and added a few lines on top of my own version.

Just in case someone wanna use / offer this button for his customers as well:

In your Plugin file (change the path):

\RainLab\Blog\Controllers\Posts::extendFormFields(function ($form) {
    $toolbar = $form->getFields()['toolbar'];
    $toolbar->useConfig([
        'path' => '$/ratmd/bloghub/controllers/posts/_post_toolbar.htm'
    ]);
});

Replacement for the top of the _post_toolbar.htm file (The \RatMD\BlogHub\Models\Settings settings part must be replaced, of course. I'm not quite sure how I can fetch the post page path directly tbh, thus I store them separately in my own configuration).

<?php
    $isCreate = $this->formGetContext() == 'create';
    $pageUrl = isset($pageUrl) ? $pageUrl : null;

    if (empty($pageUrl) && !empty($this->vars['formModel'])) {
        $post = $this->vars['formModel'];

        if (!empty($post->slug)) {
            $ctrl = new \Cms\Classes\Controller(\Cms\Classes\Theme::getActiveTheme());
            $post->setUrl(\RatMD\BlogHub\Models\Settings::get('preview_post_page'), $ctrl);
            $pageUrl = $post->url;
        }
    }
?>

and of course, the changed button:

    <!-- Preview -->
    <a
        href="<?= URL::to($pageUrl) ?>"
        target="_blank"
        class="btn btn-primary oc-icon-crosshairs <?php if (empty($pageUrl)): ?>hidden<?php endif ?>"
        data-control="preview-button">
            <?= e(trans('rainlab.blog::lang.blog.preview')) ?>
    </a>

only tested in OctoberCMS v3.

~Sam

Hey Sam,

Thanks for this. We've patched it in 0bf84f1

You are welcome to just submit this as a PR, selecting the Preview Post using the settings page is a simple solution to this.