0

I am using drupal - 7.

I wanted to embed <script> tags for Retargeting tag into each page of the Website, between the <body> </body> tags.

I am new to Drupal. I am trying to create custom block module for this.

I have created custom module to place tags into custom block. I am not sure what I have done is right or wrong. Can any one help me with this?

I just wanted to add script like this into each page for Retargeting tags for campaigns

<script async src="http://i.simpli.fi/dpx.js?cid=0&segment=Segment+Name&campaign_id=0"></script> 

I have added script tag into my js file which I have called from .info file. In my .info file I called;

scripts[] = MyCustomBlock.js

This is the code for my custom block module.

/**  * Implements hook_help().  *  * Displays help and module
information.  *  * @param path   *   Which path of the site we're
using to display help  * @param arg   *   Array that holds the current
path as returned from arg() function  */    

function MyCustomBlock_help($path, $arg) {   
  switch ($path) {
    case "admin/help#MyCustomBlock":
      return '<p>' . t("Displays links to nodes created on this date") . '</p>';
      break;   
  } 
}

/**
* Implements hook_block_info().
*/
/**  * Declaring the block  */
function MyCustomBlock_block_info() {
  $blocks['MyCustomBlock'] = array(
    // The name that will appear in the block list.
    'info' => t('MyCustomBlock'),
    // Default setting.
    'cache' => DRUPAL_CACHE_PER_ROLE,   
  );   
  return $blocks; 
} 

/**  * Preparing for a module configuration form  * Implements
hook_menu().  */

function MyCustomBlock_menu() {   
  $items = array();

  $items['admin/config/content/MyCustomBlock'] = array(
    'title' => 'MyCustomBlock',
    'description' => 'Configuration for MyCustomBlock module',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('MyCustomBlock_form'),
    'access arguments' => array('access administration pages'),
    'type' => MENU_NORMAL_ITEM,   
  );

  return $items; 
}


/*  * Adding JavaScript from within a module  */

function MyCustomBlock_add_js($data = NULL, $options = NULL) {   
    $javascript = &drupal_static(__FUNCTION__, array());   
    $jquery_added = &drupal_static(__FUNCTION__ . ':jquery_added', FALSE);
}

I can see my block module in the /admin/modules section and I can enable my custom block module. But, I am not sure how to add that script into my module.

Thanks for the help!

1
  • unless you are running multiple themes, why not just add it to a custom html.tpl.php in the theme directory - you decide exactly what you want, and where you want it, without having to add a new module...
    – Geoff
    Commented Apr 27, 2015 at 20:36

1 Answer 1

1

drupal_add_js() doesn't support the async property (there is a patch however) but the simplest solution would be to just just add that line to your html.tpl.php as Geoff suggested.

3
  • But, wouldn't it break, If I add it in html.tpl.php file? Or should I create just copy of html.tpl.php file? - I am newbie so apologize for some silly questions.
    – Vidushi
    Commented Apr 28, 2015 at 17:21
  • I am getting this error with my custom module: Notice: Undefined index: mycampaign_form in drupal_retrieve_form() (line 806 of ../includes/form.inc). Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'mycampaign_form' not found or invalid function name in drupal_retrieve_form() (line 841 of ../includes/form.inc). I have just changed the name of module from 'MyCustomBlock' to 'mycampaign' because of camel-case in the module name.
    – Vidushi
    Commented Apr 28, 2015 at 17:21
  • Copy html.tpl.php into your own theme and edit it there. Just one question per thread please.
    – Felix Eve
    Commented Apr 28, 2015 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.