I’m making a seo plugin and I’m also using the rainlab blog plugin, I have created a tab with fields where I can input the metas that I want to include in the head tag of the page but I don’t know how to call the value from the form fields that are in the blog area (rainlab plugin menu tab) while my seo component is at a cms page I have tried to use the {{this.page.variable}}’method but since the input form is at another backend page it doesn’t work.
This is how my plugin.php looks like:
<?php namespace StronganswerSeo;
use Backend;
use Event;
use SystemClassesPluginBase;
use CmsClassesPage;
use CmsClassesTheme;
use SystemClassesPluginManager;
use SystemClassesSettingsManager;
class Plugin extends PluginBase
{
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'seo',
'description' => 'meta and og tag handler',
'author' => 'stronganswer',
'icon' => 'icon-leaf'
];
}
/**
* Registers any front-end components implemented in this plugin.
*
* @return array
*/
public function registerComponents()
{
return [
'StronganswerSeoComponentsSeo' => 'seoHandler',
'StronganswerSeoComponentsBlogPost' => 'SeoBlogPost',
'StronganswerSeoComponentsStaticPage' => 'SeoStaticPage',
'StronganswerSeoComponentsCmsPage' => 'SeoCmsPage',
];
}
public function registerSettings(){
return [
'settings' => [
'label' => 'SEO Settings',
'description' => 'Seo Settings.',
'icon' => 'icon-bar-chart-o',
'class' => 'stronganswerseoModelssettings',
'context' => 'mysettings',
'category' => SettingsManager::CATEGORY_MYSETTINGS,
'order' => 1
]
];
}
/**
* Registers any back-end permissions used by this plugin.
*
* @return array
*/
public function registerPermissions()
{
return [
'stronganswer.seo.some_permission' => [
'tab' => 'seo',
'label' => 'Some permission'
],
];
}
/**
* Registers back-end navigation items for this plugin.
*
* @return array
*/
/* public function registerNavigation()
{
return [
'seo' => [
'label' => 'seo',
'url' => Backend::url('stronganswer/seo/controllers'),
'icon' => 'icon-leaf',
'permissions' => ['stronganswer.seo.*'],
'order' => 500,
],
];
}*/
public function boot()
{
Event::listen('backend.form.extendFields', function($widget)
{
if(PluginManager::instance()->hasPlugin('RainLab.Pages') && $widget->model instanceof RainLabPagesClassesPage)
{ //static pages fields
$widget->addFields([
'viewBag[seo_title]' =>[
'label' => 'Meta Title',
'tab' => 'SEO',
'type' => 'text'
],
'viewBag[seo_description]' =>[
'label' => 'Meta Description',
'tab' => 'SEO',
'size' => 'tiny',
'type' => 'textarea'
],
'viewBag[seo_keywords]' =>[
'label' => 'Meta Keywords',
'tab' => 'SEO',
'type' => 'text'
],
'viewBag[robot_index]' => [
'label' => 'Robot Index',
'type' => 'dropdown',
'tab' => 'SEO',
'options' => ["index"=>"index","noindex"=>"noindex"],
'default' => 'index',
'span' => 'left'
],
'viewBag[robot_follow]' => [
'label' => 'Robot Follow',
'type' => 'dropdown',
'tab' => 'SEO',
'options' => ["follow"=>"follow","nofollow"=>"nofollow"],
'default' => 'follow',
'span' => 'right'
]
], 'primary');
}
if(PluginManager::instance()->hasPlugin('RainLab.Blog') && $widget->model instanceof RainLabBlogModelsPost)
{
$widget->addFields([
'blog_title' =>[
'label' => 'Meta Title',
'tab' => 'Blog Seo',
'type' => 'text'
],
'seo_description' =>[
'label' => 'Meta Description',
'tab' => 'Blog Seo',
'size' => 'tiny',
'type' => 'textarea'
],
'seo_keywords' =>[
'label' => 'Meta Keywords',
'tab' => 'Blog Seo',
'type' => 'text'
],
'robot_index' => [
'label' => 'Robot Index',
'type' => 'dropdown',
'tab' => 'Blog Seo',
'options' => ["index"=>"index","noindex"=>"noindex"],
'default' => 'index',
'span' => 'left'
],
'canonical_url' => [
'label' => 'Canonical URL',
'type' => 'text',
'tab' => 'SEO',
'span' => 'left'
],
'robot_follow' => [
'label' => 'Robot Follow',
'type' => 'dropdown',
'tab' => 'Blog Seo',
'options' => ["follow"=>"follow","nofollow"=>"nofollow"],
'default' => 'follow',
'span' => 'right'
]
], 'secondary');
}
if (!$widget->model instanceof CmsClassesPage) return;
//cms page fields
$widget->addFields([
'settings[seo_title]' =>[
'label' => 'Meta Title',
'tab' => 'SEO',
'type' => 'text'
],
'settings[seo_description]' =>[
'label' => 'Meta Description',
'tab' => 'SEO',
'size' => 'tiny',
'type' => 'textarea'
],
'settings[seo_keywords]' =>[
'label' => 'Meta Keywords',
'tab' => 'SEO',
'type' => 'text'
],
'settings[robot_index]' => [
'label' => 'Robot Index',
'type' => 'dropdown',
'tab' => 'SEO',
'options' => ["index"=>"index","noindex"=>"noindex"],
'default' => 'index',
'span' => 'left'
],
'settings[robot_follow]' => [
'label' => 'Robot Follow',
'type' => 'dropdown',
'tab' => 'SEO',
'options' => ["follow"=>"follow","nofollow"=>"nofollow"],
'default' => 'follow',
'span' => 'right'
]
], 'primary');
});
}
}
and this is my component:
<?php namespace StronganswerSeoComponents;
use DB;
use CmsClassesComponentBase;
use StronganswerSeomodelsSettings;
use RainLabPagesClassesRouter;
use RainLabBlogModelsPost;
use CmsClassesTheme;
use CmsClassesPage;
use Request;
use Event;
class BlogPost extends ComponentBase
{
//singular page tags
public $page;
public $blog_title;
public $seo_title;
public $seo_description;
public $seo_keywords;
public $robot_index;
public $robot_follow;
//global tags
public $ogTitle;
public $ogDescription;
public $ogSiteName;
public $ogUrl;
public $ogType;
public $ogAuthor;
public $ogImage;
//facebook tags
public $ogFbAppId;
public $ogFbAdmins;
//google tags
public $ogGlTitle;
public $ogGlDescription;
public $ogGlImage;
//twitter tags
public $ogTtCard;
public $ogTtSite;
public $ogTtTitle;
public $ogTtDescription;
public $ogTtAuthor;
public $ogTtImage;
public function componentDetails()
{
return [
'name' => 'Seo BlogPost component',
'description' => 'handles seo fields into blogposts'
];
}
public function defineProperties()
{
return [
"post" => [
"title" => "data",
"default" => "post"
]
];
}
public function Run()
{
$theme = Theme::getActiveTheme();
$page = Page::load($theme,$this->page->baseFileName);
$this->page["hasBlog"] = true;
if($page->hasComponent("blogPost"))
{
//$seo = DB::table('rainlab_blog_posts')->get();
//$blog_title = DB::table('rainlab_blog_posts')->where('slug','=','first-blog-post')->value('seo_title');
//$this->seo_title = $this->page['blog_title'] = $this->page->getViewBag()->property('blog_title');
//$this->seo_title = $this->page["seo_title"] = $this->page->seo_title;
/*$blog_title = DB::table('rainlab_blog_posts')
->select(DB::raw('select seo_title'))
->where('slug', '=', 'first-blog-post')
->get();*/
$this->seo_description = $this->page["seo_description"] = $this->page->meta_description;
$this->seo_keywords = $this->page["seo_keywords"] = $this->page->seo_keywords;
$this->robot_follow = $this->page["robot_follow"] = $this->page->robot_follow;
$this->robot_index = $this->page["robot_index"] = $this->page->robot_index;
$settings = Settings::instance();
if($settings->enable_og_tags)
{
$this->ogTitle = $settings->og_title;
$this->ogDescription = $settings->og_description;
$this->ogSiteName = $settings->og_sitename;
$this->ogUrl = $settings->og_url;
$this->ogType = $settings->og_type;
$this->ogAuthor = $settings->og_author;
$this->ogImage = $settings->og_img;
}
if($settings->enable_fb_tags)
{
$this->ogFbAppId = $settings->og_fb_appid;
$this->ogFbAdmins = $settings->og_fb_admins;
}
if($settings->enable_ggl_tags)
{
$this->ogGlTitle = $settings->og_gl_title;
$this->ogGlDescription = $settings->og_gl_description;
$this->ogGlImage = $settings->og_gl_img;
}
if($settings->enable_tt_tags)
{
$this->ogTtCard = $settings->og_tt_card;
$this->ogTtSite = $settings->og_tt_site;
$this->ogTtTitle = $settings->og_tt_title;
$this->ogTtDescription = $settings->og_tt_description;
$this->ogTtAuthor = $settings->og_tt_author;
$this->ogTtImage = $settings->og_tt_img;
}
}
else{
$this->hasBlog = $this->page["hasBlog"] = false;
}
}
}
The code within the comments are failed trys to get the values from the form. If I go to my database I can see the values that I inputed in the form but I can’t call them to my component.
and my deafult.htm :
<meta name="title" content="{{__SELF__.blog_title}}">
<meta name="description" content="{{__SELF__.seo_description}}">
<meta name="keywords" content="{{__SELF__.seo_keywords}}">
<meta name="robots" content="{{__SELF__.robot_index}},{{__SELF__.robot_follow}}">
<meta property="og:site_name" content="{{ __SELF__.ogSiteName }}" />
<meta property="og:title" content="{{ __SELF__.ogTitle }}" />
<meta property="og:url" content="{{ __SELF__.ogUrl }}" />
<meta property="og:description" content="{{ __SELF__.ogDescription }}" />
<meta property="og:type" content="{{ __SELF__.ogType }}" />
<meta name="author" content="{{ __SELF__.ogAuthor }}" />
<meta property="og:image" content="{{ __SELF__.ogImage }}" />
<meta property="fb:app_id" content="{{ __SELF__.ogFbAppId }}" />
<meta property="fb:admins" content="{{ __SELF__.ogFbAdmins }}" />
<meta itemprop="name" content="{{ __SELF__.ogGlTitle }}" />
<meta itemprop="description" content="{{ __SELF__.ogGlDescription }}" />
<meta itemprop="image" content="{{ __SELF__.ogGlImage }}" />
<meta itemprop="image" content="{{ __SELF__.ogGlImage }}" />
<meta name="twitter:card" content="{{__SELF__.ogTtCard}}">
<meta name="twitter:site" content="{{__SELF__.ogTtSite}}">
<meta name="twitter:title" content="{{__SELF__.ogTtTitle}}">
<meta name="twitter:description" content="{{__SELF__.ogTtDescription}}">
<meta name="twitter:creator" content="{{__SELF__.ogTtAuthor}}">
<meta name="twitter:image:src" content="{{__SELF__.ogTtImage}}">
I also have forms for cms pages and for the rainlab pages plugin(static pages) I get them from another components that I have made and I wanted to point out that both of them are working fine.
3
Answers
To call the value from the DB I did like this:
Insert this into the component onRun fucntion:
Then on the default.htm:
If you need values from a form use the
Input
classhttps://octobercms.com/docs/services/request-input
But i’m thinking you’re meaning something else, but i’m not sure, because the actual question isn’t clear.
There are a lot of things to consider when extending a plugin you should check in these cases:
dd($somevalue)
the values in my components(dd kills any execution and dumps the variables. very useful to analyze them)To debug october can be a pain when starting out, but once you get the hang of it it’s fun 🙂
i’m also working to understand how to extend field in cmsPages and rainlabPages simultaneoulsy and then use a partial to print fileds in my html source.
So i create this partial:
I think the best solution is: create a plugin that extends fields of cmsPages and rainLab Pages than print with a partial!
Look into the code of seoextension by anandpatel maybe could help but i’m not very strong in coding so we can help each other.
I think the function is:
Maybe we can insert this project to git.
Bye
Gabriele