Back to ApiController class

Method displayList

public static
displayList
()
Basic display of a list view
Returns
  • static A \JControllerLegacy object to support chaining.
Since
  • 4.0.0
Class: ApiController
Project: Joomla

Method displayList - Source code

/**
 * Basic display of a list view
 *
 * @return  static  A \JControllerLegacy object to support chaining.
 *
 * @since   4.0.0
 */
public function displayList()
{
    // Assemble pagination information (using recommended JsonApi pagination notation for offset strategy)
    $paginationInfo = $this->input->get('page', [], 'array');
    $limit = null;
    $offset = null;
    if (\array_key_exists('offset', $paginationInfo)) {
        $offset = $paginationInfo['offset'];
        $this->modelState->set($this->context . '.limitstart', $offset);
    }
    if (\array_key_exists('limit', $paginationInfo)) {
        $limit = $paginationInfo['limit'];
        $this->modelState->set($this->context . '.list.limit', $limit);
    }
    $viewType = $this->app->getDocument()->getType();
    $viewName = $this->input->get('view', $this->default_view);
    $viewLayout = $this->input->get('layout', 'default', 'string');
    try {
        /** @var JsonApiView $view */
        $view = $this->getView($viewName, $viewType, '', ['base_path' => $this->basePath, 'layout' => $viewLayout, 'contentType' => $this->contentType]);
    } catch (\Exception $e) {
        throw new \RuntimeException($e->getMessage());
    }
    $modelName = $this->input->get('model', $this->contentType);
    /** @var ListModel $model */
    $model = $this->getModel($modelName, '', ['ignore_request' => true, 'state' => $this->modelState]);
    if (!$model) {
        throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'));
    }
    // Push the model into the view (as default)
    $view->setModel($model, true);
    if ($offset) {
        $model->setState('list.start', $offset);
    }
    /**
     * Sanity check we don't have too much data being requested as regularly in html we automatically set it back to
     * the last page of data. If there isn't a limit start then set
     */
    if ($limit) {
        $model->setState('list.limit', $limit);
    } else {
        $model->setState('list.limit', $this->itemsPerPage);
    }
    if (!is_null($offset) && $offset > $model->getTotal()) {
        throw new Exception\ResourceNotFound();
    }
    $view->document = $this->app->getDocument();
    $view->displayList();
    return $this;
}