diff --git a/administrator/components/com_content/src/Model/ArticleModel.php b/administrator/components/com_content/src/Model/ArticleModel.php index 72f440b073676..d9ed91e78050f 100644 --- a/administrator/components/com_content/src/Model/ArticleModel.php +++ b/administrator/components/com_content/src/Model/ArticleModel.php @@ -524,22 +524,20 @@ public function getForm($data = array(), $loadData = true) ? (int) reset($assignedCatids) : (int) $assignedCatids; - // Try to get the category from the html code of the field + // Try to get the category from the category field if (empty($assignedCatids)) { $assignedCatids = $formField->getAttribute('default', null); - // Choose the first category available - $xml = new \DOMDocument; - libxml_use_internal_errors(true); - $xml->loadHTML($formField->__get('input')); - libxml_clear_errors(); - libxml_use_internal_errors(false); - $options = $xml->getElementsByTagName('option'); - - if (!$assignedCatids && $firstChoice = $options->item(0)) + if (!$assignedCatids) { - $assignedCatids = $firstChoice->getAttribute('value'); + // Choose the first category available + $catOptions = $formField->options; + + if ($catOptions && !empty($catOptions[0]->value)) + { + $assignedCatids = (int) $catOptions[0]->value; + } } } diff --git a/administrator/components/com_fields/src/Helper/FieldsHelper.php b/administrator/components/com_fields/src/Helper/FieldsHelper.php index 06138479a76c3..f76d1af08e50d 100644 --- a/administrator/components/com_fields/src/Helper/FieldsHelper.php +++ b/administrator/components/com_fields/src/Helper/FieldsHelper.php @@ -316,17 +316,15 @@ public static function prepareForm($context, Form $form, $data) { $assignedCatids = $formField->getAttribute('default', null); - // Choose the first category available - $xml = new \DOMDocument; - libxml_use_internal_errors(true); - $xml->loadHTML($formField->__get('input')); - libxml_clear_errors(); - libxml_use_internal_errors(false); - $options = $xml->getElementsByTagName('option'); - - if (!$assignedCatids && $firstChoice = $options->item(0)) + if (!$assignedCatids) { - $assignedCatids = $firstChoice->getAttribute('value'); + // Choose the first category available + $catOptions = $formField->options; + + if ($catOptions && !empty($catOptions[0]->value)) + { + $assignedCatids = (int) $catOptions[0]->value; + } } $data->fieldscatid = $assignedCatids; diff --git a/libraries/src/MVC/Model/WorkflowBehaviorTrait.php b/libraries/src/MVC/Model/WorkflowBehaviorTrait.php index 29aea89551f3a..788d00324ec42 100644 --- a/libraries/src/MVC/Model/WorkflowBehaviorTrait.php +++ b/libraries/src/MVC/Model/WorkflowBehaviorTrait.php @@ -411,17 +411,15 @@ protected function getStageForNewItem(Form $form, $data) { $catId = $field->getAttribute('default', null); - // Choose the first category available - $xml = new \DOMDocument; - libxml_use_internal_errors(true); - $xml->loadHTML($field->__get('input')); - libxml_clear_errors(); - libxml_use_internal_errors(false); - $options = $xml->getElementsByTagName('option'); - - if (!$catId && $firstChoice = $options->item(0)) + if (!$catId) { - $catId = $firstChoice->getAttribute('value'); + // Choose the first category available + $catOptions = $field->options; + + if ($catOptions && !empty($catOptions[0]->value)) + { + $catId = (int) $catOptions[0]->value; + } } }