A more complete code
Last updated on
30 April 2025
Here's a more complete code than the "pseudo JavaScript code" presented earlier. It shows how to implement the "Basket block" for cached pages. The code has several problems and can do with some improvements.
A friendly reminder: you don't need to use this code --any custom code-- if you don't care about page caching. And surely you don't need to read this section if you don't care about anonymous users at all.
Download, Install, Configure (For Drupal 6)
- Download and install the latest version of Flag 2.x branch
- Download and install the latest version of Session API
- Download and install the latest version of Component Module
- Download and install the latest version of Views Module
- Create a new flag called "basket" at admin/build/flags/add, allow anonymous users to flag/unflag, enable flagging for at least one content type, "Display link on node page", activate "JavaScript toggle"
- Create a node view called "basket" at admin/build/views/add, under "Relationships" add "Flags: Node flag", call it "basket" and select "basket" as flag, "Include only flagged content", by "Current user"
- Under "Relationships" add "Flags: User", select "basket" as Relationship
- Add a "Empty text" to your view "Basket is empty"
- Add a "Block" display to the view, and add some fields to it (e.g. Node Title, Flag link)
- Do NOT (!) enable the block at admin/build/block
Some Custom Code
- Create a small custom module called basket_module (create a folder called basket_module at sites/all/modules)
- Create a basket_module.info file in this folder
; $Id$ name = Basket description = "Implements a dynamic basket block." core = 6.x - Create a basket_module.module file in this folder
<?php /** * Implementation of hook_init(). */ function basket_module_init() { if ($GLOBALS['user']->uid == 0) { // If the user is anon, tell Flag's JS (the anti-crawlers mechanism) to kick in. drupal_add_js(array('flag' => array('anonymous' => 1)), 'setting'); } $basket_fetcher = url('component/block/views/basket-block_1', array('query' => drupal_get_destination())); drupal_add_js(array('basket' => array('basket_fetcher' => $basket_fetcher)), 'setting'); drupal_add_js(drupal_get_path('module', 'basket_module') . '/basket.js'); } - Create a basket.js file in this folder
$(document).bind('flagGlobalAfterLinkUpdate', function(event, data) { // The following line is needed for compatibility with Drupal 5, // which doesn't have 'Drupal.settings.basePath'. Drupal 5 users // must update the '/' to match their bash path. var base_url = (Drupal.settings && Drupal.settings.basePath) || '/'; function blockExistsOnPage(module_name, block_id) { var domId = 'block-' + module_name + '-' + block_id; return $('#'+domId).length != 0; } function refreshBlock(module_name, block_id) { var domId = 'block-' + module_name + '-' + block_id; $.get(Drupal.settings.basket.basket_fetcher, function(newHtml) { // Replace the old block with the new one. var domElement = $(newHtml).insertAfter('#'+domId); $('#'+domId).remove(); if (Drupal.attachBehaviors) { Drupal.attachBehaviors(domElement); } }); } if (data.flagName == 'basket') { if (blockExistsOnPage('views', 'basket-block_1')) { refreshBlock('views', 'basket-block_1'); } } }); $(document).ready(function() { $.get(Drupal.settings.basket.basket_fetcher, function(data) { // Make sure the ID of this element (#header) exists on every page! // For example if you want to display this block in "#sidebar-right", // make sure #sidebar-right is actually there. $("#header").prepend(data); if (Drupal.attachBehaviors) { Drupal.attachBehaviors("#block-views-basket-block_1"); } }); }); - Enable the module.
- Add some styles, beautify, enjoy :)
PROBLEMS IN THIS CODE:
- The block is fetched unconditionally from the server. Instead, this should be done only if Flag's cookie indicates the user has flagged content.
Help improve this page
Page status: Not set
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion