Concrete5.7.5.2 - 如何将主题树属性值导入选择框?

时间:2015-10-19 02:27:58

标签: drop-down-menu tree attributes concrete5-5.7

我有一个主题树,我在页面上用作属性:

类别 -
-Topic 1
-Topic 2
-Topic 3

如何将主题放入块中的数组中?然后我可以在选择框中使用它?例如

$topics = ("Topic 1", "Topic 2", "Topic 3);
echo $form->select('categories', $topics);

如果我的选择框位于页面的最右侧,则它总是缺少正确的边框。如果我将它移动到其他地方,它显示正常。其他人都有这个吗?

BTW,对于那些想要从选择框属性中获取值的人:

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();  
            }  
        }  
    }  
...
}

1 个答案:

答案 0 :(得分:1)

您可以在块控制器中执行类似的操作...

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(我怀疑您希望id为select选项的值),如此...

[[name=>'Topic 1', id => 1], [name=>'Topic 2', id => 2]..etc.]

...然后在您的视图功能中,您可以设置变量以使其在视图模板中可用...

public function view() {
  $topics = $this->getTopics('My topic name');
  $this->set('topics', $topics);
}

然后,您可以迭代模板中的主题以输出选择列表。

希望有助于如何获取主题列表?