Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set WidgetId and ElementID in form action url #3

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions src/Controller/UserFormWidgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ class UserFormWidgetController extends UserDefinedFormController
'Form',
];

private ?UserFormWidget $widget;
private ?ElementWidget $element;
private static array $url_handlers = [
'$WidgetId/$ElementID/Form' => 'Form',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use capital ID for consistency

];

private ?UserFormWidget $widget = null;
private ?ElementWidget $element = null;

public function __construct($dataRecord = null)
{
$this->widget = $dataRecord;

Requirements::javascript('//code.jquery.com/jquery-3.6.0.min.js');
Requirements::javascript(
'silverstripe/userforms:client/dist/js/jquery-validation/jquery.validate.min.js'
Expand All @@ -45,23 +51,40 @@ public function __construct($dataRecord = null)
Requirements::javascript('silverstripe/userforms:client/dist/js/userforms.js');

if (array_key_exists('WidgetID', $_POST)) {
$dataRecord = UserFormWidget::get()->byID($_POST['WidgetID']);
$this->widget = UserFormWidget::get()->byID($_POST['WidgetID']);
}

$this->widget = $dataRecord;

if (array_key_exists('ElementID', $_POST)) {
$this->element = ElementWidget::get()->byID($_POST['ElementID']);
} else {
$this->element = $this->widget->getElement();
$this->element = $this->widget?->getElement();
}

if (!$this->widget) {
$this->setWidgetAndElementFromUrl($_SERVER['REQUEST_URI']);
}

parent::__construct($dataRecord);
parent::__construct($this->widget);
}

private function setWidgetAndElementFromUrl(string $url): void
{
$requestParts = explode('/', $url);
if (is_int($urlSegmentIndex = array_search(self::$url_segment, $requestParts))) {
$widgetId = $requestParts[$urlSegmentIndex+1];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use capital ID for consistency

$elementId = $requestParts[$widgetId+1];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use capital ID for consistency

$this->widget = UserFormWidget::get()->byID($widgetId);
$this->element = ElementWidget::get()->byID($elementId);
}
}

public function Link($action = null)
{
return Controller::join_links(Director::baseURL(), self::$url_segment, $action);
if (!$this->widget || !$this->element) {
$this->setWidgetAndElementFromUrl($this->getRequest()->getURL());
}

return Controller::join_links(Director::baseURL(), self::$url_segment, $this->widget->ID, $this->element->ID, $action);
}

public function Form()
Expand All @@ -72,6 +95,10 @@ public function Form()
$page = $this->element->getPage();
$form->Fields()->push(HiddenField::create('_PageUrl', '_PageUrl', $page->Link()));

if (!$this->widget) {
$this->widget = UserFormWidget::get()->byID($this->getRequest()->param('WidgetID'));
}

if ($this->widget) {
$form->Fields()->push(HiddenField::create('WidgetID', 'WidgetID', $this->widget->ID));
}
Expand Down