diff --git a/administrator/components/com_content/src/Model/ArticleModel.php b/administrator/components/com_content/src/Model/ArticleModel.php index 7903582d1f989..d613d2e592884 100644 --- a/administrator/components/com_content/src/Model/ArticleModel.php +++ b/administrator/components/com_content/src/Model/ArticleModel.php @@ -163,9 +163,6 @@ protected function cleanupPostBatchCopy(TableInterface $table, $newId, $oldId) $this->workflowCleanupBatchMove($oldId, $newId); - // Register FieldsHelper - \JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); - $oldItem = $this->getTable(); $oldItem->load($oldId); $fields = FieldsHelper::getFields('com_content.article', $oldItem, true); @@ -487,8 +484,7 @@ public function getItem($pk = null) */ public function getForm($data = array(), $loadData = true) { - $app = Factory::getApplication(); - $user = $app->getIdentity(); + $user = Factory::getApplication()->getIdentity(); // Get the form. $form = $this->loadForm('com_content.article', 'article', array('control' => 'jform', 'load_data' => $loadData)); @@ -498,62 +494,38 @@ public function getForm($data = array(), $loadData = true) return false; } - $jinput = $app->input; - - /* - * The front end calls this model and uses a_id to avoid id clashes so we need to check for that first. - * The back end uses id so we use that the rest of the time and set it to 0 by default. - */ - $id = $jinput->get('a_id', $jinput->get('id', 0)); + $id = (int) $this->getState('article.id'); - // Determine correct permissions to check. - if ($id = $this->getState('article.id', $id)) + // For new articles we load the potential state + associations + if ($id == 0 && $formField = $form->getField('catid')) { - if ($app->isClient('site')) - // Existing record. We can't edit the category in frontend if not edit.state. - { - if ($id != 0 && (!$user->authorise('core.edit.state', 'com_content.article.' . (int) $id)) - || ($id == 0 && !$user->authorise('core.edit.state', 'com_content'))) - { - $form->setFieldAttribute('catid', 'readonly', 'true'); - $form->setFieldAttribute('catid', 'required', 'false'); - $form->setFieldAttribute('catid', 'filter', 'unset'); - } - } - } - else - { - // For new articles we load the potential state + associations - if ($formField = $form->getField('catid')) - { - $assignedCatids = (int) ($data['catid'] ?? $form->getValue('catid')); + $assignedCatids = $data['catid'] ?? $form->getValue('catid'); - $assignedCatids = is_array($assignedCatids) - ? (int) reset($assignedCatids) - : (int) $assignedCatids; + $assignedCatids = is_array($assignedCatids) + ? (int) reset($assignedCatids) + : (int) $assignedCatids; + + // Try to get the category from the category field + if (empty($assignedCatids)) + { + $assignedCatids = $formField->getAttribute('default', null); - // Try to get the category from the category field - if (empty($assignedCatids)) + if (!$assignedCatids) { - $assignedCatids = $formField->getAttribute('default', null); + // Choose the first category available + $catOptions = $formField->options; - if (!$assignedCatids) + if ($catOptions && !empty($catOptions[0]->value)) { - // Choose the first category available - $catOptions = $formField->options; - - if ($catOptions && !empty($catOptions[0]->value)) - { - $assignedCatids = (int) $catOptions[0]->value; - } + $assignedCatids = (int) $catOptions[0]->value; } } - - // Activate the reload of the form when category is changed - $form->setFieldAttribute('catid', 'refresh-enabled', true); - $form->setFieldAttribute('catid', 'refresh-cat-id', $assignedCatids); - $form->setFieldAttribute('catid', 'refresh-section', 'article'); } + + // Activate the reload of the form when category is changed + $form->setFieldAttribute('catid', 'refresh-enabled', true); + $form->setFieldAttribute('catid', 'refresh-cat-id', $assignedCatids); + $form->setFieldAttribute('catid', 'refresh-section', 'article'); } // Check for existing article. @@ -581,24 +553,6 @@ public function getForm($data = array(), $loadData = true) $form->setFieldAttribute('state', 'filter', 'unset'); } - // Prevent messing with article language and category when editing existing article with associations - $assoc = Associations::isEnabled(); - - // Check if article is associated - if ($this->getState('article.id') && $app->isClient('site') && $assoc) - { - $associations = Associations::getAssociations('com_content', '#__content', 'com_content.item', $id); - - // Make fields read only - if (!empty($associations)) - { - $form->setFieldAttribute('language', 'readonly', 'true'); - $form->setFieldAttribute('catid', 'readonly', 'true'); - $form->setFieldAttribute('language', 'filter', 'unset'); - $form->setFieldAttribute('catid', 'filter', 'unset'); - } - } - return $form; } diff --git a/components/com_content/src/Model/FormModel.php b/components/com_content/src/Model/FormModel.php index 9172510f446aa..b63bb2d0ad4d3 100644 --- a/components/com_content/src/Model/FormModel.php +++ b/components/com_content/src/Model/FormModel.php @@ -17,6 +17,7 @@ use Joomla\CMS\Language\Associations; use Joomla\CMS\Language\Multilanguage; use Joomla\CMS\Object\CMSObject; +use Joomla\CMS\Table\Table; use Joomla\Registry\Registry; use Joomla\Utilities\ArrayHelper; @@ -201,6 +202,55 @@ public function save($data) return parent::save($data); } + /** + * Method to get the record form. + * + * @param array $data Data for the form. + * @param boolean $loadData True if the form is to load its own data (default case), false if not. + * + * @return Form|boolean A Form object on success, false on failure + * + * @since 1.6 + */ + public function getForm($data = [], $loadData = true) + { + $form = parent::getForm($data, $loadData); + + if (empty($form)) + { + return false; + } + + $user = Factory::getApplication()->getIdentity(); + + $id = (int) $this->getState('article.id'); + + // Existing record. We can't edit the category in frontend if not edit.state. + if ($id > 0 && !$user->authorise('core.edit.state', 'com_content.article.' . $id)) + { + $form->setFieldAttribute('catid', 'readonly', 'true'); + $form->setFieldAttribute('catid', 'required', 'false'); + $form->setFieldAttribute('catid', 'filter', 'unset'); + } + + // Prevent messing with article language and category when editing existing article with associations + if ($this->getState('article.id') && Associations::isEnabled()) + { + $associations = Associations::getAssociations('com_content', '#__content', 'com_content.item', $id); + + // Make fields read only + if (!empty($associations)) + { + $form->setFieldAttribute('language', 'readonly', 'true'); + $form->setFieldAttribute('catid', 'readonly', 'true'); + $form->setFieldAttribute('language', 'filter', 'unset'); + $form->setFieldAttribute('catid', 'filter', 'unset'); + } + } + + return $form; + } + /** * Allows preprocessing of the JForm object. * @@ -228,7 +278,7 @@ protected function preprocessForm(Form $form, $data, $group = 'content') $form->setFieldAttribute('language', 'default', '*'); } - return parent::preprocessForm($form, $data, $group); + parent::preprocessForm($form, $data, $group); } /**