Exemplo n.º 1
0
 public function boot()
 {
     // Extend the navigation
     Event::listen('backend.menu.extendItems', function ($manager) {
         $manager->addSideMenuItems('RainLab.Blog', 'blog', ['tags' => ['label' => 'Tags', 'icon' => 'icon-tags', 'code' => 'tags', 'owner' => 'RainLab.Blog', 'url' => Backend::url('bedard/blogtags/tags')]]);
     });
     // Extend the controller
     PostsController::extendFormFields(function ($form, $model, $context) {
         if (!$model instanceof PostModel) {
             return;
         }
         $form->addSecondaryTabFields(['tagbox' => ['label' => 'Tags', 'tab' => 'rainlab.blog::lang.post.tab_categories', 'type' => 'owl-tagbox', 'slugify' => true]]);
     });
     // Extend the model
     PostModel::extend(function ($model) {
         // Relationship
         $model->belongsToMany['tags'] = ['Bedard\\BlogTags\\Models\\Tag', 'table' => 'bedard_blogtags_post_tag', 'order' => 'name'];
         // getTagboxAttribute()
         $model->addDynamicMethod('getTagboxAttribute', function () use($model) {
             return $model->tags()->lists('name');
         });
         // setTagboxAttribute()
         $model->addDynamicMethod('setTagboxAttribute', function ($tags) use($model) {
             $this->tags = $tags;
         });
     });
     // Attach tags to model
     PostModel::saved(function ($model) {
         if ($this->tags) {
             $ids = [];
             foreach ($this->tags as $name) {
                 $create = Tag::firstOrCreate(['name' => $name]);
                 $ids[] = $create->id;
             }
             $model->tags()->sync($ids);
         }
     });
 }