1

I want to know how to use hook_js_alter to change the localSettings variable in autologout.js.

In autologout.module in line number ( 377 - 389)

$settings = array(
    'timeout' => $refresh_only ? ($timeout * 500) : ($timeout * 1000),
    'timeout_padding' => $timeout_padding * 1000,
    'message' => t('@msg', array('@msg' => $msg)),
    'redirect_url' => url($redirect_url, array('query' => $redirect_query)),
    'title' => t('@name Alert', array('@name' => variable_get('site_name', 'Drupal'))),
    'refresh_only' => $refresh_only,
    'no_dialog' => $no_dialog,
  );
drupal_add_js(array('autologout' => $settings), 'setting');
drupal_add_js(drupal_get_path('module', 'autologout') . "/autologout.js");

In this it passes the settings variable to the javascript file.

I want to change " 'title' => t('@name Alert', array('@name' => variable_get('site_name', 'Drupal')))," attribute using "hook_js_alter" in my custom module so that javscript takes the new value.

  • 1
    Replace the file if you need to, monkey patch the appropriate method if you don't. Where are you stuck exactly? Please edit the question to provide more details – Clive Feb 25 '15 at 9:38
  • Hello Clive, I have edited the question. Can you please check if this would suffice? – Storm Light Feb 25 '15 at 9:48
  • Thanks Clive, please let me know the best possible solution in this situation. – Storm Light Feb 25 '15 at 10:08
1

The structure of the $javascript array passed to hook_js_alter() is similar to the following one. (I reported only the relevant part of the array.)

array(
  'settings' => array(
    'data' => array(
      0 => array('basePath' => '/'),
      1 => array ('pathPrefix' => ''),
      2 => array('ajaxPageState' => array ('theme' => 'bartik', 'theme_token' => 'oFpiDA70-NnnvSdIL6bFt1oLccvTT5SZeOp3G34cOfs')),
      3 => array ('overlay' => array('paths' => array('admin' => 'node/*/edit node/*/delete node/*/revisions node/*/revisions/*/revert node/*/revisions/*/delete node/add node/add/* overlay/dismiss-message user/*/shortcuts admin admin/* batch taxonomy/term/*/edit user/*/cancel user/*/edit user/*/edit/*', 'non_admin' => 'admin/structure/block/demo/* admin/reports/status/php'))),
      4 => array('overlay' => array('pathPrefixes' => array())),
      5 => array('overlay' => array('ajaxCallback' => 'overlay-ajax'))
    ),
    'type' => 'setting',
    'scope' => 'header',
    'group' => -100,
    'every_page' => true,
    'weight' => 0
  )
  // ...
);

I would use the following code for hook_js_alter().

if (isset($javascript['settings']['data']) && is_array($javascript['settings']['data'])) {
  foreach ($javascript['settings']['data'] as $index => $settings) {
    if (isset($settings['autologout']['title']) && is_array($settings['autologout'])) {
      $javascript['settings']['data'][$index]['autologout']['title'] = 'Your value';
      break;
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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