2013-05-15 70 views
0

我想在我的drupal網站的首頁上創建一個簡單的圖像滑塊。我寫了一個模塊,其基本輪廓如下Drupal 6 javascript in front page的模塊塊

<?php 

function slider_init(){ 
    drupal_add_js(drupal_get_path('module', 'slider') .'/slider.js'); 
} 

function slider_block($op = 'list', $delta = 0, $edit = array()) { 
    $block = array(); 

    switch ($op) { 
    case "list": 
     // Generate listing of blocks from this module, for the admin/block page 
     $block[0]["info"] = t("slider"); 
     break; 

    case "view": 
    // Generate content for blocks from this module 
    $block_content = ""; 
    $block_content .= "hello"; 


    //Query for the projects 
     $icons = db_query("SELECT * FROM {alumni_frontpage_projects}"); 

    //Initiation arrays from each table column: title, description, url, icon link 
     $links = array(); 
     $title = array(); 
     $description = array(); 
     $url = array(); 

    //Generate arrays from each table column: title, description, url, icon link 
     while($icon_data = db_fetch_array($icons)){ 

      $links[]  = $icon_data['slider_image_location']; 
      $title[]  = $icon_data['title']; 
      $description[]= $icon_data['description']; 
      $url[]  = $icon_data['url']; 

     } 

     //Count elements in array and randomly choose one 
     $res = count($links)-1; 
     $seed = rand(0,$res); 

     //Generate HTML and Javascript of Slider 


     //Check that content isn't empty 
     if ($block_content == "") { 
      $block_content = t("Sorry No Content");   
     } 

     $block["content"] = $block_content; 
     break; 

    case "save": 
     break; 

    case "configure": 
     break; 
    }   

    return $block; 
} 

現在,一切都很好。我生成一個塊,我可以把它放在任何我想要的地方。大。但我真的很想在這個塊中使用javascript,這樣我就可以傳遞數組並使用onclick事件來滑過我查詢的數組中的一些圖像。所以我發現,我將不得不將變量傳遞到JavaScript,我將不得不識別我想在模塊中使用的JavaScript文件。

//add variable to the Javascript 
    drupal_add_js(array('slider_settings' => array('variable_name' => $variable_name)), 'setting'); 

    //add Javascript file to module 
    drupal_add_js(drupal_get_path('module', 'slider') .'/slider.js');<br> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我把這些在hook_init但沒有任何反應! !!!!我測試過看到基本警報。如果我將硬代碼放入_block中,例如:

$block_content .= ' 
<script type="text/javascript"> 
var x 
x = 50; 
document.write(x); //prints the value of x 
</script>'; 

然後我看到'50'打印。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如果我嘗試將變量傳遞給硬編碼的腳本。那也行不通。 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如果我嘗試通過前面的代碼寫50,但是從一個單獨的文件名爲slider.js,它在同一個文件夾中使用「drupal_add_js(drupal_get_path('module','slider')。'/ slider.js');」,這也不起作用(即使我不是傳遞一個變量)



那麼究竟是怎麼回事!是否有可能我錯過了一些重要的核心Drupal文件?有沒有辦法來進一步解決這個問題?

謝謝!

回答

0

drupal_add_js應該在hook_init()中工作首先檢查你的鉤子函數是否正確地命名爲ModuleName_init()或不是。

+0

好的,我會證實這一點。 – ingrid