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

Could you please forward me to a good article about how to use Polymorphic relations? After making a few tries it seems the RelationRender fails to render poly relations if I use <?= $this->relationRender('tags'); ?> for example. I've tried to use in fields definition

tags:
        label: Tags
        type: checkboxlist

and checkboxes are rendered but relation data is not saved.

I'm trying to make a tags for the companies and categories :

Company:

public $morphMany = [
    'tags' => ['Max\PluginName\Models\Tag', 'table' => 'max_taggables', 'name'=>'taggable'],
];

Tag:

public $morphTo  = [
    'companies' => ['Max\PluginName\Models\Company', 'table' => 'max_taggables', 'name'=>'taggable'],
];

I would like to acheeve the same relation form as for the belongsToMany relation which works fine with definition like:

tags:
    label: Tags
    view:
        list: ~/plugins/max/pluginname/models/tag/columns.yaml
        toolbarButtons: add|remove
    manage:
        showSearch: true
        recordsPerPage: 10
        list: ~/plugins/acme/cmmp/models/tag/columns.yaml

Last updated

To make this work, you will need to edit the RelationsController.php file in /modules/backend/behaviors/RelationController.php

You will have to add in the morph cases in several places. Looking at the docs, they do not support Polymorphic relations in the backend https://octobercms.com/docs/backend/relations#relation-display

I don't know why, but this is painful.

Thanks for suggestion, but I did it via new FormWidget. I passed needed data to it and I work in my widget class to acheeve logics. relationcontroller stays the same in this case.

Hi Max, any possibility for you to share your code on this widgetform please ? it looks an interesting idea to solve this problem indeed, thanks

Chris , Like I wrote before the RelationController saves the data from form so the only thing was necessary is to display these in a correct way:

<input type="checkbox" id="checkbox_Form-field-Company-tags_'.$tag->id.'" name="Company[tags]['.$tag->id.']" value="'.$tag->id.'" checked="checked">

This form widget is very simple and I had no time to make it flexible for all fields. I have to remake it in the future.

https://memorialservices.lv/tests/formwidgets.rar

But there was a tutorial video on Vimeo not so long time back about how to extend relationController for deeper then 3 levels, I think the same tutorial can be used for this task as well.

Hi Max, thanks for your reply. I cant seem to open the rar file, it keep looping while uncompressing it. I am using a Mac, perhaps that is the reason ? I think the video you are referring to is the one demoing the acme.formist plugin, is that right ? It helps well for the creation of a new morph relation model in a modal window. What I dont see is how to display the morph relation model in the first level form so that the user does not need to click on a button to open and see the morph relation model data. Do you see what I mean ?

Here is the zip: https://memorialservices.lv/tests/formwidgets.zip

I hope it will help

maxDubovsky said:

Here is the zip: https://memorialservices.lv/tests/formwidgets.zip

I hope it will help

Hi max, any chance you can reupload the widget again? Thanks.

daniel.rojas58117 said:

maxDubovsky said:

Here is the zip: https://memorialservices.lv/tests/formwidgets.zip

I hope it will help

Hi max, any chance you can reupload the widget again? Thanks.

Sorry, this was a few years ago. But there should be a doc how to create formwidget (use artisan).

In the formWidget php file I used:

public function render() { $this->prepareVars(); return $this->makePartial('tags'); }

/**
 * Prepares the form widget view data
 */
public function prepareVars()
{
    $this->vars['name'] = $this->formField->getName();
    $this->vars['value'] = $this->getLoadValue();
    $this->vars['model'] = $this->model;
    $this->vars['var'] = 'pased_value';

    $this->vars['model_id'] = $this->model->id;

    if ($company = Company::whereId($this->model->id)->first()){
        $this->vars['alltags'] = $company->tags;
    }else{
        $this->vars['alltags'] = array();
    }
}

thanks a lot for the quick answer!

1-10 of 10

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