This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Akhil
Akhil

Hi I need help in implementing dynamic meta tags for using my component in a plugin. Scene is like this :

        I have developed a plugin named Akhil.Vh1 with a component name "MusicCurrentDetail". 

Now code for component is like this in component/MusicCurrentDetail.php

    <?php namespace Akhil\Vh1\Components;

    use Cms\Classes\ComponentBase;
    use Akhil\Vh1\Models\MusicCurrent as modelMusicCurrent;
    use Config;
    use Url;

    class MusicCurrentDetail extends ComponentBase
    {
        public $post;

        public function componentDetails()
        {
            return [
                'name'        => 'Music Current Detail Component',
                'description' => 'Music Current Detail Front End Component'
            ];
        }

        public function defineProperties()
        {
            return [
                'idParam' => [
                    'title'       => 'Slug param name',
                    'description' => 'The URL route parameter used for looking up the post by its slug.',
                    'default'     => ':post_id',
                    'type'        => 'string'
                ],
            ];
        }

        public function onRun()
        {
            $this->post = $this->page['post'] = $this->loadPost();
        }

        protected function loadPost()
        {
            $alias = $this->propertyOrParam('idParam');
            $post = modelMusicCurrent::isPublished()->where('alias', '=', $alias)->first();
            if($post){
                if($post->id){
                    //
                    $this->page['site_name'] = Config::get('app.siteName');
                    $this->meta_title = $this->page['meta_title'] = $post->page_title;
                    $this->page['meta_description'] = $post->meta_description;
                    $this->page['description'] = $post->meta_keywords;
                    $this->page['og_title'] = $post->page_title;
                    $this->page['og_description'] = $post->meta_description;
                    $this->page['og_image'] = $this->themeUrl('assets/images/logo_vh1.png');
                    $this->page['og_type'] = 'website';
                    $this->page['base_url'] = URL::to('/');

                    $params = array('post_id' => $alias);#
                    $absolute = true;#
                    $this->page['og_url'] = $this->pageUrl('music/music-current-id', $params, true, $absolute);
                    $this->page['base_url'] = URL::to('/');
                    //
                    $post->prev = $this->loadPrevious($post->id);
                    $post->next = $this->loadNext($post->id);
                    $post->realtedStories = modelMusicCurrent::listFrontEnd([
                                                'search'       => $post->name
                                            ]);
                }
            }
            return $post;
        }

        protected function loadPrevious($currentId){
            $prev = modelMusicCurrent::isPublished()->where('id', '<', $currentId)->orderBy('id', 'DESC')->first();
            return $prev;
        }

        protected function loadNext($currentId){
            $next = modelMusicCurrent::isPublished()->where('id', '>', $currentId)->orderBy('id', 'DESC')->first();
            return $next;
        }

    }

Here i have initialized the meta tag page variables but is not reflecting in pages or layaouts.

{{ this.page.meta_title }} | {{ this.page.site_name }}
Daniel81
Daniel81

For your "meta_title" try {{ meta_title }} or {{ __SELF__.meta_title }} as you've assigned the variable for both, and for "site_name" try {{ site_name }}

Check the screencast out here for more information on components & variable scoping

Last updated

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.