Drupal 7 - 如何在属性中使用变量?

时间:2013-07-14 21:50:33

标签: php forms drupal drupal-7

Drupal7 - 如何在属性/占位符中使用变量?

$form['name'] = array(
    '#type' => 'textfield',
    //'#maxlength' => 50,
    '#size' => 60,
    '#required' => TRUE,
    '#title' => t('Name'),
    '#attributes' => array('placeholder' => 'Max'),
);

以上示例有效,以下内容无效!

    ...
    $name = $account->field_name['und']['0']['value'];

function confirm_personal_user_information() {

    $form['name'] = array(
        '#type' => 'textfield',
        //'#maxlength' => 50,
        '#size' => 60,
        '#required' => TRUE,
        '#title' => t('Name'),
        '#attributes' => array('placeholder' => $name),
    );

...

}

任何想法?提前谢谢!

编辑:============================================= === 解决方案:在表单函数中定义变量,然后验证有问题......

示例:

function confirm_personal_user_information() {
    $name = 'Max Power';

    $form['#action'] = 'Validate';
    $form['#id'] = 'form_user_information_xxx';
    //$form['#validate'] = form_user_information_validators();
    $form['#submit'] = 'form_user_information_submit';
    $form['#prefix'] = '<div id="form_user_information">';
    $form['#suffix'] = '</div>';

    $form['name'] = array(
        '#type' => 'textfield',
        //'#maxlength' => 50,
        '#size' => 60,
        '#required' => TRUE,
        '#title' => t('Name'),
        //'#attributes' => array('placeholder' => $name),
        '#default_value' => "$name",
    );

    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Validate');

return $form;
}

//print form
$form = drupal_get_form('confirm_personal_user_information');
print drupal_render($form);

1 个答案:

答案 0 :(得分:1)

'#attributes' => array(
   'class' => array('service-tax'),
   'amount' => $amt
),