Using drupal_ add_ Css() function to add CSS file

After the project goes online, how to reduce customers’ doubts about the delivery?Here is the secret script of three moves>>>

After seeing the title, some friends may have a question: “in the info file of the theme, can’t you add CSS files for Drupal theme?Lao Liang, why are you so complicated?I just want to learn Drupal’s theme development. When I see PHP functions, I have a big head… “
OK, please listen to me explain the reason:” use Drupal_ add_ The biggest advantage of CSS () function to add CSS files is that we can control the logic and location of CSS files more precisely. ” Let me give you an example. Please look at the code:

function mytheme_preprocess_page(&$variables) {
  if ($variables['is_front']) {
    drupal_add_css(path_to_theme() . '/css/mycss.css', array('group' => CSS_THEME));
  }
}

The context of this code and $variables [‘Is_ The description of the front ‘] variable has been explained in the previous two articles of Drupal topic development tutorial, so I won’t repeat it here. If a friend hasn’t read it, you can find the links to those two articles in the list of related articles at the bottom of this article.

Here I’ll explain the above code: Drupal_ add_ The CSS () function is used to add CSS files. It has two parameters. The first parameter is the path of the CSS file, where the path_ to_ The theme() function is used to get the path of the current topic. The second parameter is an optional configuration option in the form of an array. Through this array, we can control the added CSS file more precisely. For example, in this example, we use the ‘Group’ key to specify the CSS group to which the CSS file belongs.

Finally, explain the implementation effect of the above code: when the website visitors request the home page, add a CSS file named mycss.css for the current Drupal theme, and the file is located in CSS_ The group, not the default CSS_ Default group.

Similar Posts: