7

Is it possible to use jquery get or post methods to fetch data dynamic in a a drupal7.x block module. I am a newbie to drupal.

Here is my event_calendar.module file

      function event_calendar_help($path, $arg)
      {
          switch ($path) 
          {
          case "admin/help#event_calendar":
          return '<p>'.  t("A block module that creates events and lists them in a event calendar") .'</p>';
      break;
          }
    }
     /**
    * Implements hook_block_info().
    */
    function event_calendar_block_info() {
      $blocks['event_calendar'] = array(
        'info' => t('Event calendar'), 
        'cache' => DRUPAL_CACHE_PER_ROLE, //Default
      );
      return $blocks;
    }

    /**
    * Implements hook_block_view().
    *
    * Prepares the contents of the block.
    */
    function event_calendar_block_view($delta = '') 
    {
      switch($delta)
      {
        case 'event_calendar':
          $block['subject'] = NULL;
          if(user_access('access content'))
           {
            $items = array();        
            $basepath = drupal_get_path('module', 'event_calendar');
            $markup = '<div id="content">
                    <div style="float:right;margin-bottom:5px">
                        <label style="float:left;padding:3px;">Week :</label>
                        <div id="date_picker">
                            <span id="startDate"></span></span> - <span id="endDate"></span>
                            <div class="week-picker" style="display:none;position:absolute"></div>   
                        </div>
                    </div>
                    <hr style="clear:both;"/>
                    <div id="calender_content">

                    </div>
                    <div id="color_code" style="float:right">
                        <div class="lane1 colorbox"></div><div style="float:left">Lane 1</div>
                        <div class="lane2 colorbox"></div><div style="float:left">Lane 2</div>
                        <div class="lane3 colorbox"></div><div style="float:left">Lane 3</div>
                        <div class="lane4 colorbox"></div><div style="float:left">Lane 4</div>
                    </div>
                </div>';
      $block['content'] = array(
        '#markup'   => $markup,
        '#attached' => array
            (
            'css' => array($basepath . '/css/event_calendar.css',$basepath . '/css/smoothness/jquery-ui-1.8.16.custom.css'),
            'js'  => array($basepath . '/javascript/event_calendar.js',$basepath . '/javascript//jquery-ui-1.8.16.custom.min.js'),
            ),
        );

          }
return $block;
      }

    } 

I want to know how to write functions inside above .module file to access some content dynamically through jquery get or post method?

Or should I use something like 'hook_menu' other than block?

1 Answer 1

3

For using get data by jquery ajax you need first seta menu link for listen to your ajax requst, and in callback of your menu you passed back a block or any data you want.

function yourmodule_menu () {
   $items=array();
   $items['youruniqepath'] =array(
        'title' => 'my menu',
        'description' => 'A menu link to handle ajax request',
        'page callback' => 'yourhandlerfunction',
        'access callback' => TRUE, //you can set it with your permission
      );
   return $items;
   }

and in yourhalderjust need pass a block :

function yourhandlerfunction () {
  //with any method you want get your block,
  // I suggest something like this
    $block = module_invoke('module_name', 'block_view', 'block_delta');
    print render($block);
}

and in your clientside get it with something like this

$.ajax({
    type: "POST",
    url: 'youruniqepath',
    //data: {}, you can also pass block name and act more dynamicly
    success: function (data){
        $('#yourplace').html(data);
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.