0

我正在开发 Drupal 7 的模块。我定义了一个名为“Booth”的节点类型。现在在我的模块中,我创建了一个包含姓名、电话、地址等字段的表单。这些字段之一是 Booth,它是一个 Select 类型元素。我想将展位标题(我在“添加内容 > 展位”中添加)作为我的选择元素选项。我怎样才能做到这一点?如何使用展位内容类型的标题字段填充选项数组?[请看下图]

第一个字段必须填写展位标题

$form['exbooth'] = array(
    '#type' => 'select',
    '#title' => t('Exhibition Booth'),
    '#options' => array(), // I want to fill this array with title fields of booth content type
    '#required' => TRUE,
);
$form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#required' => TRUE,
);
$form['lastname'] = array(
    '#type' => 'textfield',
    '#title' => t('Last Name'),
    '#required' => TRUE,
4

1 回答 1

0

在对drupal API进行了一些挖掘之后,我终于找到了解决方案。

我使用 entity_load() 函数来检索“booth”内容类型的所有节点,然后将结果的标题放在一个数组中,并将该数组设置为 Select 选项:

$entities = entity_load('node');
$booths = array();
foreach($entities as $entity) {
    if($entity->type == 'booth') {
        $i = 1;
        $booths[$i] = $entity->title;
    }
}
....
//inputs
$form['exbooth'] = array(
    '#type' => 'select',
    '#title' => t('Exhibition Booth'),
    '#options' => $booths, // I set my array of booth nodes here
    '#required' => TRUE,
);
于 2016-05-14T20:19:14.550 回答