No Code Attached Yet
avatar Kurantje
Kurantje
1 Aug 2024

Steps to reproduce the issue

Start with clean Joomla 5.1.2 installation, load attached com_stars (fails).zip
Navigate to /component/stars/?view=planets
Click on New button

Expected result

Form allowing you to add a new planet as happens when installing attached com_star (works).zip

Actual result

Page /component/stars/?view=planet&layout=edit is loaded, throws exception as per issue title

System information (as much as possible)

Standard Joomla 5.1.2 installation
PHP version 8.2

Additional comments

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

com_stars (fails).zip
com_stars (works).zip

avatar Kurantje Kurantje - open - 1 Aug 2024
avatar joomla-cms-bot joomla-cms-bot - change - 1 Aug 2024
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 1 Aug 2024
avatar Fedik
Fedik - comment - 1 Aug 2024

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(); return false when item not found.

avatar Quy Quy - change - 1 Aug 2024
Status New Closed
Closed_Date 0000-00-00 00:00:00 2024-08-01 17:31:52
Closed_By Quy
avatar Quy Quy - close - 1 Aug 2024
avatar Kurantje
Kurantje - comment - 1 Aug 2024

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(); return false 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...

avatar Fedik
Fedik - comment - 1 Aug 2024

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.

avatar Fedik
Fedik - comment - 2 Aug 2024

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

Add a Comment

Login with GitHub to post a comment