[Drupal] How to display Affiliate links on page load for the contents using Category as reference id

| | 3 min read

In my previouds article "How to display Affiliate Links on page load for the contents using Category as reference id-Part 1?" I have provided a brief intrduction on affliate links and what all configurations we must do. In this article I will be explaining how to generate Affiliate Link on page load using hook_nodeapi().

Hope you all remember the configurations we did in the first part and how we generated the patterns using preg_match(). If not please refer the part 1 of this article as this is the continuation of part 1.

After the part 1 you know how we generated the patterns from node title now we have to get the corresponding urls using category as reference.For this we write a query to get all the Affiliate links and Affiliate Image by using category as the reference id.



  $affliatelink = db_query('SELECT node.title AS node_title, files.filepath AS files_affliate_path, content_type_affiliate.field_affiliate_url_template_value AS field_affiliate_url_template_value FROM {content_type_affiliate} content_type_affiliate LEFT JOIN {node} node ON node.vid = content_type_affiliate.vid LEFT JOIN {files} files ON files.fid = content_type_affiliate.field_affiliate_button_fid LEFT JOIN {term_node} term_node ON term_node.vid = content_type_affiliate.vid WHERE term_node.tid = %d', $category);
  
  /*Query to get corresponding affliate links or URL Template and Affliate Image by using category as reference id category*/
  while ($affliatelinkurl = db_fetch_object($affliatelink)) {
    print $affiliatelinkurl->field_affiliate_url_template_value;
    print $affiliatelinkurl->files_affiliate_path;
  }

In content type "Affiliate Links" you know how we are savung URL Template, it will be either of this two formats

Once we get the corresponding links next step is to replace the [title-for-url-search] and [truncated-title] with the generated title. Its done using str_replace() which is shown below



 /*Replacing url search title in affliate link with generated node page title*/
 $patterns="[title-for-url-search]";
 $replacements=$titleforurlsearch;
 $string=$affliatelinkurl->field_affiliate_url_template_value;
 $fullurl = str_replace($patterns, $replacements, $string);
 
 /*Replacing truncated title in affliate link with generated node page title*/
 $patterns="[truncated-title]";
 $replacements=$truncatedtitle;
 $string=$fullurl;
 $fullurl = str_replace($patterns, $replacements, $string);
 $affliatetitle = $affiliatelinkurl->node_title;
 $files_affiliate_path = $affiliatelinkurl->files_affiliate_path;
 $node->titleforurlsearch[] = array(url=>$fullurl,title=>$affiliatetitle,path=>$files_affiliate_path);

The result we obtained here are Affiliate URL, Affliate Image and Node Title. Now we have to display the result in our node page. For this we pass the result as a multi dimensional array.


  $node->titleforurlsearch[] = array(url=>$fullurl,title=>$affiliatetitle,path=>$files_affiliate_path);

Now to disply the result in our node page, we can call this in our node.tpl.php file as shown below.



 if ($node->titleforurlsearch):
  foreach ($node->titleforurlsearch as $value) {
    print ''.theme('imagecache', 'affiliate_button', $value['path'], 'Buy from '.$value['title'], 'Buy from '.$value['title']).' ';
  }
 endif;

Next question that comes to your mind how can we implement this, where should we call this, how can we relate all this and so on. Simple create a custom module write the following code in a hook_nodeapi() and our function is ready.



/**
 * Implementation of hook_nodeapi().
 */

function custom_module_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

  switch ($op) {
    case 'view':
      if (($node->type == 'event')) {
      
        /*checking content type is event*/
        $node->title=trim($node->title,"  ");
        $nodetitle = explode(',',$node->title);
        $patterns='( - | )';
        $replacements='+';
        $string=$nodetitle[0];
        $nodetitleset = preg_replace($patterns, $replacements, $string);
        $titleforurlsearch = drupal_strtolower($nodetitleset);
        $node->title=trim($node->title,"  ");
        $truncatedtitle = explode('-',$node->title);
        $truncatedtitle=trim($truncatedtitle[0],"  ");
        $truncatedtitle = drupal_strtolower($truncatedtitle);
        
        /*converting title to the format search url title and truncated title*/
        foreach ($node->taxonomy as $value) {
          $category= $value->tid;
          $vname= $value->name;
          $affliatelink = db_query('SELECT node.title AS node_title, files.filepath AS files_affiliate_path, content_type_affiliate.field_affiliate_url_template_value AS field_affiliate_url_template_value FROM {content_type_affiliate} content_type_affiliate LEFT JOIN {node} node ON node.vid = content_type_affiliate.vid LEFT JOIN {files} files ON files.fid = content_type_affiliate.field_affiliate_button_fid LEFT JOIN {term_node} term_node ON term_node.vid = content_type_affiliate.vid WHERE term_node.tid = %d', $category);
          
          /*Query to get corresponding affliate links or URL Template by using category as reference id category*/
          while ($affliatelinkurl = db_fetch_object($affiliatelink)) {
            $possibilities = taxonomy_get_term($category);
            if (($possibilities->name == 'Books') || ($possibilities->name == 'Recorded Music')) {
              $patterns="[title-for-url-search]";
              $replacements=$titleforurlsearch;
              $string=$affiliatelinkurl->field_affiliate_url_template_value;
              $fullurl = str_replace($patterns, $replacements, $string);
              
              /*Replacing search url title in affiliate link with generated node page title*/
              $patterns="[truncated-title]";
              $replacements=$truncatedtitle;
              $string=$fullurl;
              $fullurl = str_replace($patterns, $replacements, $string);
              $affiliatetitle = $affiliatelinkurl->node_title;
              $files_affiliate_path = $affiliatelinkurl->files_affiliate_path;
              $node->titleforurlsearch[] = array(url=>$fullurl,title=>$affiliatetitle,path=>$files_affiliate_path);
              /*Replacing truncated title in affiliate link with generated node page title*/
            }
            else if (($possibilities->name == 'Live Music')||( $possibilities->name == 'Stage')) {
              $patterns="[title-for-url-search]";
              $replacements=$titleforurlsearch;
              $string=$affiliatelinkurl->field_affiliate_url_template_value;
              $fullurl = str_replace($patterns, $replacements, $string);
              
              /*Replacing search url title in affiliate link with generated node page title*/
              $patterns="[truncated-title]";
              $replacements=$truncatedtitle;
              $string=$fullurl;
              $fullurl = str_replace($patterns, $replacements, $string);
              $affliatetitle = $affiliatelinkurl->node_title;
              $files_affliate_path = $affiliatelinkurl->files_affliate_path;
              $node->titleforurlsearch[] = array(url=>$fullurl,title=>$affiliatetitle,path=>$files_affiliate_path);
              /*Replacing [truncated-title] in affiliate link with generated node page title*/
            }
          }
        }
      }
  }
}

Please fell free to share your thoughts and doubts regarding this here.