如何在Drupal table中嵌入form元素

admin 提交于 星期三, 09/13/2017 - 09:59

1. 注册theme

function weike_course_evaluate_theme() {

  return array(

    'weike_course_evaluate_all'  => array(

      'render element' => 'form',

    ),

  );

}

2. 生成form,数据以value类型form元素传递。

function weike_course_evaluate_all($form, &$form_state) {

  $result = db_query('…');

  $form['data'] = array();

  $form['final-score'] = array();

 

  $form['title'] = array(

      '#markup' => '<h2>' . $node->title . '</h2>',

  );

 

       $count = 0;

    foreach ($result as $srow) {

      $form['data'][$count] = array();

      $form['data'][$count]['uid'] = array(

          '#type' => 'value',

          '#value' => $srow->uid,

      );

      $form['data'][$count]['name'] = array(

          '#type' => 'value',

          '#value' => $srow->name,

      );

      if (isset($score_result[$srow->uid]->final_score))

        $final_score = $score_result[$srow->uid]->final_score;

      else

        $final_score = '';

      $form['final-score']['final-score-' . $count] = array(

          '#type' => 'textfield',

          '#size' => 5,

          '#default_value' => $final_score,

      );

      $count++;

    }

 

  $form['save'] = array(

      '#type' => 'submit',

      '#value' => '保存',

  );

 

  return $form;

}

 

2. 实现theme

function theme_weike_course_evaluate_all($variables) {

  $form = $variables['form'];

  $i = 0;

 

  $header = array('姓名', '最终考核成绩');

  $rows = array();

  for ($i = 0; isset($form['data'][$i]); $i++) {

    $rows[] = array(

        $form['data'][$i]['name']['#value'],

        render($form['final-score']['final-score-' . $i]),

    );

  }

 

  $output = render($form['title']);

 

  $output .= theme('table', array('header' => $header, 'rows' => $rows));

 

  $output .= drupal_render_children($form);

 

  return $output;

}

 

参考: http://stackoverflow.com/questions/1771159/how-to-include-drupal-form-elements-in-a-data-table

标签

添加新评论

Restricted HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <img src>
  • 自动断行和分段。
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
请输入"Drupal8"