Skip to content

Commit

Permalink
unify with atk4/data impl
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 12, 2025
1 parent 4d30032 commit 09e6d34
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/Form/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Atk4\Core\Factory;
use Atk4\Data\Field;
use Atk4\Data\Reference\HasOneSql;
use Atk4\Ui\Button;
use Atk4\Ui\Header;
use Atk4\Ui\HtmlTemplate;
Expand Down Expand Up @@ -121,6 +122,23 @@ public function addSubLayout($seed = [self::class], $addDivider = true)
return $v;
}

private function getEntityFieldCaptionWithoutReferenceSuffix(Control $control): string
{
$field = $control->entityField->getField();
$caption = $field->getCaption();

if ($field->hasReference()) {
$ref = $field->getReference();
if ($ref instanceof HasOneSql) {
$analysingTheirModel = $ref->createAnalysingTheirModel();

return \Closure::bind(static fn () => $ref->getOurFieldCaptionWithoutReferenceSuffix($analysingTheirModel), null, HasOneSql::class)();
}
}

return preg_replace('~ ID$~i', '', $caption);
}

#[\Override]
protected function recursiveRender(): void
{
Expand Down Expand Up @@ -171,10 +189,9 @@ protected function recursiveRender(): void
$template = $element->renderLabel ? $labeledControl : $noLabelControl;
$label = $element->caption;
if ($label === null) {
$label = $element->entityField->getField()->getCaption();
if (property_exists($element, 'model')) {
$label = preg_replace('~ ID$~i', '', $label);
}
$label = property_exists($element, 'model')
? $this->getEntityFieldCaptionWithoutReferenceSuffix($element)
: $element->entityField->getField()->getCaption();
}

// anything but form controls gets inserted directly
Expand Down

0 comments on commit 09e6d34

Please sign in to comment.