List Model

This method gets the value of a user state variable and sets it in the session. This is the same as the method in \JApplication except that this also can optionally force you back to the first page when a filter has changed.

  • string $key - The key of the user state variable.
  • string $request - The name of the variable passed in a request.
  • string $default - The default value for the variable if not found (Optional)
  • string $type - Filter for the variable. (Optional)
  • boolean $resetPage - If true, the limitstart in request is set to zero
  • return mixed - The request user state.
public function getUserStateFromRequest($key, $request, $default = null, $type = 'none', $resetPage = true)
{
$app = \JFactory::getApplication();
$input = $app->input;
$old_state = $app->getUserState($key);
$cur_state = $old_state !== null ? $old_state : $default;
$new_state = $input->get($request, null, $type);

// BC for Search Tools which uses different naming
if ($new_state === null && strpos($request, 'filter_') === 0)
{
$name = substr($request, 7);
$filters = $app->input->get('filter', array(), 'array');

if (isset($filters[$name]))
{
$new_state = $filters[$name];
}
}

if ($cur_state != $new_state && $new_state !== null && $resetPage)
{
$input->set('limitstart', 0);
}

// Save the new value only if it is set in this request.
if ($new_state !== null)
{
$app->setUserState($key, $new_state);
}
else
{
$new_state = $cur_state;
}

return $new_state;
}