You are here

dynamic_background_panels.module in Dynamic Background 7

File

modules/dynamic_background_panels/dynamic_background_panels.module
View source
<?php

/*
 * @file
 * Alters the panels variant "General" form to enable users to select a dynamic
 * background image any given panel variant.
 */

/**
 * Implements hook_permission().
 */
function dynamic_background_panels_permission() {
  return array(
    'configure panels dynamic background' => array(
      'title' => t('Configure panels dynamic background'),
    ),
  );
}

/**
 * Implementation of hook_menu(). Hooks into the dynamic background modules menu
 * structure and adds the "nodes" menu tab to the administration interface.
 *
 * @return array menu items
 */
function dynamic_background_panels_menu() {
  $items = array();
  $items['admin/config/user-interface/backgrounds/panels'] = array(
    'title' => 'Panels',
    'description' => 'Configure dynamic background penels',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_background_panels_admin_form',
    ),
    'access arguments' => array(
      'configure panels dynamic background',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -10,
  );
  return $items;
}

/**
 * Build the administration interface for dynamic background panels and enables
 * administrators enter css to used for insertion of backgrounds.
 *
 * @return array $form
 */
function dynamic_background_panels_admin_form() {
  $form = array(
    '#tree' => TRUE,
  );

  // Add image style to the form.
  $form += dynamic_background_image_style_form('dynamic_background_panels_image_style');

  // Add css behaviour form to the form.
  $form += dynamic_background_css_behaviour_form('dynamic_background_panels_css');
  return system_settings_form($form);
}

/**
 * Implements hook_form_alter().
 */
function dynamic_background_panels_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'panels_panel_context_edit_settings') {

    // Add fieldset.
    $form['conf']['dynamic_background'] = array(
      '#type' => 'fieldset',
      '#title' => t('Dynamic background'),
      '#description' => t('To used this feature, you have to setup custom CSS at dynamic background administratio page.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
    );

    // Find the currently selected image
    $image_id = NULL;
    if (isset($form_state['handler']->conf['dynamic_background'])) {
      foreach ($form_state['handler']->conf['dynamic_background'] as $id => $value) {
        if (isset($value['selected']) && $value['selected']) {
          $image_id = $id;
          break;
        }
      }
    }

    // Add the image selection part of the form
    $form['conf']['dynamic_background'] += dynamic_background_image_selector_form($image_id);

    // Add extra submit handler.
    $form['#submit'][] = 'dynamic_background_panels_conf_submit_handler';
  }
}

/*
 * Ensures that the configuration form panels is saved.
 */
function dynamic_background_panels_conf_submit_handler(&$form, &$form_state) {
  if (isset($form_state['values']['dynamic_background'])) {
    $form_state['handler']->conf['dynamic_background'] = $form_state['values']['dynamic_background'];
  }
}

/**
 * Implements hook_dynamic_background_css().
 */
function dynamic_background_panels_dynamic_background_css($vars) {

  // If the current page has a panel, try to use dynamic background.
  $image_id = NULL;
  if ($pm_page = page_manager_get_current_page()) {
    if (isset($pm_page['handler']->conf['dynamic_background'])) {
      foreach ($pm_page['handler']->conf['dynamic_background'] as $id => $value) {
        if ($value['selected']) {
          $image_id = $id;
          break;
        }
      }
    }
  }

  // Generate the css based in the image id.
  if (!is_null($image_id)) {
    $backgrounds = variable_get('dynamic_background_images', array());
    if (isset($backgrounds[$image_id]['picture'])) {

      // Load image style settings.
      $image_style = variable_get('dynamic_background_panels_image_style', FALSE);
      return array(
        'image' => $backgrounds[$image_id]['picture'],
        'configuration' => variable_get('dynamic_background_panels_css', array()),
        'image_style' => $image_style ? $image_style['style'] : FALSE,
      );
    }
  }
}

/**
 * Implements hook_dynamic_background_weight for the main module.
 */
function dynamic_background_panels_dynamic_background_weight() {
  return array(
    'weight' => -45,
  );
}

Functions

Namesort descending Description
dynamic_background_panels_admin_form Build the administration interface for dynamic background panels and enables administrators enter css to used for insertion of backgrounds.
dynamic_background_panels_conf_submit_handler
dynamic_background_panels_dynamic_background_css Implements hook_dynamic_background_css().
dynamic_background_panels_dynamic_background_weight Implements hook_dynamic_background_weight for the main module.
dynamic_background_panels_form_alter Implements hook_form_alter().
dynamic_background_panels_menu Implementation of hook_menu(). Hooks into the dynamic background modules menu structure and adds the "nodes" menu tab to the administration interface.
dynamic_background_panels_permission Implements hook_permission().