You are here

function dynamic_background_create_css in Dynamic Background 7.2

Same name and namespace in other branches
  1. 6 dynamic_background.module \dynamic_background_create_css()
  2. 7 dynamic_background.module \dynamic_background_create_css()

Helper function that creates CSS based on module configuration and image selection.

Parameters

array $images_conf:

bool $reset: Optional to reset the static cache used.

Return value

string The CSS or FALSE if custom CSS have not been defined.

2 calls to dynamic_background_create_css()
dynamic_background_css in ./dynamic_background.module
Menu callback function used to generate an body style css with the selected background image. The callback is /background.css.
dynamic_background_preprocess_html in ./dynamic_background.module
Implements hook_preprocess_html().

File

./dynamic_background.module, line 553
This module enables administrators to upload images used as background on the site. The selected background image link is exposed as either $background in the page.tpl file or as /background.css.

Code

function dynamic_background_create_css($images_conf, $reset = FALSE) {
  static $css;
  if (!isset($css) || $reset) {

    // Build style array based on weight, this will allow weight base override
    // at the same time allowing different selectors.
    $style_array = array();
    foreach ($images_conf as $image_conf) {

      // Add image style, if one have been defined.
      $image = $image_conf['image'];
      if (isset($image_conf['image_style']) && $image_conf['image_style']) {

        // Image style found, so update the image path with an image style
        // based one.
        $image->url = image_style_url($image_conf['image_style'], $image->uri);
      }

      // Only use image if css behaviour have be set.
      if (!empty($image_conf['configuration'])) {

        // Check if selector have been used, if it have and has a higher weight
        // override it.
        if (isset($style_array[$image_conf['configuration']['selector']])) {
          if ($style_array[$image_conf['configuration']['selector']]['weight'] > $image_conf['weight']) {
            $style_array[$image_conf['configuration']['selector']] = array(
              'css' => $image_conf['configuration']['css'],
              'image' => $image,
              'weight' => $image_conf['weight'],
            );
          }
        }
        else {
          $style_array[$image_conf['configuration']['selector']] = array(
            'css' => $image_conf['configuration']['css'],
            'image' => $image,
            'weight' => $image_conf['weight'],
          );
        }
      }
    }

    // Build css based on weighted style array.
    $css = '';
    foreach ($style_array as $selector => $style) {
      $style['image']->url = isset($style['image']->url) ? $style['image']->url : file_create_url($style['image']->uri);
      $css .= $selector . " {\n        background-image: url('" . $style['image']->url . "');\n        " . $style['css'] . "\n      }\n";
    }
  }
  return $css;
}