在drupal 7中阻止没有出现

时间:2013-04-06 20:31:27

标签: drupal drupal-7 drupal-modules drupal-views

我已经在一个区块中创建了一个表单,第一次出现但它没有再次出现,我不知道为什么会发生这种情况,因为我对drupal很新。

     <?php

         function my_module_block_info() {
              $blocks = array();
              $blocks['my_block'] = array(
              'info' => t('My Custom Block'),
              );

              return $blocks;
         }
         function hook_block_view($delta = '') {
               switch ($delta) {
               case 'my_block':
               $block['subject'] = t('My form');
               $block['content'] = my_module_my_form();
         }
       }

       function my_module_menu() {
           $items = array();
           $items['my_module/form'] = array(
           'title' => t('My form'),
           'page callback' => 'my_module_form',
           'acess arguments' => array('access content'),
           'description' => t('My form'),
           'type' => MENU_CALLBACK,
           'access callback' => TRUE,
            );
       return $items;
           }

        function my_module_form() {
        return drupal_get_form('my_module_my_form');
            }

        function my_module_my_form($form) {

             $form['name'] = array(
             '#type' => 'textfield',
             '#title' => t('username'),
             '#required' => TRUE,
             '#description' => "enter your username.",
             '#size' => 20,
             '#maxlength' => 20,
             );
             $form['picture'] = array(
             '#type' => 'file',
             '#title' => t('Image'),
             '#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
              );
     $form['submit'] = array(
      '#type' => 'submit',
      '#value' => 'update your profile',
     );
     return $form;
 }

    function my_module_my_form_submit($form, &$form_state) {
     global $user;
     $account = user_load($user -> uid);
     $image_path = 'picture://';
      $validators = array(
      'file_validate_is_image' => array(),
       );
     $file = new StdClass();
         $file -> uid = $user -> uid;
         $file->uri = $image_path;
         $file = file_save_upload('picture', $validators);
     $new = array (
      'name' => $form_state['values']['name'],
      'picture' => $file,
      );
      user_save($account,$new );
    $message = 'You have updated your profile';
    drupal_set_message(t($message));
 }

此表单正在更新用户名和用户图片,但是当我尝试在块中调用此表单作为其内容但在第一次之后它无法正常工作时。请帮助我。

1 个答案:

答案 0 :(得分:3)

功能

存在许多问题
function hook_block_view($delta = '') {
  switch ($delta) {
    case 'my_block':
      $block['subject'] = t('My form');
      $block['content'] = my_module_my_form();
    }
}
  1. 您在这里写的是hook_block_view()的实现,而不是钩子本身,因此该函数应该被称为my_module_block_view();
  2. $block['content']应填充渲染表单,因此您需要调用my_module_form()而不是表单定义函数(就像您在菜单挂钩中的页面回调中所做的那样);
  3. 您的功能不会返回任何内容!你应该在switch语句后包含return $block;
  4. 所以你的功能应该更像是

    function my_module_block_view($delta = '') {
      switch ($delta) {
        case 'my_block':
          $block['subject'] = t('My form');
          $block['content'] = my_module_form();
          break;
      }
      return $block;
    }