Concrete5.7.5.2 - 如何将主题树属性值放入 select 框中?
Concrete5.7.5.2 - How to get Topics tree attributes values into a select box?
我有一个主题树,我将其用作页面上的属性:
类别-
-主题 1
-主题 2
-主题 3
如何将主题放入块中的数组中?然后我可以在 select 框中使用哪个?例如
$topics = ("Topic 1", "Topic 2", "Topic 3);
echo $form->select('categories', $topics);
如果我的 select 框位于页面的最右侧,它总是缺少右边框。如果我将它移到其他地方,它显示正常。还有其他人有这个吗?
顺便说一句,对于那些想从 select 框属性中获取值的人:
use Concrete\Core\Attribute\Key\CollectionKey as CollectionKey;
use Concrete\Attribute\Select\Controller as SelectController;
use Concrete\Core\Attribute\Type as AttributeType;
$ak = CollectionKey::getByHandle('region');
$at = AttributeType::getByHandle('select');
$satc = new SelectController($at);
$satc->setAttributeKey($ak);
$values = $satc->getOptions()->getOptions();
foreach ($values as $key => $value) {
$this->options[$value->getSelectAttributeOptionID()] = $value->getSelectAttributeOptionValue();
}
谢谢。
[已解决]
感谢迈克,这是一段工作代码:
use Concrete\Core\Tree\Type\Topic as TopicTree;
public $category = array('');
public function view() {
...
$this->requireAsset('core/topics');
$tt = new TopicTree();
$tree = $tt->getByName('My Categories');
$node = $tree->getRootTreeNodeObject();
$node->populateChildren();
if (is_object($node)) {
foreach($node->getChildNodes() as $key => $category) {
if ($category instanceof \Concrete\Core\Tree\Node\Type\Topic) {
$this->category[$category->getTreeNodeDisplayName()] = $category->getTreeNodeDisplayName();
}
}
}
...
}
你可以在你的块控制器中做这样的事情...
private function getTopics($topicTreeName)
{
$this->requireAsset('core/topics');
$tt = new TopicTree();
/** @var Topic $tree */
$tree = $tt->getByName($topicTreeName);
/** @var TopicCategory $node */
$node = $tree->getRootTreeNodeObject();
$node->populateChildren();
$topics = [];
/** @var Concrete/Core/Tree/Node/Type/Topic $topic */
foreach ($node->getChildNodes() as $topic) {
if ($topic instanceof \Concrete\Core\Tree\Node\Type\Topic) {
$topics[] = [
'id' => $topic->getTreeNodeID(),
'name' => $topic->getTreeNodeDisplayName(),
];
}
}
return $topics;
}
这将为您提供一组主题及其 ID(我怀疑您会想要 select 选项值的 ID),如下所示...
[[name=>'Topic 1', id => 1], [name=>'Topic 2', id => 2]..etc.]
...然后在您的视图函数中,您可以设置变量以使其在视图模板中可用...
public function view() {
$topics = $this->getTopics('My topic name');
$this->set('topics', $topics);
}
然后您可以遍历模板中的主题以输出 select 列表。
希望对如何获取主题列表有所帮助?
我有一个主题树,我将其用作页面上的属性:
类别-
-主题 1
-主题 2
-主题 3
如何将主题放入块中的数组中?然后我可以在 select 框中使用哪个?例如
$topics = ("Topic 1", "Topic 2", "Topic 3);
echo $form->select('categories', $topics);
如果我的 select 框位于页面的最右侧,它总是缺少右边框。如果我将它移到其他地方,它显示正常。还有其他人有这个吗?
顺便说一句,对于那些想从 select 框属性中获取值的人:
use Concrete\Core\Attribute\Key\CollectionKey as CollectionKey;
use Concrete\Attribute\Select\Controller as SelectController;
use Concrete\Core\Attribute\Type as AttributeType;
$ak = CollectionKey::getByHandle('region');
$at = AttributeType::getByHandle('select');
$satc = new SelectController($at);
$satc->setAttributeKey($ak);
$values = $satc->getOptions()->getOptions();
foreach ($values as $key => $value) {
$this->options[$value->getSelectAttributeOptionID()] = $value->getSelectAttributeOptionValue();
}
谢谢。
[已解决]
感谢迈克,这是一段工作代码:
use Concrete\Core\Tree\Type\Topic as TopicTree;
public $category = array('');
public function view() {
...
$this->requireAsset('core/topics');
$tt = new TopicTree();
$tree = $tt->getByName('My Categories');
$node = $tree->getRootTreeNodeObject();
$node->populateChildren();
if (is_object($node)) {
foreach($node->getChildNodes() as $key => $category) {
if ($category instanceof \Concrete\Core\Tree\Node\Type\Topic) {
$this->category[$category->getTreeNodeDisplayName()] = $category->getTreeNodeDisplayName();
}
}
}
...
}
你可以在你的块控制器中做这样的事情...
private function getTopics($topicTreeName)
{
$this->requireAsset('core/topics');
$tt = new TopicTree();
/** @var Topic $tree */
$tree = $tt->getByName($topicTreeName);
/** @var TopicCategory $node */
$node = $tree->getRootTreeNodeObject();
$node->populateChildren();
$topics = [];
/** @var Concrete/Core/Tree/Node/Type/Topic $topic */
foreach ($node->getChildNodes() as $topic) {
if ($topic instanceof \Concrete\Core\Tree\Node\Type\Topic) {
$topics[] = [
'id' => $topic->getTreeNodeID(),
'name' => $topic->getTreeNodeDisplayName(),
];
}
}
return $topics;
}
这将为您提供一组主题及其 ID(我怀疑您会想要 select 选项值的 ID),如下所示...
[[name=>'Topic 1', id => 1], [name=>'Topic 2', id => 2]..etc.]
...然后在您的视图函数中,您可以设置变量以使其在视图模板中可用...
public function view() {
$topics = $this->getTopics('My topic name');
$this->set('topics', $topics);
}
然后您可以遍历模板中的主题以输出 select 列表。
希望对如何获取主题列表有所帮助?