October: Relation renderer repeating itself on the model

Created on 2 Nov 2019  路  3Comments  路  Source: octobercms/october

Hey Guys,

I have extended the Graker.PhotoAlbums using relation renderer to add support for my plugin. Strangely, when I click on the record, the relation is rendered on the popup too . Following are some screenshots

Screenshot 2019-11-02 at 7 30 29 PM
Screenshot 2019-11-02 at 7 30 48 PM
Screenshot 2019-11-02 at 7 40 56 PM

Added the following code in my Plugin.php file

public function boot(){

        //extending the user model on boot to load a relationship of Thoughts with the Photo
        //the reverse relation of this model is established in the model's Thought.php file
        PhotoModel::extend(function ($model) {
            $model->hasMany['thoughts'] = ['Mantiseye\Thoughts\Models\Thought',
                ];
        });

        // Extend all backend form usage
        Event::listen('backend.form.extendFieldsBefore', function ($widget) {
            if (!$widget->getController() instanceof \Graker\PhotoAlbums\Controllers\Photos) {
                return;
            }
            $widget->fields['thoughts'] =
                [
                    'label'     => 'Thoughts',
                    'path'      => '$/mantiseye/thoughts/controllers/thoughts/_fields_thoughts.htm',
                    'type'      => 'partial'
                ];

        });

        PhotoController::extend(function ($controller) {
            $controller->implement[] = 'Backend.Behaviors.RelationController';
            $controller->addDynamicProperty('relationConfig', '$/mantiseye/thoughts/controllers/thoughts/config_relation.yaml');
        });


   }

Kindly guide me how this can be resolved ?

Question

All 3 comments

@Nvironmental the issue stems from your backend.form.extendFieldsBefore listener, specifically the getController call you are using. The relation modal also runs through the same controller, so the listener will skip the return there and re-add the thoughts field.

You will need to add another couple of checks to ensure you're only extending the correct form similar to the getController lines above. For example, you should probably check which model is connected to the form via $widget->model and return if it's not the photo model, or you could do a check to see if the form is not nested via $widget->isNested and return if it's a nested form, and not the main form.

@bennothommo Hi Ben,

Thank you for your inputs. I realised my mistake by extending the widget on to controller rather than the model, $widget->model helped me solve the problem. Also, $widget->isNested helped me put extra check on the model.

You're welcome, @Nvironmental. :)

Was this page helpful?
0 / 5 - 0 ratings