I don't know if there's an issue like mine, I tried to search without results.

I have a block that runs custom php code and it generates a link using the global variables of drupal, the link is composed like:
echo "<a href='" . $base_url . "/" . $requestpath . "?action=read' >";

The block runs well until it is refreshed and it generates, for example, the link http://mysite.com/node/1?action=read

After the refresh the link transforms into http://mysite.com/block_refresh/block/2/action=read

Is it a known bug or is there a workaround?

Comments

tripper54’s picture

Title: Update generate wrong url » Add instructions for retrieving page path in block code
Component: Code » Documentation
Category: Support request » Task

The path you're seeing is the page callback to regenerate the block via AJAX. The solution here is to use the 'send path arguments to block' setting:

1. In the block refresh settings for the block, make sure 'Send page path arguments to block' is checked.
2. Your generated link will now have the original path appended to it, eg http://mysite.com/block_refresh/block/2/node/1?action=read
3. Now just cut the block refresh portion of the argument out of $requestpath, like

$trimmed_path = str_replace('/block_refresh/block/2','', $requestpath);

Note you should really use drupal's l() function to generate links :) https://api.drupal.org/api/drupal/includes%21common.inc/function/l/7

I'll mark this as a documentation task as this stuff should be covered in the README.

smurfxx’s picture

Component: Documentation » Code
Category: Task » Support request

Thank you very much! I didn't think about an str_replace! Now it works very well!!!

tripper54’s picture

Component: Code » Documentation
Category: Support request » Task

Glad I could help.