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条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。