I've created a custom block and put this code into php input:

if (arg(0) == 'node' && is_numeric(arg(1))) {
  //load $node object
  $node = node_load(arg(1));
  //check for node update access
  if (node_access("update", $node)){
    $nid = $node->nid;
    print l(t('edit'), "node/$nid/edit") .' '. l(t('delete'), "node/$nid/delete");
  }
}

This code works without ajaxify. When ajaxified, it outputs nothing. The block shows up, but is empty. What's wrong?

CommentFileSizeAuthor
#14 ajaxify_regions.patch1.94 KBToxid
#12 ajaxify_regions.patch1.66 KBToxid

Comments

Toxid’s picture

I tried print_r ($node); and it seems that it's empty. Is there another way to load $node?

mikeytown2’s picture

Something like this would require a custom handler OR for the Ajaxify logic to be a lot smarter... which could happen.

http://api.drupal.org/api/function/arg/6
Store the q variable in the jquery drupal settings array and then on hook_init set $_GET['q'] back to the stored value on Ajaxify callback. In boost stats I do store the q variable, so 90% of the logic has already been figured out.

Toxid’s picture

Ah, so it was the arguments that was the problem. I'm not a coder, so I'm not quite sure how to do what you describe, but thanks for pointing me in the right direction. I'll have a look at boost stats to see how it's done.

Toxid’s picture

I couldn't figure this one out. I took some code from the boost module and pasted into ajaxify regions, but I can't get the values to Drupal.settings. I removed the code I thought was unessessary, such as the if statements.

//Generate js for q

function ajaxify_regions_generate() {
Global $base_path;

  // define variables
  
    $nid = arg(1);
	$title = drupal_urlencode(strip_tags(drupal_get_title()));
    $q = $_GET['q'];
	
 // variables to insert in Drupal.settings
  $page_js = array(
    'ajaxify' => array(
      'nid' => $nid,
      'q' => $q,
      'title' => $title,
    ),
  );
  
  // Not sure about this piece
  $site_js = <<<ETO
$.getJSON(Drupal.settings.basePath + "$filename", {nocache: "1", js: "1", nid: Drupal.settings.ajaxify.nid, qq: Drupal.settings.ajaxify.q, title: Drupal.settings.ajaxify.title, referer: document.referrer}, function(response) {
  $.each(response, function(id, contents) {
    if (contents == 'NULL') {
      $(id).parent().parent().hide();
    }
    else {
      $(id).html(contents);
    }
  });
});
ETO;

  // page specific variables. Add the values to Drupal.settings
  drupal_add_js($page_js, 'setting');
  // site-wide code
  drupal_add_js($site_js, 'inline', 'footer');
}

So drupal_add_js($page, 'setting'); should insert the values in the Drupal.settings array, but I can't see them when I look in the source code.

EDIT: I noticed my mistake now. I removed the sitewide code, and renamed the function so to ajaxify_regions_init. Now I get the $nid printed to Drupal.settings.

mikeytown2’s picture

You should be able to get it working with the following as a guide

What we want is q; but we'll call it qq since q is used by drupal via .htaccess rules.

  // define variables
  $qq = $_GET['q'];

  // variables to insert in Drupal.settings
  $page_js = array(
    'ajaxify' => array('qq' => $qq),
  );

  // page specific variables. Add the values to Drupal.settings
  drupal_add_js($page_js, 'setting');

This makes $_GET['q'] available to jQuery. Next step is to pass this back to ajaxif's callback ajaxify_regions_ajax_handler() and have that function process it.

          drupal_add_js('
          var blocks = "blocks='. implode('/', $ajax_blocks) .'";
          var qq = "qq=Drupal.settings.ajaxify.qq";
          $(function(){$.ajax({
            url: Drupal.settings.basePath + "'. $url .'",
            type: "GET",
            dataType: "json",
            data: blocks + "&" + qq ,
            success: function(data) { ajaxify_regions_success_block(data); }
          })});', 'inline');

Finally we need to process the qq variable and set that to q

// Set the context so blocks that use arg() will be correct.
if (isset($_GET['qq'])) {
  $_GET['q'] = $_GET['qq'];
}
// Force arg to get "reset"
arg(0, $_GET['q']);
Toxid’s picture

Oh, this is a pretty easy code, thanks. So we're overwriting ajaxifys' js to make it add the q value. When looking in firebugs console, I can see that qq is "node/42". But the code isn't working yet, drupal gives me an error that "var blocks = "blocks='. implode('/', $ajax_blocks) .'";" is an invalid argument. I've experimented with it but cannot get it right. The block still shows nothing.

mikeytown2’s picture

ajaxify_regions_preprocess() is where the var blocks = "blocks='. implode('/', $ajax_blocks) .'"; lives

Toxid’s picture

It appears I was wrong, drupal really only gave me a row number so I pasted the code at that row. But then I changed this part: var qq = "qq=Drupal.settings.ajaxify.qq"; and removed the qq=, then the code works and the console shows that the script loads correctly. This is what it looks like in the console:
GET http://localhost/xnalarapressflow/?blocks=block-18/nice_menus-1&Drupal.s...

block-18 and nice_menues-1 are the blocks I've ajaxified. For some reason, the q value isn't converted.

mikeytown2’s picture

var qq = "qq=" + Drupal.settings.ajaxify.qq;

Toxid’s picture

I suppose I replace the drupal_add_js in the preprocess function with the new js. That works, I get "&qq=node/42" now. However, it seems to have been caught in some kind of loop, because the content is never loaded. The ajax loading image just keeps spinning. In the firebug console, the script finishes okay.

mikeytown2’s picture

Create a patch of what you got so far and I'll try to get some time to make it fly

Toxid’s picture

StatusFileSize
new1.66 KB

Thanks, that would be great :) I'm attaching the patch. I haven't done much really, just replace the drupal_add_js in ajaxify_region_preprocess function and then added an ajaxify_regions_init function at the end to generate the variables.

mikeytown2’s picture

First issue: place the $_GET['q'] = $_GET['qq']; in ajaxify_regions_ajax_handler. you don't need to use hook_init. do it after $_GET['q'] gets used in that function.

Toxid’s picture

StatusFileSize
new1.94 KB

You did it! It works. Thank you very much! This module just got a whole lot better. I moved the code to reset arg into the handler function too. I'm attaching the final patch here.

Toxid’s picture

Status: Active » Fixed
mikeytown2’s picture

Status: Fixed » Needs review

marking this "needs review" since it would be nice to have this functionality in this module

mikeytown2’s picture

Title: Custom block - when used with ajaxify regions it outputs nothing » set $_GET['q'] to the initial value so arg() has the correct context
csevb10’s picture

Status: Needs review » Fixed

Ok, so the very original versions actually did exactly this, but the logic got lost somewhere along the way.
I committed what I think is a simpler/cleaner version here: http://drupal.org/cvs?commit=396738

Take a look at the diff and what I did to accomplish it, then let me know if there is anything that you feel could be done better:
http://drupalcode.org/viewvc/drupal/contributions/modules/ajaxify_region...

mikeytown2’s picture

I've found $_SERVER['HTTP_REFERER'] to not be 100% reliable (what referer_uri() uses).

You might also want to consider using javascript to get the current URL http://stackoverflow.com/questions/406192/how-to-get-the-current-url-in-... If going this route or using the code you currently have I would opt to set 'q' and then run drupal_init_path() again.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

asak’s picture

@mikeytown2: This issue is closed, but it doesn't seem like your last suggestion was considered.

If I understand you correctly, you're saying that line 61 in the _ajax_handler function - $_GET['q'] = $ref_uri; - should be followed by a simple call to drupal_init_path(); ? is that it?

I seem to be bumping into you more often lately on d.o - I like it. you're doing awesome work ;)

mikeytown2’s picture

To answer your question, yes do this

$_GET['q'] = $ref_uri;
drupal_init_path(); 

Good example is the front page. $_GET['q'] will be empty, but after running drupal_init_path(), $_GET['q'] will then be set correctly.

Also
I highly recommending passing document.referrer back in the data object of your ajax call... or at least having that as an option. Reason I say option has to do with caching of the ajax. If the block returns differently depending on where it thinks it is, and the ajax is cached, then the wrong info could show up. Granted this isn't an issue right now because the usage for this project is fairly small and blocks that use arg() is not a common thing and caching the output of this is not that common for people who use this module. Also if none of my blocks use arg(), and I do cache, then I would want returning document.referrer in GET parameter off so my cache hit rate will be higher for that set of blocks.

Long story short, I hope this project goes to a per block configuration style; it would offer more configuration options... although I do like the ability to do * and get all block variants.