Back to JsonApiView class

Method displayItem

public string
displayItem
(mixed $item = null)
Execute and display a template script.
Parameters
  • object $item Item
Returns
  • string
Since
  • 4.0.0
Class: JsonApiView
Project: Joomla

Method displayItem - Source code

/**
 * Execute and display a template script.
 *
 * @param   object  $item  Item
 *
 * @return  string
 *
 * @since   4.0.0
 */
public function displayItem($item = null)
{
    if ($item === null) {
        /** @var \Joomla\CMS\MVC\Model\AdminModel $model */
        $model = $this->getModel();
        $item = $this->prepareItem($model->getItem());
    }
    if ($item->id === null) {
        throw new RouteNotFoundException('Item does not exist');
    }
    // Check for errors.
    if (\count($errors = $this->get('Errors'))) {
        throw new GenericDataException(implode("\n", $errors), 500);
    }
    if ($this->type === null) {
        throw new \RuntimeException('Content type missing');
    }
    $eventData = ['type' => OnGetApiFields::ITEM, 'fields' => $this->fieldsToRenderItem, 'relations' => $this->relationship, 'context' => $this->type];
    $event = new OnGetApiFields('onApiGetFields', $eventData);
    /** @var OnGetApiFields $eventResult */
    $eventResult = Factory::getApplication()->getDispatcher()->dispatch('onApiGetFields', $event);
    $element = (new Resource($item, $this->serializer))->fields([$this->type => $eventResult->getAllPropertiesToRender()]);
    if (!empty($this->relationship)) {
        $element->with($eventResult->getAllRelationsToRender());
    }
    $this->document->setData($element);
    $this->document->addLink('self', Uri::current());
    return $this->document->render();
}