1

Initial situation:

I have implemented the J4 ordering function in different views of a component. It works fine so far, although the component was not really rewritten for J4. .

Problem:

When I change the order of the main items, it correctly changes the ordering values in the column "ordering", but at the same time it deletes all connected custom fields (not the Joomla Custom fields, but its own kind of fields of the component).

My thoughts:

I figured out, that at the same time of storing the new ordering, a MySQL delete-query is running which is responsible for deleting all the custom fields. When I comment this query out, ordering works without losing the custom fields, but the custom fields are no longer stored at Item Edit.

I guess the logic is: when saving the Item in Edit-View, all old custom fields are deleted and saved new into the database, if any input for them is given. So they are not updated but completely deleted and newly stored. And because there are no field inputs given when ordering in the Items view, only the process of deleting is executed. Is that possible?

Solution? Does it make sense to do a work-around by surrounding the Delete-Query with an if-condition, only to work when in Item-Edit-View? If yes, how? What would be the condition statement to make sure it's only in the Edit-Viewm?

The Class with the Delete-Query:

class JCTableFieldInItem extends JTable
{
    public function delete($pk = null, $fields_id = false, $extension = false)
    {     
        $query = "DELETE a.*
                  FROM #__content_fields AS a 
                  INNER JOIN #__cddir_fields AS b ON a.fields_id = b.id 
                  WHERE a.content_id=".$pk." AND b.extension='" . $extension . "'";
        $this->_db->setQuery($query);

        // Check for a database error.
        $this->_db->execute();
        return true;
    }

1 Answer 1

1

Did you develop the ordering functionality along the lines of the MVC Component Tutorial Adding Ordering step? (The solution for Joomla 4 is very similar, but Joomla components use the draggable javascript library instead of the sortable one.)

If so when you change the order the javascript should send an Ajax message to the service with the task variable set to something like <component name>.saveOrderAjax (really what you set as the data-url attribute of the <tbody> element).

So in your delete method you could try checking if it's that Ajax request, and if so then don't execute the SQL delete operation. Eg

$app = Factory::getApplication();
$task = $app->input->get('task');
if (!str_ends_with($task, 'saveOrderAjax')) {
    // execute delete query
}
1
  • It works perfect! Thanks a lot!
    – Steven
    May 17, 2023 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.