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.
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.
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?
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;
}