Whcih i can embed in my pages to render Demo items. I want a shrotcode like this:
[demo id="1"][/demo]
which renders the Demo item with its own partial, like if it was called on a page where the Demo component is embedded. How can i acieve this please? So how can i access/initialize components from the shortcode engine?
Thanks, thats is useful but i mean this: imagine there is a site with Rainlab.Blog plugin, and users want to have a shortcode to dispaly a blogpost on anywhere, like in a static page. How can i create a
If we are talking about rendering from a finished component, then your path is right. The difficulty lies in the fact that some methods in the blog component are private, i.e. they should not be used from code without redefinition. I did not redefine the classes, just changed the code for the blog component a bit. Here is the work code, it can still be improved, but, alas, there is no time for this (it was added to the v1.0.4 engine and published, use:[blog id=1][/blog], demo link for StaticPage: https://shortcodes.test.linkonoid.com/test-shortcode-plugin.html):
<?php namespace Linkonoid\ShortcodesEngine\Classes;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use RainLab\Blog\Components\Post as BlogPostComponent;
use RainLab\Blog\Models\Post as BlogPost;
use Cms\Classes\Controller;
class BlogShortcode extends Shortcode
{
public $postId;
public function __construct($manager)
{
parent::__construct();
$this->manager = $manager;
}
public function init()
{
$this->manager->getHandlers()->add('blog', function(ShortcodeInterface $sc) {
$this->postId = $sc->getParameter('id');
BlogPostComponent::extend(function ($component) {
$component->addDynamicMethod('onRenderNew', function () use($component) {
$component->categoryPage = $component->property('categoryPage');
$post = new BlogPost;
$post = $post->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel')
? $post->transWhere('id', $this->postId)
: $post->where('id', $this->postId);
try {
$component->post = $post->firstOrFail();
} catch (ModelNotFoundException $ex) {
}
$controller = new Controller;
$controller->setComponentContext($component);
/*
if (!$component->checkEditor()) {
$post = $post->isPublished();
}
*/
if ($component->post && $component->post->categories->count()) {
$blogPostsComponent = null;//$component->getComponent('blogPosts', $component->categoryPage);
$component->post->categories->each(function ($category) use ($component,$controller,$blogPostsComponent) {
$category->setUrl($component->categoryPage, $controller, [
//'slug' => $component->urlProperty(null, 'categoryFilter')
]);
});
}
return $controller->renderPartial('@default.htm');
});
});
$blogPostComponent = new BlogPostComponent;
return $blogPostComponent->onRenderNew();
});
}
}