0
votes

I'm trying to create a simple image slider on the front page of my drupal website. I wrote a module with the basic outline as follows

<?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;
}

Now, everything is fine. I generate a block and I can place it wherever I want. Great. But I would really like to use javascript in this block so that I can pass the arrays and use onclick events to slide through some images from the arrays i queried. So I found out that I will have to pass the variables into javascript and I'll have to identify the javascript file i want to use in the module.

 //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>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I put these in the hook_init BUT NOTHING HAPPENS!!!!! I tested to see basic alerts. If I put hard code into the _block, such as:

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

Then I see '50' prints.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If I try to pass a variable to the hard coded script. That DOES NOT WORK either. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If I try to write 50 through the previous code, but from a separate file called slider.js which is in the same folder using "drupal_add_js(drupal_get_path('module', 'slider') .'/slider.js');", That DOES NOT WORK either (even though I'm not passing a variable)



So what the heck is going on!! Is it possible I'm missing some important core drupal files? Is there a way to trouble shoot this further?

Thanks!

1

1 Answers

0
votes

drupal_add_js should work in hook_init().First Check whether your hook function is correctly named as ModuleName_init() or not.