Use media manager for blog post featured image
13
By default, blog posts enable you to use a featured image, but only if you upload your image directly. If you're looking to use the Media Manager for your featured images, all it requires is making a small plugin.
<?php
namespace Acme\BlogBanner;
use System\Classes\PluginBase;
use Event;
/**
* Class Plugin
*
* @package Acme\BlogBanner
*/
class Plugin extends PluginBase
{
public $require = ['RainLab.Blog'];
/**
* @inheritdoc
*/
public function pluginDetails(): array
{
return [
'name' => 'Blog Banner',
'description' => 'A RainLab.Blog extension adding banner image via Media Manager to posts.',
'author' => 'Acme',
'icon' => 'icon-file-image-o',
];
}
public function register()
{
Event::listen('backend.form.extendFields', function (\Backend\Widgets\Form $formWidget) {
if (!$formWidget->getController() instanceof \RainLab\Blog\Controllers\Posts) {
return;
}
if (!$formWidget->model instanceof \RainLab\Blog\Models\Post) {
return;
}
$formWidget->addSecondaryTabFields([
'metadata[banner_image]' => [
'tab' => 'rainlab.blog::lang.post.tab_manage',
'label' => 'Banner Image',
'type' => 'mediafinder',
'mode' => 'image'
],
]);
$formWidget->removeField('featured_images');
});
}
}
Here, we call it banner_image
instead of featured_image
to avoid any conflicts, but that's all you need to enable the Media Manger for blog posts.
To use the image in the CMS pages, just use:
<img src="{{ post.metadata.banner_image|media }}">
There are no comments yet
Be the first one to comment