我正在 Joomla 中创建我自己的插件,我想在文章中添加一个多选列表。我还希望默认选择其中的一些选项。以下是我的代码。
public function onContentPrepareForm($form, $data)
{
if (!($form instanceof JForm))
{
$this->_subject->setError('JERROR_NOT_A_FORM');
return false;
}
$id = $data->id;
$catid = $data->catid;
$db = JFactory::getDBO();
$query = "SELECT ID, title FROM #__content WHERE catid = '" . SUBTOPIC_CATEGORY_ID . "'";
$db->setQuery($query);
$resultArray = $db->loadAssocList();
$optionsString = '';
foreach($resultArray as $result)
{
$optionsString .= '<option value="' . $result['ID'] . '"> ' . $result['title'] . '</option>';
}
if(isset($id) && isset($catid) && $catid == TOPIC_CATEGORY_ID)
{
$query = "SELECT subtopic FROM #__empd_topic WHERE ID = '$id'";
$db->setQuery($query);
$subtopicArray = $db->loadRow();
}
$xmlText = '<?xml version="1.0" encoding="utf-8"?>
<form>
<fields name="subtopic" label="subtopic">
<fieldset name="subtopic" label="subtopic">
<field
name="subtopic"
type="list"
id="subtopic"
multiple ="true"
label = "subtopic"
message = "Message"
>' . $optionsString . '</field>
</fieldset>
</fields>
</form>';
$xmlObj = new SimpleXMLElement($xmlText);
$form->setField($xmlObj);
return true;
}
现在 xml 中的<field>
节点有一个默认属性,但它只能有一个值,我也在xml 中的节点selected="selected"
内尝试过<option>
,但它不起作用。您可以在此处获取有关 xml 列表的参考。