Back to ListModel class

Method getUserStateFromRequest

public mixed
getUserStateFromRequest
(mixed $key, mixed $request, mixed $default = null, mixed $type = 'none', mixed $resetPage = true)
Gets the value of a user state variable and sets it in the session
Parameters
  • 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, for valid values see {@link InputFilter::clean()}. Optional.
  • bool $resetPage If true, the limitstart in request is set to zero
Returns
  • mixed The request user state.
Since
  • 1.6
Class: ListModel
Project: Joomla

Method getUserStateFromRequest - Source code

/**
 * Gets the value of a user state variable and sets it in the session
 *
 * This is the same as the method in Application except that this also can optionally
 * force you back to the first page when a filter has changed
 *
 * @param   string   $key        The key of the user state variable.
 * @param   string   $request    The name of the variable passed in a request.
 * @param   string   $default    The default value for the variable if not found. Optional.
 * @param   string   $type       Filter for the variable, for valid values see {@link InputFilter::clean()}. Optional.
 * @param   boolean  $resetPage  If true, the limitstart in request is set to zero
 *
 * @return  mixed  The request user state.
 *
 * @since   1.6
 */
public function getUserStateFromRequest($key, $request, $default = null, $type = 'none', $resetPage = true)
{
    $app = Factory::getApplication();
    $input = $app->input;
    $old_state = $app->getUserState($key);
    $cur_state = $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;
}