Back to ApiController class

Method edit

public static
edit
()
Method to edit an existing record.
Returns
  • static A \JControllerLegacy object to support chaining.
Since
  • 4.0.0
Class: ApiController
Project: Joomla

Method edit - Source code

/**
 * Method to edit an existing record.
 *
 * @return  static  A \JControllerLegacy object to support chaining.
 *
 * @since   4.0.0
 */
public function edit()
{
    /** @var \Joomla\CMS\MVC\Model\AdminModel $model */
    $model = $this->getModel(Inflector::singularize($this->contentType));
    if (!$model) {
        throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'));
    }
    try {
        $table = $model->getTable();
    } catch (\Exception $e) {
        throw new \RuntimeException($e->getMessage());
    }
    $recordId = $this->input->getInt('id');
    if (!$recordId) {
        throw new Exception\ResourceNotFound(Text::_('JLIB_APPLICATION_ERROR_RECORD'), 404);
    }
    $key = $table->getKeyName();
    // Access check.
    if (!$this->allowEdit(array($key => $recordId), $key)) {
        throw new NotAllowed('JLIB_APPLICATION_ERROR_CREATE_RECORD_NOT_PERMITTED', 403);
    }
    // Attempt to check-out the new record for editing and redirect.
    if ($table->hasField('checked_out') && !$model->checkout($recordId)) {
        // Check-out failed, display a notice but allow the user to see the record.
        throw new Exception\CheckinCheckout(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKOUT_FAILED', $model->getError()));
    }
    $this->save($recordId);
    $this->displayItem($recordId);
    return $this;
}