01/10/2018, 17:29

Tại sao không được dùng trùng tên file css cho theme và module trong Drupal 7

Hôm nay mình tạo một module trong Drupal. Trong module của mình có một file css tên là style.css. Và mình dùng drupal_add_css() để load file css này. Tuy nhiên, mình làm mãi mà file này vẫn không được load. Đoạn mã của mình như sau: drupal_add_js(drupal_get_path('module', 'shoppingcart') . ...

Hôm nay mình tạo một module trong Drupal. Trong module của mình có một file css tên là style.css. Và mình dùng drupal_add_css() để load file css này. Tuy nhiên, mình làm mãi mà file này vẫn không được load. Đoạn mã của mình như sau:

 drupal_add_js(drupal_get_path('module', 'shoppingcart') . '/js/jquery-impromptu.3.1.min.js');
 drupal_add_js(drupal_get_path('module', 'shoppingcart') . '/js/nkh_simple_paypal_shopping_cart.js');
 drupal_add_css(drupal_get_path('module', 'shoppingcart') . '/css/jquery.impromptu.css');
 drupal_add_css(drupal_get_path('module', 'shoppingcart') . '/css/style.css');

Mình nghĩ mãi không biết có sai đường dẫn hay không, có bị đụng gì hay không. Thế là mình bắt tay vào debug hàm drupal_add_css()

Note that Modules should always prefix the names of their CSS files with the module name; 
for example, system-menus.css rather than simply menus.css. Themes can override module-supplied 
CSS files based on their filenames, and this prefixing helps prevent confusing name collisions for 
theme developers. See drupal_get_css() where the overrides are performed....

Mình tiếp tục debug drupal_get_css() thì thấy là nó đã bị unset nếu trùng tên

$previous_item = array();
  foreach ($css as $key => $item) {
    if ($item['type'] == 'file') {
      // If defined, force a unique basename for this file.
      $basename = isset($item['basename']) ? $item['basename'] : basename($item['data']);
      if (isset($previous_item[$basename])) {
        // Remove the previous item that shared the same base name.
          
        unset($css[$previous_item[$basename]]);
      }
      $previous_item[$basename] = $key;
    }
  }

Hàm drupal_get_css() lại sẽ được gọi trong file include/theme.inc. Vậy nó sẽ là hàm cuối cùng được gọi để tạo các đường dẫn đến css

Cuối cùng mình phát hiện ra theme của mình có một file css cũng có tên là style.css nó đã ghi đè file style.css của module mình

Sau khi đổi lại tên file css của module thì file css được load lên.


0