Skip to content

Commit

Permalink
Introduce View::$entity property for entity access
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Mar 26, 2024
1 parent cb95074 commit 826484f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion demos/form/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
$form->onSubmit(static function (Form $form) {
// implement subscribe here

return $form->jsSuccess('Subscribed ' . $form->model->get('email') . ' to newsletter.');
return $form->jsSuccess('Subscribed ' . $form->entity->get('email') . ' to newsletter.');
});

$form->buttonSave->set('Subscribe');
Expand Down
7 changes: 5 additions & 2 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
use Atk4\Ui\Js\JsExpression;
use Atk4\Ui\Js\JsExpressionable;

/**
* @property false $model use $entity property instead
*/
class Form extends View
{
use HookTrait;
Expand Down Expand Up @@ -359,7 +362,7 @@ public function addGroup($title = null)
*/
public function controlFactory(Field $field, $controlSeed = []): Control
{
$this->model->assertIsEntity($field->getOwner());
$this->entity->assertIsEntity($field->getOwner());

$fallbackSeed = [Control\Line::class];

Expand Down Expand Up @@ -394,7 +397,7 @@ public function controlFactory(Field $field, $controlSeed = []): Control

$defaults = [
'form' => $this,
'entityField' => new EntityFieldPair($this->model, $field->shortName),
'entityField' => new EntityFieldPair($this->entity, $field->shortName),
'shortName' => $field->shortName,
];

Expand Down
6 changes: 3 additions & 3 deletions src/Form/AbstractLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ protected function _addControl(Control $control, Field $field): Control
*/
public function addControl(string $name, $control = [], array $fieldSeed = []): Control
{
if ($this->form->model === null) {
$this->form->model = (new ProxyModel())->createEntity();
if ($this->form->entity === null) {
$this->form->entity = (new ProxyModel())->createEntity();
}
$model = $this->form->model->getModel();
$model = $this->form->entity->getModel();

// TODO this class should not refer to any specific form control
$controlClass = is_object($control)
Expand Down
9 changes: 8 additions & 1 deletion src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class View extends AbstractView
protected array $_jsActions = [];

public ?Model $model = null;
public ?Model $entity = null;

/**
* Name of the region in the parent's template where this object will output itself.
Expand Down Expand Up @@ -118,7 +119,13 @@ public function setModel(Model $model): void
throw new Exception('Different model is already set');
}

$this->model = $model;
if ($model->isEntity()) {
unset($this->{'model'});
$this->entity = $model;
} else {
unset($this->{'entity'});
$this->model = $model;
}
}

/**
Expand Down

0 comments on commit 826484f

Please sign in to comment.