All Projects → damianlewis → oc-sortablerelations-plugin

damianlewis / oc-sortablerelations-plugin

Licence: MIT license
Adds drag and drop sorting functionality to the view list of the relations controller in October CMS

Programming Languages

javascript
184084 projects - #8 most used programming language
PHP
23972 projects - #3 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to oc-sortablerelations-plugin

oc-api-plugin
Tools for building RESTful HTTP + JSON APIs for OctoberCMS.
Stars: ✭ 28 (+75%)
Mutual labels:  octobercms, octobercms-plugin
oc-gdpr-plugin
October CMS plugin to make websites GDPR and ePrivacy compliant
Stars: ✭ 32 (+100%)
Mutual labels:  octobercms, octobercms-plugin
contenteditor-plugin
Edit your content in page - plugin for OctoberCMS
Stars: ✭ 32 (+100%)
Mutual labels:  octobercms, octobercms-plugin
oc-speedy-plugin
Website optimization plugin for October CMS
Stars: ✭ 18 (+12.5%)
Mutual labels:  octobercms, octobercms-plugin
oc-site-search-plugin
Adds global search capabilities to October CMS.
Stars: ✭ 39 (+143.75%)
Mutual labels:  octobercms, octobercms-plugin
octobercms-backendskin
Backend Skin Plugin For OctoberCms
Stars: ✭ 21 (+31.25%)
Mutual labels:  octobercms, octobercms-plugin
oc-responsive-images-plugin
Adds reponsive images capabilities to October CMS
Stars: ✭ 37 (+131.25%)
Mutual labels:  octobercms, octobercms-plugin
oc-backup-plugin
Backup system for October CMS
Stars: ✭ 18 (+12.5%)
Mutual labels:  octobercms, octobercms-plugin
Docktober
🍂 Simple: Docker + OctoberCMS
Stars: ✭ 57 (+256.25%)
Mutual labels:  octobercms
oc-bootstrapper
Easily bootstrap a new October CMS project
Stars: ✭ 86 (+437.5%)
Mutual labels:  octobercms
oc-mall-theme
Demo theme for the oc-mall-plugin
Stars: ✭ 19 (+18.75%)
Mutual labels:  octobercms
Rutorika Sortable
Adds sortable behavior to Laravel Eloquent models
Stars: ✭ 241 (+1406.25%)
Mutual labels:  sortable
oc-good-news-plugin
📰 News (blogging) plugin for October CMS
Stars: ✭ 23 (+43.75%)
Mutual labels:  octobercms
oc-api-plugin
Base API Plugin for OctoberCMS
Stars: ✭ 26 (+62.5%)
Mutual labels:  octobercms
awesome-october-cms
A curated list of awesome things related to OctoberCMS.
Stars: ✭ 19 (+18.75%)
Mutual labels:  octobercms
debugbar-plugin
Integrates PHP Debugbar with October CMS
Stars: ✭ 36 (+125%)
Mutual labels:  octobercms
oc-menumanager-plugin
A Menu Management Plugin for October CMS
Stars: ✭ 29 (+81.25%)
Mutual labels:  octobercms
October
Self-hosted CMS platform based on the Laravel PHP Framework.
Stars: ✭ 10,740 (+67025%)
Mutual labels:  octobercms
sveltober
Cybernetically enhanced October applications
Stars: ✭ 19 (+18.75%)
Mutual labels:  octobercms
ngx-sortable
ngx-sortable is an angular sortable list components that support drag and drop sorting
Stars: ✭ 19 (+18.75%)
Mutual labels:  sortable

Sortable relations plugin for October

Adds drag and drop sorting functionality to the view list of the relations controller in October.

Usage

The following example illustrates how to use this plugin. It shows a many-to-many relationship between a category and it's related products.

Add a relation_sort_order field to the pivot database table.

class CreateCategoryProductTable extends Migration
{
    public function up()
    {
        Schema::create('acme_plugin_category_product', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->integer('category_id')->unsigned();
            $table->integer('product_id')->unsigned();
            $table->integer('relation_sort_order')->unsigned();
            $table->primary(['category_id', 'product_id'], 'category_product_primary');
        });
    }

    ...
}

Use the DamianLewis\SortableRelations\Traits\SortableRelations trait within the parent model of the relation. Add the sortable relation to the $sortableRelations array and add the 'relation_sort_order' attribute to the pivot data array.

class Category extends Model
{
    use DamianLewis\SortableRelations\Traits\SortableRelations;

    public $table = 'acme_plugin_categories';

    public $belongsToMany = [
        'products' => [
            'Acme\Plugin\Models\Product',
            'table' => 'acme_plugin_category_product',
            'pivot' => ['relation_sort_order']
        ]
    ];

    public $sortableRelations = [
        'products'
    ];
}

Implement the DamianLewis\SortableRelations\Behaviors\SortableRelations behavior within the parent controller of the relation and define the $sortableRelationConfig configuration file. This file should define the model class and the name of the sortable relation.

class Categories extends Controller
{
    public $implement = [
        'Backend.Behaviors.FormController',
        'Backend.Behaviors.ListController',
        'Backend.Behaviors.RelationController',
        'DamianLewis.SortableRelations.Behaviors.SortableRelations'
    ];

    public $formConfig = 'config_form.yaml';
    public $listConfig = 'config_list.yaml';
    public $relationConfig = 'config_relation.yaml';
    public $sortableRelationConfig = 'config_sortable_relation.yaml';

    ...
}

Example sortable relations configuration file config_sortable_relation.yaml:

modelClass: Acme\Plugin\Models\Category
relationName: products

Make sure the pivot[sort_order] column has been included in the view.list configuration for the relation controller.

Example relations configuration file config_relation.yaml:

products:
    label: Product
    view:
        list:
            columns:
                title:
                    label: Title
                    type: partial
                pivot[relation_sort_order]:
                    label: Order
                    type: number
                    invisible: true
    manage:
        list: $/acme/plugin/models/product/columns.yaml
        form: $/acme/plugin/models/product/fields.yaml

Lastly, two hidden input fields need to be added to the table rows. One for the parent model ID and the other for the related model ID. This can be accomplished by using a column partial type. The input field for the parent model should include a Lists-relationViewList-parent-id id attribute with a value equal to the parent model ID. The input field for the related model should include a Lists-relationViewList-related-id id attribute with a value equal to the related model ID.

Example partial file for the title column:

<input type="hidden" id="<?= 'Lists-relationViewList-parent-id-'.$record->pivot->category_id ?>" value="<?= $record->pivot->category_id ?>">
<input type="hidden" id="<?= 'Lists-relationViewList-related-id-'.$record->pivot->product_id ?>" value="<?= $record->pivot->product_id ?>">
<?= $value ?>

Note

If extending the relation configuration for the parent controller, make sure to include a call to the 'SortableRelations' relationExtendConfig method as shown in the following example.

class Categories extends Controller
{
    ...
    
    public function relationExtendConfig($config, $field, $model)
    {
        $this->asExtension('SortableRelations')->relationExtendConfig($config, $field, $model);
    }
    
    ...
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].