Start with clean Joomla 5.1.2 installation, load attached com_stars (fails).zip
Navigate to /component/stars/?view=planets
Click on New button
Form allowing you to add a new planet as happens when installing attached com_star (works).zip
Page /component/stars/?view=planet&layout=edit is loaded, throws exception as per issue title
Standard Joomla 5.1.2 installation
PHP version 8.2
The difference can be found in site/src/Controller/PlanetController.php
In the 'works' version I added an override for getRedirectToItemAppend function found in libraries/src/MVC/Controller/FormController.php
The difference is that the override adds 'id=0' if called without arguments, as done in the add function in libraries/srcMVC/Controller/FormController.php
I noticed getRedirectToItemAppend() is also used in administrator/components/com_menus/src/Controller/ItemController.php and components/com_content/src/Controller/ArticleController.php.
As a test I changed the function behaviour in libraries/src/MVC/Controller/FormController.php:
Replace lines 486 - 488:
if ($recordId) {
$append .= '&' . $urlVar . '=' . $recordId;
}
With:
$append .= '&' . $urlVar . '=' . (int) $recordId;
I did not notice any adverse effects of this change in the rest of the system, but then I did not do a full regression test....
Finally, I'm not sure adding the id=0 is addressing the root cause of the exception or only a work-around?
Looking forward to your feedback.
Kind regards, Onno
Labels |
Added:
No Code Attached Yet
|
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2024-08-01 17:31:52 |
Closed_By | ⇒ | Quy |
The problem in your loadFormData() method:
protected function loadFormData() { $app = Factory::getApplication(); $data = $app->getUserState('com_stars.edit.planet.data', []); if (empty($data)) { $data = $this->getItem(); } return $data; }
The method always should return array. However in your case it return
false
for new item, because$data = $this->getItem();
returnfalse
when item not found.
Thanks Fedik, returning an array shows the edit form.
But I'm not out of the woods yet: Saving the form does not save the data: the id is set to 1...
What array should I return? I tried array(), array('id'=>0): id is stuck on 1...
Saving the form does not save the data: the id is set to 1
You probably missing something, I did not checked whole your component.
You also can try to ask for help on forum https://forum.joomla.org/viewforum.php?f=833
What array should I return?
The loadFormData()
should return saved data for editing, or empty data for new item.
Btw, your component works,
For creating a new item, always open it as
/component/stars/?view=planet&layout=edit
When you have
/component/stars/?view=planet&layout=edit&id=1
you will edit item with ID 1
The problem in your loadFormData() method:
The method always should return array.
However in your case it return
false
for new item, because$data = $this->getItem();
returnfalse
when item not found.