dqmg80654 2015-03-16 08:08
浏览 42
已采纳

如何在Drupal 7中创建自定义块?

I want to create a custom block in which I want to show custom form.

Form will contain only two element.

  • input box (#centimeter)
  • submit button

When this submit button is clicked I want to convert this centimeter in inch and display the result below this form and I want to show this in block in sidebar.

My questions:

  1. How to do this?
  2. I tried following link but don't know where to write the code - should I create any php file in some folder?
  3. What to do if I want to show this block as a page with some url like /measurement/convert-cm-to-inches

http://kahthong.com/2013/06/create-your-own-custom-drupal-block-programmatically

  • 写回答

1条回答 默认 最新

  • dor2p0520 2015-03-16 11:51
    关注

    Hope this may help you.

    <?php
    /**
     * Implements hook_block_view().
     */
    function your_module_block_view($block_name = '')
    {
        // in my example I show the form only in the front page.
        // You can show it where you want, obviously
        if (!drupal_is_front_page())
        {
            return NULL;
        }
    
        $form = drupal_get_form('your_module_form');
        $block = array
        (
            // 'subject' => t('Subject'),
            'content' => $form,
        );
    
        return $block;
    }
    
    /**
     * Implements hook_form().
     */
    function your_module_form($form, &$form_state)
    {
        // now I add a text field to the form
        // with a label and fixed dimensions (you never know...)
        $form['text'] = array
        (
          '#title' => t('Label for the text box'),
          '#type' => 'textfield',
          '#size' => 32,
          '#maxlength' => 128,
        );
    
        // now I add also a button
        $form['submit'] = array
        (
           '#type' => 'submit',
           '#value' => t('Submit'),
        );
    
        // and now I assign a my function as handler of the submit event 
        $form['#submit'][] = 'your_module_submit_handler';
        return $form;
    }
    
    function your_module_submit_handler($form, &$form_state)
    {
        // this function will be executed after the click
        // event of the user on the "submit" button.
        // here I only print a message
        // you can access a database, redirect, or whatever you want, obviously 
        drupal_set_message(t('Ok!'));
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 为什么安装CSiBridge时出现了这个问题,尝试了很多方法都不行,求详细操作步骤?
  • ¥15 鼠标右键,撤销删除 复制 移动,要怎样删除
  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术