Drupal模块后台配置的分析

    技术2025-05-30  8

    开始接触drupal,难免对模块里面的函数摸不着头脑。这里以Question模块为例,分析后台配置表单的生成处理以及数据的存储等一系列过程。

     

    首先看下question_menu函数:

    function question_menu() { $items = array(); $items['admin/content/question'] = array( 'title' => 'Questions', 'description' => 'Manage the question queue.', 'access arguments' => array('manage questions'), 'page callback' => 'question_list_page', ); $items['question'] = array( 'title' => 'Ask a question', 'description' => 'Post a question to be answered', 'page callback' => 'question_add', 'access arguments' => array('ask questions'), ); $items['admin/settings/question'] = array( 'title' => 'Question', 'description' => 'Edit Question settings.', 'page callback' => 'drupal_get_form', 'page arguments' => array('question_settings'), 'access arguments' => array('administer site configuration'), 'type' => MENU_NORMAL_ITEM, ); return $items; }

    在$items['admin/settings/question']中,调用question_settings函数作为参数传入drupal_get_form进行处理。

     

    在question_settings中:

    function question_settings() { // require users to be registered in order to ask questions? $form['question_require_registered'] = array( '#type' => 'checkbox', '#weight' => -5, '#title' => t('Require registered users?'), '#return_value' => 1, '#default_value' => variable_get('question_require_registered', FALSE), '#description' => t('Require users to be authenticated in order to submit questions?'), ); // Thank You node $form['question_thanks'] = array( '#type' => 'textfield', '#weight' => -3, '#title' => t('Path to "Thank You" node'), '#default_value' => variable_get('question_thanks', FALSE), '#size' => 40, '#maxlength' => 100, '#description' => t('This is where users will end up after they submit the question form. Example: "node/454".<br/>Leave blank and user will be returned to the form page with a thank you message.'), ); // Instructions $form['question_instructions'] = array( '#type' => 'textarea', '#weight' => -1, '#title' => t('Instructions for the user'), '#default_value' => variable_get('question_instructions', FALSE), '#size' => 40, '#description' => t('This message will appear above the question form to provide the user instructions.'), ); $form['question_instructions_format'] = filter_form(variable_get('question_instructions_format', FALSE), 0, array('question_instructions_format')); return system_settings_form($form); }

    最后一行:

    return system_settings_form($form);

    为以上$form生成默认按钮并提交给system_settings_form_submit处理,进而将配置的值存入数据表{variable}(通过variable_set函数实现,可参考API)。

    最新回复(0)