0

I have below code coming from External Link module in Drupal 7:

/**
* Implements hook_page_build().
*/
function extlink_page_build() {
   $path = drupal_get_path('module', 'extlink');
   drupal_add_js($path .'/extlink.js', array('every_page' => TRUE));
   drupal_add_js(array('extlink' => array(
     'extTarget'     => variable_get('extlink_target', 0),
     'extClass'      => variable_get('extlink_class', 'ext'),
     'extLabel'      => check_plain(variable_get('extlink_label', t('(link is external)'))),
     'extImgClass'   => variable_get('extlink_img_class', 0),
     'extSubdomains' => variable_get('extlink_subdomains', 1),
     'extExclude'    => variable_get('extlink_exclude', ''),
     'extInclude'    => variable_get('extlink_include', ''),
     'extCssExclude' => variable_get('extlink_css_exclude', ''),
     'extCssExplicit' => variable_get('extlink_css_explicit', ''),
     'extAlert'      => variable_get('extlink_alert', 0),
     'extAlertText'  => variable_get('extlink_alert_text', 'This link will take you to an external web site. We are not responsible for their content.'),
     'mailtoClass'   => variable_get('extlink_mailto_class', 'mailto'),
     'mailtoLabel'   =>   check_plain(variable_get('extlink_mailto_label', t('(link sends e-mail)'))),
   )), 'setting');
}

I want to add below code from above function only if my some condition is true (which will be based on some user filed) , without overriding files of module it self:

drupal_add_js(array('extlink' => array( 'extTarget' => variable_get('extlink_target', 0),)), 'setting');

I am not getting any solution how to override this function thorough my custom module, Please suggest !

To make more clear I just want to add condition in above function so that js will only get added if condition according to user field is true.

UPDATE: I have tried like below code but its not working, please suggest what I am doing wrong My custom module name: custom_overrides

function custom_overrides_page_alter(&$page) {
   global $user;
   $userid = $user->uid; 

    if($userid){
        $path = drupal_get_path('module', 'extlink');
        drupal_add_js($path .'/extlink.js', array('every_page' => TRUE));
        drupal_add_js(array('extlink' => array('extTarget' => variable_get('extlink_target', 0),)), 'setting');
  }
}

Thanks!

2 Answers 2

0

In hook_page_alter you can override js settings just by using drupal_add_js like you pasted above.

8
  • Thanks for your help , but I already tried like this. I have update code in questions, please suggest what I am doing wrong ? Thanks!
    – jas
    Commented Oct 26, 2015 at 11:14
  • I think its hook_hook_page_alter or like you provided hook_page_build_alter
    – jas
    Commented Oct 26, 2015 at 11:21
  • you're using hook_page_alter, try hook_page_build_alter
    – rémy
    Commented Oct 26, 2015 at 16:23
  • Thx but It seems there is no hook like page_build_alter in your provided link as well, but I tried still no effect , any other ideas ?
    – jas
    Commented Oct 26, 2015 at 16:33
  • sorry you're right .. should be hook_page_alter - is your code executed ? what's inside drupal_get_js ? why are you readding the extlink.js ?
    – rémy
    Commented Oct 26, 2015 at 16:47
0

Finally I found solution to my question in this way:

function news_js_alter(&$javascript) {

   $path = drupal_get_path('module', 'extlink');
   global $user;
   $userid = $user->uid;
   if (!$userid) {
    unset($javascript[$path . '/extlink.js']);
   }
}

Module was adding js settings, so I wanted to add only when user was loged in, so now in my theme news I have added above code in template.php and it worked. May this help other users trying to solve similar conditions.

Thanks!

1
  • Is there any other way how I can unset only required variable only like "extTarget" in js settings with js_alter rather than unset complete file ? If some one having look on this please suggest ?
    – jas
    Commented Oct 26, 2015 at 19:12

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.