Community Documentation

Allow File Downloads if User has Flagged the Node

Last updated March 19, 2012. Created by queryblitz on March 12, 2012.
Log in to edit this page.

This tutorial will show you how to offer file downloads to users who have flagged the current node. Uses for this might include having a "Buy This Item" flag and then displaying the file after the user has "bought" it.

Basically what we're going to do is set a file download CCK field to show in a block, and then set the block to display to a user only after they have flagged the node containing that field.

First, create a node type and add a file download CCK field to it.

Next, add a node flag and enable it for the node type containing the file download field. This is the flag users will have to flag in order to download the file, so you might call it something like "Buy" or "Purchase". (See this tutorial to learn how to charge userpoints when users flag the node.)

Install and enable the CCK Blocks module.

Edit the file download CCK field and set it to show as a block.

On the block settings page, set the "Show block on specific pages" to PHP, and enter this code into the text area:

<?php
 
if ((arg(0) == 'node') && is_numeric(arg(1))) {
   
$flag = flag_get_flag('name-of-flag');
    if (
$flag && $flag->is_flagged(arg(1))) {
      return
TRUE;
    }
  }
?>

Change where it says name-of-flag to the machine name of the flag you're using.

See example at queryblitz.com/store.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Designers/themers, Programmers, Site administrators
Keywords
CCK Blocks, Flag

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.