Drupal: не может удалить файл js и использовать новые js в каталоге темы - PullRequest
2 голосов
/ 28 сентября 2010

Я хочу скопировать файл js в папку с моей темой вместо взлома модуля. Это мой код:

 /*update js files */
  $scripts = drupal_add_js();

  unset($scripts['module']['sites/all/modules/imagefield_crop/imagefield_crop.js']);
  $scripts['module']['sites/all/themes/zen/zen/js/imagefield_crop.js'] = array('preprocess' => 1, 'cache' => 1);

  $vars['scripts'] = drupal_get_js('header', $scripts);

Это работает для lightbox2, но не работает для imagefield_crop.js

Я очистил все кэши Drupal и кеш браузера, но мой браузер продолжает загружать исходные файлы js в каталог модуля.

спасибо

Обновление : это массив $ scripts

['module']

...

    [sites/all/modules/imagefield_crop/Jcrop/js/jquery.Jcrop.js] => Array
                (
                    [cache] => 1
                    [defer] => 
                    [preprocess] => 1
                )

Ответы [ 2 ]

2 голосов
/ 28 сентября 2010

Учитывая обновленный вопрос после обсуждения в комментариях, кажется, что вы перепутали вовлеченные файлы js. Imagefield_crop добавляет два разных:

  1. jquery.Jcrop.js, который является импортированным библиотечным файлом, обеспечивающим функциональность обрезки в целом (в контексте jquery) - обычно у вас не должно быть причин для замены этого.
  2. 'imagefield_crop.js', который обеспечивает «мостовое соединение», позволяющее вышеуказанной библиотеке работать должным образом в контексте Drupal - я понимаю, что вы хотели заменить эту.

Оба необходимы для работы функциональности. Ваш опубликованный код заменит только второй, и если вы случайно не разместили неверный фрагмент кода в обновлении своего вопроса, он, похоже, будет работать.

Если вы хотите заменить оба (или только первый), вам нужно расширить / настроить логику сброса, чтобы сделать это.

1 голос
/ 28 сентября 2010

Здравствуйте, вот возможные решения, которые могут помочь, хотя я никогда не делал этого раньше


/**
 * Implementation of hook_theme_registry_alter().
 * Based on the jquery_update module.
 *
 * Make this page preprocess function runs *last*,
 * so that a theme can't call drupal_get_js().
 */
function MYMODULE_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['page'])) {
    // See if our preprocess function is loaded, if so remove it.
    if ($key = array_search('MYMODULE_preprocess_page', 
      $theme_registry['page']['preprocess functions'])) {
      unset($theme_registry['page']['preprocess functions'][$key]);
    }
    // Now add it on at the end of the array so that it runs last.
    $theme_registry['page']['preprocess functions'][] = 'MYMODULE_preprocess_page';
  } 
}

/**
 * Implementation of moduleName_preprocess_hook().
 * Based on the jquery_update module functions.  *
 * Strips out JS and CSS for a path.
 */
function MYMODULE_preprocess_page(&$variables, $arg = 'my_page', $delta=0) {

  // I needed a one hit wonder. Can be altered to use function arguments
  // to increase it's flexibility.
  if(arg($delta) == $arg) {
    $scripts = drupal_add_js();
    $css = drupal_add_css();
    // Only do this for pages that have JavaScript on them.
    if (!empty($variables['scripts'])) {
      $path = drupal_get_path('module', 'admin_menu');
      unset($scripts['module'][$path . '/admin_menu.js']);
      $variables['scripts'] = drupal_get_js('header', $scripts);
    }
    // Similar process for CSS but there are 2 Css realted variables.
    //  $variables['css'] and $variables['styles'] are both used.
    if (!empty($variables['css'])) {
      $path = drupal_get_path('module', 'admin_menu');
      unset($css['all']['module'][$path . '/admin_menu.css']);
      unset($css['all']['module'][$path . '/admin_menu.color.css']);
      $variables['styles'] = drupal_get_css($css);
    }
  }
}

http://www.mediacurrent.com/blogs/remove-or-replace-jscss-page

...