Form Controller

It is a method to reload a record.

  • param string $key - The name of the primary key of the URL variable.
  • param string $urlVar - The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
  • return - void
public function reload($key = null, $urlVar = null)
{
// Check for request forgeries.
$this->checkToken();

$app = \JFactory::getApplication();
$model = $this->getModel();
$data = $this->input->post->get('jform', array(), 'array');

// Determine the name of the primary key for the data.
if (empty($key))
{
$key = $model->getTable()->getKeyName();
}

// To avoid data collisions the urlVar may be different from the primary key.
if (empty($urlVar))
{
$urlVar = $key;
}

$recordId = $this->input->getInt($urlVar);

// Populate the row id from the session.
$data[$key] = $recordId;

// Check if it is allowed to edit or create the data
if (($recordId && !$this->allowEdit($data, $key)) || (!$recordId && !$this->allowAdd($data)))
{
$this->setRedirect(
\JRoute::_(
'index.php?option=' . $this->option . '&view=' . $this->view_list
. $this->getRedirectToListAppend(), false
)
);
$this->redirect();
}

// The redirect url
$redirectUrl = \JRoute::_(
'index.php?option=' . $this->option . '&view=' . $this->view_item .
$this->getRedirectToItemAppend($recordId, $urlVar),
false
);

// Validate the posted data.
// Sometimes the form needs some posted data, such as for plugins and modules.
$form = $model->getForm($data, false);

if (!$form)
{
$app->enqueueMessage($model->getError(), 'error');

$this->setRedirect($redirectUrl);
$this->redirect();
}

// Save the data in the session.
$app->setUserState($this->option . '.edit.' . $this->context . '.data', $form->filter($data));

$this->setRedirect($redirectUrl);
$this->redirect();
}