dsfadsa08911 2014-10-02 07:44
浏览 22
已采纳

Drupal Block View执行Javascript功能

This would be my first custom module I am creating, and having a few grey hair's for something as simple as rendering a js function into a drupal block module.

so the script works just fine in php mode by pasting the following code into a drupal create new block , and it renders out just fine.

   <!-- Include my.js  -->
    <script src="https://1.2.3.4/static/my.js"></script>

    <!-- Decide where you want to put my -->
    <div id="my_container" style="position: relative; width: 60em; height: 30em;">
        <div id="my"></div>
    </div>

    <!-- Call my.init() at some point after the page is done loading -->
    <script>
    window.onload = function() {
        // Initialize my:
        my.init({url: 'https://1.2.3.4/'});
    }
    </script>

now what is the best practice to load this into a block module, is it hook_init or hook_block_view or both which ever it is , please give example and explanation .

Many Thanks in advance.

</div>
  • 写回答

2条回答 默认 最新

  • dqg95034 2014-10-02 12:34
    关注

    I'm guessing this is in Drupal 7.

    If you need to get started quickly...

    Example of how to organise your module. Let's call your module "mymodule".

    [inside mymodule folder]

    mymodule.info mymodule.module --js/ --inc/

    Put all the javascript specifically related to your module in the 'js' folder Put all the PHP files specifically related to your module in the 'inc' folder (short for includes)

    1) Use hook_block_view()

    //Within mymodule_block_view()
    
    //Put your content for the block
    $block['content']= '<div id="my_container" style="position: relative; width: 60em; height: 30em;">
                  <div id="my"></div>
                </div>';
    
    //Later on
    return $block;
    

    See https://api.drupal.org/api/drupal/modules%21block%21block.api.php/function/hook_block_view/7 for full example.

    I would split this out into a separate file, and either use module_file_include() (see https://api.drupal.org/api/drupal/includes!module.inc/function/module_load_include/7 ) or wrap it in a function and call the function from within mymodule_block_view()

    2) Use drupal_add_js() in your block view

    $module_path = drupal_get_path('module', 'mymodule'); //Get path of your module
    drupal_add_js($module_path.'/js/[your JS file]', 'file');
    

    Explanation and example for adding JS here: https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_add_js/7

    Please note that just plainly adding <script> to $block['content'] would work, but by using drupal_add_js(), you can take advantage of Drupal's cache (and various caching modules) that can help speed up your site. You don't get any of the advantages if you just use <script>.

    3) General tip: learn jQuery, much easier than pure javascript

    drupal_add_js("jQuery(document).ready(my.init({url: 'https://1.2.3.4/'}));", 'inline');
    

    Partial example (within your hook_block_view):

    $module_path = drupal_get_path('module', 'mymodule'); //Get path of your module
    drupal_add_js($module_path.'/js/[your JS file]', 'file');
    $block['content']= '<div id="my_container" style="position: relative; width: 60em; height: 30em;">
                      <div id="my"></div>
                    </div>';
    
    drupal_add_js("jQuery(document).ready(my.init({url: 'https://1.2.3.4/'}));", 'inline');
    

    As I mentioned above, I would separate out module code into smaller (more manageable) files.

    So for example.

    inc/mymodule_menu.inc.php //hook_menu
    inc/mymodule_blocks.inc.php //hook_block_view
    inc/mymodule_firstblock.inc.php //Code for a block wrapped in a function, which hook_block_view calls
    

    Hope this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集