drupal block variable_set不保存选择值

时间:2013-03-26 04:38:34

标签: drupal-6 drupal-modules

我正在尝试创建一个从选择框中保存值的块。但是,重新加载后,variable_get不会返回保存的值...因此variable_set接缝不起作用。

我在这里做错了什么? (Drupal 6)

function get_courses(){
   global $user;     
    $my_items_sql = 'SELECT course_node.uid, ';
                     //get the course nid
                     $my_items_sql .='course_node.nid as course_nid, ';
                     //get the course title
                     $my_items_sql .='course_node.title as course_title ';
                     $my_items_sql .=' from node as course_node where course_node.type="course" and course_node.uid = "'.$user->uid.'"';
                     $my_items_sql .=' order by course_node.nid; ';
                     $my_items_data= db_query($my_items_sql);

                     $my_courses = array();
                     while ($row = db_fetch_array($my_items_data)) {

                            //$course_node = node_load($row["course_nid"]);
                            $my_courses[$row["course_nid"]]=$row["course_title"];
                     }
                     //$variables['courses']=$my_courses;
                     //drupal_add_js(array('courses'=>$my_courses), "setting");

         return $my_courses;
}
function front_page_block($op='list',$delta = 0, $edit = array()) {

    $course_options = array();
    $courses = get_courses();
    $tmp = $courses;
    $first_course = $tmp;
    reset($first_course );
    $first_course_nid = key($first_course);

    $first_course_nid = key($first_course);
    switch($op){
        case 'list':
            $blocks[0]['info']= t('Course Data Loader');
            $blocks[0]['cache']= BLOCK_NO_CACHE;
            return $blocks;
        case 'configure':
            $form['course_to_display'] = array(
            '#type' => 'select',
            '#description' => t('Display flashcards from which deck on the front page?'),
            '#options' => $courses,
            '#default_value'=>variable_get('front_page_deck_to_load', $first_course_nid)
            );
            return $form;
        case 'save': 
            variable_set('front_page_deck_to_load', 
                $edit['front_page_deck_to_load']);

            break;


    }

1 个答案:

答案 0 :(得分:1)

首先检查 $ edit ['front_page_deck_to_load'] 的值,然后再调用

variable_set('front_page_deck_to_load',$ edit ['front_page_deck_to_load']); 功能。

如果正确设置 $ edit ['front_page_deck_to_load'] 的值,则variable_set正在运行。

现在的问题是, $ form ['course_to_display'] '#default_value'设置不正确。请按照以下方式修改 get_courses()功能,然后重试:

function get_courses(){
   global $user;     
    $my_items_sql = 'SELECT course_node.uid, ';
                     //get the course nid
                     $my_items_sql .='course_node.nid as course_nid, ';
                     //get the course title
                     $my_items_sql .='course_node.title as course_title ';
                     $my_items_sql .=' from node as course_node where course_node.type="course" and course_node.uid = "'.$user->uid.'"';
                     $my_items_sql .=' order by course_node.nid; ';
                     $my_items_data= db_query($my_items_sql);

                     $my_courses = array();
                     while ($row = db_fetch_array($my_items_data)) {

                            //$course_node = node_load($row["course_nid"]);
                            $my_courses[$row["course_title"]]=$row["course_title"];
                     }
                     //$variables['courses']=$my_courses;
                     //drupal_add_js(array('courses'=>$my_courses), "setting");

         return $my_courses;
}

仅供参考:请按照http://drupal.org/node/240783

上的评论#1 进行操作