Back to ApiController class

Method displayItem

public static
displayItem
(mixed $id = null)
Basic display of an item view
Parameters
  • int $id The primary key to display. Leave empty if you want to retrieve data from the request
Returns
  • static A \JControllerLegacy object to support chaining.
Since
  • 4.0.0
Class: ApiController
Project: Joomla

Method displayItem - Source code

/**
 * Basic display of an item view
 *
 * @param   integer  $id  The primary key to display. Leave empty if you want to retrieve data from the request
 *
 * @return  static  A \JControllerLegacy object to support chaining.
 *
 * @since   4.0.0
 */
public function displayItem($id = null)
{
    if ($id === null) {
        $id = $this->input->get('id', 0, 'int');
    }
    $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', Inflector::singularize($this->contentType));
    // Create the model, ignoring request data so we can safely set the state in the request from the controller
    $model = $this->getModel($modelName, '', ['ignore_request' => true, 'state' => $this->modelState]);
    if (!$model) {
        throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'));
    }
    try {
        $modelName = $model->getName();
    } catch (\Exception $e) {
        throw new \RuntimeException($e->getMessage());
    }
    $model->setState($modelName . '.id', $id);
    // Push the model into the view (as default)
    $view->setModel($model, true);
    $view->document = $this->app->getDocument();
    $view->displayItem();
    return $this;
}