OctoberCMS call another plugin's data in current plugin's dropdown

1.1k views Asked by At

I am new to OctoberCMS and i love the way it works. Currently i have created two plugins called as Products and Product Categories. I have created these plugins using Builder Plugin which is also a very good plugin to create another plugins with ease.

Now the thing is, In my Products Categories plugin, i simply have a single field called as Product Category and user will add as many categories as he/she wants and this plugin works fine.

And in my Products plugin, i have a field called as Product Category which is a dropdown field and i want all those categories which i have created in Product Categories plugins respectively but somehow i am unable to implement this feature. This is what i have tried so far.

Plugin.php (plugins\technobrave\products)

<?php namespace Technobrave\Products;

use System\Classes\PluginBase;
use technobrave\productcategory\Models\ProductCategory as ProductCategory;
use technobrave\products\Models\Product as ProductModel;

class Plugin extends PluginBase
{
    public function registerComponents()
    {
    }

    public function registerSettings()
    {
    }

    public function boot()
    {
        ProductModel::extend(function($model){
            $model->hasOne['ProductCategory'] = ['technobrave\productcategory\Models\ProductCategory'];
        });
    }
}

Product.php (plugins\technobrave\products\models)

<?php namespace Technobrave\Products\Models;

use Model;

/**
 * Model
 */
class Product extends Model
{
    use \October\Rain\Database\Traits\Validation;

    /*
     * Validation
     */
    public $rules = [
    'category' => 'required',
    'product_brand' => 'required',
    'product_channel' => 'required',
    'product_type' => 'required',
    'client_name' => 'required',
    'project_name' => 'required',    
    'product_description' => 'required',
    'banner_image' => 'required',
    'product_images' => 'required',
    ];

    public $customMessages = [
                'category.required' => 'Please Select Product Category',
                'product_brand.required' => 'Please Select Product Brand',
                'product_channel.required' => 'Please Select Product Channel',
                'product_type.required' => 'Please Select Product Type',
                'client_name.required' => 'Please Enter Client Name',
                'project_name.required' => 'Please Enter Project Name',
                'product_short_description.required' => 'Please Enter Product Short Description',
                'product_description.required' => 'Please Enter Product Description',
                'banner_image.required' => 'Please select Product Banner Image',
                'product_images.required' => 'Please select Product Image(s)',


    ];

    /*
     * Disable timestamps by default.
     * Remove this line if timestamps are defined in the database table.
     */
    //public $timestamps = false;

    /**
     * @var string The database table used by the model.
     */
    public $table = 'technobrave_products_';
    public $settingsFields = 'fields.yaml';

    public $attachOne = [
            'banner_image' => 'System\Models\File'
    ];


    public $attachMany = [
            'product_images' => 'System\Models\File'
    ];

    public $belongsTo = [
        'ProductCategory'=> ['technobrave\productcategory\Models\ProductCategory']
    ];

    // Here i want my product dropdown categories to be dynamic 
    public function getCategoryOptions()
    { 
        echo '<pre>';
        print_r($ProductCategory);
        exit;
        //return array();
    }

}

But i am keep getting fatal error message saying:

Undefined variable: ProductCategory

For this particular code which i put in Product.php

echo '<pre>';
print_r($ProductCategory);
exit;

in above Plugin.php file i have below code

use technobrave\productcategory\Models\ProductCategory as ProductCategory;

so by doing this, i am trying to get all the categories which i have already created and somehow trying to show it in my dropdown.

I know in OctoberCMS while creating plugins, we can deal with relations logic (i.e. hasMany, hasOne etc) but for now i want to achieve in this way with external plugins. I want add those categories to be filled in the method getCategoryOptions() and i will return those in my dropdown.

Can someone guide me if its possible in the way which i want ?

Thanks

1

There are 1 answers

0
Mittul At TechnoBrave On BEST ANSWER

Ok. I have found out two ways to be able to achieve this and here below are those.

Way One

Product.php (plugins\technobrave\products\models)

<?php namespace Technobrave\Products\Models;

use Model;
use technobrave\productcategory\Models\ProductCategory as ProductCategory;

public function getCategoryOptions()
    {      
        $fields =  ProductCategory::lists('category_name','id');         
        print_r($fields);
    }

Here above, i have just used use technobrave\productcategory\Models\ProductCategory as ProductCategory; and in my method getCategoryOptions(), i have just added this ProductCategory::lists('category_name','id'); and returned it to be able to fill dynamic categories in my dropdown. This works well.

Way Two

Product.php (plugins\technobrave\products\models)

<?php namespace Technobrave\Products\Models;

use Model;
use technobrave\productcategory\Models\ProductCategory as ProductCategory;

    public function getCategoryOptions()
    { 
        // getting only active categories
       $get_categories = ProductCategory::all()->where('status',1);    

       $fields[''] = 'Select Product Category';
       foreach ($get_categories as $current_category) {
            $fields[$current_category->attributes['id']] = $current_category->attributes['category_name'];
       }
       print_r($fields);


    }

Here above, i simply written a query in my method getCategoryOptions() and got the records.

You can use whichever method which you prefer. Additionally, it would be great if i find better ways to implement the same thing.

Thanks