Display a list of records inside a record without a relation

Hello,

I try to display a list of records inside a record. While this would be easily possible with having a fixed relation somehow, the problem here is that there’s no relation.

The list should simply show records with a similar name, so administrators can compare them to the actual record.

I tried two approaches already:

  • Relation: I couldnt find a relation which fits here
  • Recordfinder field: The recordfindet might do the job, but actually I dont wanna save those records.
    Sure I could probably use “_myfield” so the data isnt saved, but a list would still be the better approch.

What would be the best way to realize this?

The rough direction should hopefully do it :wink:

Thanks a lot. ^^

Hey @LordRazen

This is a common issue and I try my best to explain it in this video:

You should not be afraid to design your controls using the controller and partials. The in-built widgets and behaviors are there to solve standard requirements, but when going outside the box, they are limited, and a simple MVC pattern is all you need.

1 Like

Uoh didnt think of those videos.

I started the work and could almost finish it!

I registered this method in the Controller:
MyModels.php

    public function __construct()
    {
        $this->createRelatedModelListWidget();
    }

This is the method inside the controller:

    public function createRelatedModelListWidget()
    {
        # Create Config
        $config        = $this->makeConfig('$/mch/myplugin/models/mymodel/columns_recordfinder.yaml');
        $config->model = new MyModel;

        # Create List Widget with this config and bind it to the controller
        $widget = $this->makeWidget('Backend\Widgets\Lists', $config);
        $widget->setSearchTerm('MySearchterm');
        $widget->bindToController();

        # Save it to vars, so it's accessible within the view / partial
        $this->vars['relatedModelListWidget'] =  $widget;
    }

And I call the widget within a single record backend view:

    <?= $relatedModelListWidget->render() ?>

Works so far. Just one problem is still open:
The searchterm should be the name of the model. But those methods are all in the controller. How can I pass the Models name as a searchterm in the controller?

And pagination would also nice to have :smiley:

Found the answer to my questions for “use the name of the model as search term” and “add pagination” to this custom list:

    public function createRelatedModelListWidget()
    {
        # Model
        if (!isset($this->params[0])) return;
        $model = MyModel::find($this->params[0]);

        # Create Config
        $config        = $this->makeConfig('$/mch/myplugin/models/mymodel/columns_recordfinder.yaml');
        $config->model = new MyModel;
        $config->recordUrl = 'mch/myplugin/mymodel/update/:id';
        $config->recordsPerPage = 10;

        # Create List Widget with this config and bind it to the controller
        $widget = $this->makeWidget('Backend\Widgets\Lists', $config);
        $widget->setSearchTerm($model->name);
        $widget->bindToController();

        # Save it to vars, so it's accessible within the view / partial
        $this->vars['relatedModelListWidget'] =  $widget;
    }

Hope this helps someone at a later point. ^^

1 Like

Got another question on this issue: What if I would like to remove the open record from the list / add a scope to this list?

I couldnt find any method to do so, but I’m sure I overlooked something.