0

I am newbie and working on Drupal-7.

I have created custom block module called 'mycampaign' to add script to Retargeting Tags into each page.

Script:

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

But, I am getting this error:

Warning: file_get_contents(\sites\all\modules\mycampaign\mycampaign.js\mycampaign.js) [function.file-get-contents]: failed to open stream: No such file or directory in drupal_build_js_cache() (line 5008 of /mnt/www/html/xyzexample/docroot/includes/common.inc).

Appreciate your help!

.info file for my custom module

name = mycampaign
description = This module provides a custom block with dynamic content. 
package = MyModules 
core = 7.x 
; NEW LINE
configure = admin/config/content/mycampaign
scripts[] = mycampaign.js

File code from my custom module:

<?php

/**
 * 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
 */
  /**
* Implements hook_block_info().
*/

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

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


function mycampaign_block_view($delta = ' ') {
  if ($delta == 'mycampaign_delta') {
    $block = array(
      'subject' => t('mycampaign block title'),
      'content' => array(
        '#markup' => '<p>' . t('mycampaign block output.') . '</p>',
        '#attached' => array(
          'js' => array('\sites\all\modules\mycampaign\mycampaign.js'),
        ),
      ),
      drupal_add_html_head($viewport, 'viewport')
    );
    return $block;
  }
}

/**
 * Implements hook_menu().
 */
function mycampaign_menu() {
 // $items = array();

  $items['admin/config/content/mycampaign'] = array(
    'title' => 'My Campaign',
    'description' => 'Configuration for My Campaign module',
//    'page callback' => '_mycampaign_page',
    'page arguments' => array('mycampaign_form'),
    'access arguments' => array('access mycampaign content'),
        'type' => MENU_NORMAL_ITEM, //Will appear in Navigation menu.
      );
    return $items;

}

function mycampaign_init(){
    drupal_add_js(drupal_get_path('mycampaign', 'mycampaign').'\sites\all\modules\mycampaign\mycampaign.js\mycampaign.js'); 
} 

/**
 * Creating the configuration form
 */

 function mycampaign_contents() {
  //Get today's date.
  $today = getdate();
  //Calculate midnight a week ago.
  $start_time = mktime(0, 0, 0,$today['mon'],($today['mday'] - 7), $today['year']);
  //Get all posts from one week ago to the present.
  $end_time = time();

 //NEW LINE 
 $max_num = variable_get('mycampaign_max', 3);

  //Use Database API to retrieve my campaign.
  $query = db_select('node', 'n')
    ->fields('n', array('nid', 'title', 'created'))
    ->condition('status', 1) //Published.
    ->condition('created', array($start_time, $end_time), 'BETWEEN')
    ->orderBy('created', 'DESC') //Most recent first.
    ->range(0, $max_num) //NEW LINE
    ->execute();
  return $query;
}
/**
 * Validating the data
 */
 /**
 * Implements validation from the Form API.
 * 
 * @param $form
 *   A structured array containing the elements and properties of the form.
 * @param $form_state
 *   An array that stores information about the form's current state 
 *   during processing.
 */
function mycampaign_form_validate($form, &$form_state){
  $max_num = $form_state['values']['mycampaign_max'];
  if (!is_numeric($max_num)){
    form_set_error('mycampaign_max', t('You must enter a number for the maximum number of posts to display.'));
  }
  elseif ($max_num <= 0){
    form_set_error('mycampaign_max', t('Maximum number of posts to display must be positive.'));
  }
}

/**
 * Specifying a custom permission for a new page
 * Implements hook_permission().
 */

function mycampaign_permission() {
  return array(
    'access mycampaign content' => array(
      'title' => t('Access content for the mycampaign module'),
    )
  );
}


/**
* Implements hook_menu().
*/
/*
function mycampaign_menu() {
    $items = array();    
    $items['mycampaign'] = array(
        'title' => 'My Campaign',
        'page callback' => '_mycampaign_page',
        'access arguments' => array('access mycampaign content'),
        'type' => MENU_NORMAL_ITEM, //Will appear in Navigation menu.
      );
    return $items;
}
 * */

And script in JS file

<script async src="http://i.simpli.fi/dpx.js?cid=0&segment=Segment+Name&campaign_id=0"></script>
2
  • Please post your .info file and the code from your .module file that is supposed to add the script. Commented Apr 30, 2015 at 13:40
  • can you manually load the file by entering it in the browser as a url?
    – Geoff
    Commented May 1, 2015 at 0:45

1 Answer 1

1

The error message means that Drupal is unable to locate mycampaign.js.

This may be because you've got the path wrong, or because the file and/or the directory it is in is so well protected that your web server don't have access to it.

In your case, you have the path wrong on two counts. Change:

function mycampaign_init(){
  drupal_add_js(drupal_get_path('mycampaign', 'mycampaign').'\sites\all\modules\mycampaign\mycampaign.js\mycampaign.js'); 
} 

into:

function mycampaign_init(){
  drupal_add_js(drupal_get_path('module', 'mycampaign').'\sites\all\modules\mycampaign\mycampaign.js'); 
} 

Pay attention to the first parameter as well as the path.

You also have two implementations of hook_menu. That should have produced another runtime error.

Since the script is not found, it is not loaded. I.e. at this point, it doesn't matter what it contains.

3
  • Thanks Free Radical & RoloDMonkey. I have edited the question with code files- I just noticed wrong path "\mycampaign.js\mycampaign.js'. It is actually just on '\mycampaign.js' I have changed the corrected path but I am still getting this and won't be able to see my [script] in page source. Warning: file_get_contents(/sites/all/modules/mycampaign/mycampaign.js) [function.file-get-contents]: failed to open stream: No such file or directory in drupal_build_js_cache() (line 5008 of /mnt/www/html/xyzexample/docroot/includes/common.inc).
    – Vidushi
    Commented Apr 30, 2015 at 15:29
  • Yes, I have cleared all cache.
    – Vidushi
    Commented Apr 30, 2015 at 15:41
  • try using `$path = drupal_get_path('module', 'mycampaign');' to get the path to the module directory.
    – Ollie
    Commented Apr 30, 2015 at 16:01

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.