You are here

content_theme.module in Content Theme 6

Same filename and directory in other branches
  1. 7.2 content_theme.module
  2. 7 content_theme.module

This module allows to use different themes than the site default on content creating, editing, and viewing pages.

File

content_theme.module
View source
<?php

/**
 * @file
 * This module allows to use different themes than the site default on content
 * creating, editing, and viewing pages.
 */

/**
 * Implementation of hook_help().
 */
function content_theme_help($path, $arg) {
  switch ($path) {
    case 'admin/help#content_theme':
      $output = '<p>' . t('The Content Theme module is a simple and easy module to use which allows to use different themes than the site default on content creating, editing, and viewing pages.') . '</p>';
      $output .= '<p>' . t('For more information, see the online project page for <a href="@content_theme">Content Theme module</a>.', array(
        '@content_theme' => 'http://drupal.org/project/content_theme',
      )) . '</p>';
      return $output;
    case 'admin/build/content-theme':
    case 'admin/build/content-theme/content-node':
      $output = '<p>' . t('<em>Content node themes</em> apply only to its own content and override the content type themes, content wide themes, and the system default theme.') . '</p>';
      return $output;
    case 'admin/build/content-theme/content-type':
      $output = '<p>' . t('<em>Content type themes</em> apply to all content based on its content type and override content wide themes and the system default theme. But these settings can be overridden by content node settings.') . '</p>';
      return $output;
    case 'admin/build/content-theme/content-wide':
      $output = '<p>' . t('<em>Content wide themes</em> apply to all content and override the system default theme. But these settings can be overridden by content type or content node settings.') . '</p>';
      return $output;
  }
}

/**
 * Implementation of hook_init().
 */
function content_theme_init() {
  if (arg(0) == 'node' && (is_numeric(arg(1)) || arg(1) == 'add' && !is_null(arg(2)))) {
    global $custom_theme;
    if (arg(1) == 'add') {
      $theme_node = FALSE;
      $theme_type = variable_get('content_theme_content_type_edit_' . str_replace('-', '_', arg(2)), '-content_wide-');
      $theme_wide = variable_get('content_theme_content_wide_edit', '0');
    }
    elseif (arg(2) == 'edit') {
      $theme_node = db_result(db_query('SELECT theme FROM {content_theme_node} WHERE nid = %d AND action = "edit"', arg(1)));
      $theme_type = variable_get('content_theme_content_type_edit_' . db_result(db_query('SELECT type FROM {node} WHERE nid = %d', arg(1))), '-content_wide-');
      $theme_wide = variable_get('content_theme_content_wide_edit', '0');
    }
    else {
      $theme_node = db_result(db_query('SELECT theme FROM {content_theme_node} WHERE nid = %d AND action = "view"', arg(1)));
      $theme_type = variable_get('content_theme_content_type_view_' . db_result(db_query('SELECT type FROM {node} WHERE nid = %d', arg(1))), '-content_wide-');
      $theme_wide = variable_get('content_theme_content_wide_view', '0');
    }
    if ($theme_node !== FALSE && $theme_node != '-content_wide-') {
      $custom_theme = $theme_node;
    }
    else {
      if ($theme_type != '-content_wide-' && !$theme_node) {
        $custom_theme = $theme_type;
      }
      else {
        $custom_theme = $theme_wide;
      }
    }
  }
}

/**
 * Implementation of hook_perm().
 */
function content_theme_perm() {
  $perm = array();
  foreach (node_get_types('names') as $type => $name) {
    $perm[] = "select {$type} content editing theme";
    $perm[] = "select {$type} content viewing theme";
  }
  $perm[] = 'administer content theme';
  return $perm;
}

/**
 * Implementation of hook_menu().
 */
function content_theme_menu() {
  $menu = array();
  $menu['admin/build/content-theme'] = array(
    'title' => 'Content theme',
    'description' => 'Configure which theme is used when content is created, edited or be viewed.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'content_theme_admin_content_node',
    ),
    'access arguments' => array(
      'administer content theme',
    ),
    'file' => 'content_theme.admin.inc',
  );
  $menu['admin/build/content-theme/content-node'] = array(
    'title' => 'Content node',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $menu['admin/build/content-theme/content-type'] = array(
    'title' => 'Content type',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'content_theme_admin_content_type',
    ),
    'access arguments' => array(
      'administer content theme',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 0,
    'file' => 'content_theme.admin.inc',
  );
  $menu['admin/build/content-theme/content-wide'] = array(
    'title' => 'Content wide',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'content_theme_admin_content_wide',
    ),
    'access arguments' => array(
      'administer content theme',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
    'file' => 'content_theme.admin.inc',
  );
  return $menu;
}

/**
 * Implementation of hook_theme().
 */
function content_theme_theme() {
  $theme = array();
  $theme['content_theme_admin_content_node'] = array(
    'arguments' => array(
      'form' => NULL,
    ),
    'file' => 'content_theme.admin.inc',
  );
  $theme['content_theme_admin_content_type'] = array(
    'arguments' => array(
      'form' => NULL,
    ),
    'file' => 'content_theme.admin.inc',
  );
  return $theme;
}

/**
 * Implementation of hook_form_FORM_ID_alter().
 */
function content_theme_form_node_type_form_alter(&$form, &$form_state) {
  $edit_theme = variable_get('content_theme_content_type_edit_' . $form['#node_type']->type, '-content_wide-');
  $view_theme = variable_get('content_theme_content_type_view_' . $form['#node_type']->type, '-content_wide-');

  // Add content type theme options to content type form.
  $form['content_theme'] = array(
    '#type' => 'fieldset',
    '#title' => t('Content theme settings'),
    '#description' => t('Applies to all content based on this content type and overrides content wide themes and the system default. But these settings can be overridden by content node settings.'),
    '#collapsible' => TRUE,
    '#collapsed' => $edit_theme == '-content_wide-' && $view_theme == '-content_wide-',
  );
  $form['content_theme']['content_theme_content_type_edit'] = array(
    '#type' => 'select',
    '#title' => t('Creating & editing theme'),
    '#description' => t('Choose which theme the content creating and editing pages should display in. Content wide theme: %content_wide_theme; system default theme: %system_default_theme.', array(
      '%content_wide_theme' => content_theme_get_info_theme_name('content_wide', 'edit'),
      '%system_default_theme' => content_theme_get_info_theme_name(),
    )),
    '#default_value' => $edit_theme,
    '#options' => content_theme_get_content_type_options(),
  );
  $form['content_theme']['content_theme_content_type_view'] = array(
    '#type' => 'select',
    '#title' => t('Viewing theme'),
    '#description' => t('Choose which theme the content viewing pages should display in. Content wide theme: %content_wide_theme; system default theme: %system_default_theme.', array(
      '%content_wide_theme' => content_theme_get_info_theme_name('content_wide', 'view'),
      '%system_default_theme' => content_theme_get_info_theme_name(),
    )),
    '#default_value' => $view_theme,
    '#options' => content_theme_get_content_type_options(),
  );
}

/**
 * Implementation of hook_node_type().
 */
function content_theme_node_type($op, $info) {
  switch ($op) {
    case 'delete':
      variable_del('content_theme_content_type_edit_' . $info->type);
      variable_del('content_theme_content_type_view_' . $info->type);
      break;
  }
}

/**
 * Implementation of hook_form_alter().
 */
function content_theme_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) {
    $edit_theme = isset($form['#node']->content_theme_content_node_edit) ? $form['#node']->content_theme_content_node_edit : '-content_type-';
    $view_theme = isset($form['#node']->content_theme_content_node_view) ? $form['#node']->content_theme_content_node_view : '-content_type-';
    $edit_access = user_access('select ' . $form['type']['#value'] . ' content editing theme');
    $view_access = user_access('select ' . $form['type']['#value'] . ' content viewing theme');

    // Add content node theme options to content node form.
    $form['content_theme'] = array(
      '#type' => 'fieldset',
      '#title' => t('Content theme settings'),
      '#description' => t('Applies only to this content node and overrides the content type themes, content wide themes, and the system default.'),
      '#collapsible' => TRUE,
      '#collapsed' => $edit_theme == '-content_type-' && $view_theme == '-content_type-',
      '#access' => $edit_access || $view_access,
      '#weight' => 20,
    );
    $form['content_theme']['content_theme_content_node_edit'] = array(
      '#type' => 'select',
      '#title' => t('Editing theme'),
      '#description' => t('Choose which theme the content creating and editing pages should display in. Content type theme: %content_type_theme; content wide theme: %content_wide_theme; system default theme: %system_default_theme.', array(
        '%content_type_theme' => content_theme_get_info_theme_name('content_type', 'edit', $form['type']['#value']),
        '%content_wide_theme' => content_theme_get_info_theme_name('content_wide', 'edit'),
        '%system_default_theme' => content_theme_get_info_theme_name(),
      )),
      '#default_value' => $edit_theme,
      '#options' => content_theme_get_content_node_options(),
      '#access' => $edit_access,
    );
    $form['content_theme']['content_theme_content_node_view'] = array(
      '#type' => 'select',
      '#title' => t('Viewing theme'),
      '#description' => t('Choose which theme the content viewing pages should display in. Content type theme: %content_type_theme; content wide theme: %content_wide_theme; system default theme: %system_default_theme.', array(
        '%content_type_theme' => content_theme_get_info_theme_name('content_type', 'view', $form['type']['#value']),
        '%content_wide_theme' => content_theme_get_info_theme_name('content_wide', 'view'),
        '%system_default_theme' => content_theme_get_info_theme_name(),
      )),
      '#default_value' => $view_theme,
      '#options' => content_theme_get_content_node_options(),
      '#access' => $view_access,
    );
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function content_theme_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'load':
      $result = db_query('SELECT theme FROM {content_theme_node} WHERE nid = %d AND action = "edit"', $node->nid);
      $node->content_theme_content_node_edit = ($theme = db_fetch_object($result)) ? $theme->theme : '-content_type-';
      $result = db_query('SELECT theme FROM {content_theme_node} WHERE nid = %d AND action = "view"', $node->nid);
      $node->content_theme_content_node_view = ($theme = db_fetch_object($result)) ? $theme->theme : '-content_type-';
      break;
    case 'update':
      db_query('DELETE FROM {content_theme_node} WHERE nid = %d', $node->nid);
    case 'insert':
      $theme = isset($node->content_theme_content_node_edit) ? $node->content_theme_content_node_edit : '-content_type-';
      if ($theme != '-content_type-') {
        db_query('INSERT INTO {content_theme_node} (nid, action, theme) VALUES (%d, "edit", "%s")', $node->nid, $theme);
      }
      $theme = isset($node->content_theme_content_node_view) ? $node->content_theme_content_node_view : '-content_type-';
      if ($theme != '-content_type-') {
        db_query('INSERT INTO {content_theme_node} (nid, action, theme) VALUES (%d, "view", "%s")', $node->nid, $theme);
      }
      break;
    case 'delete':
      db_query('DELETE FROM {content_theme_node} WHERE nid = %d', $node->nid);
      break;
  }
}

/**
 * Helper functions.
 */
function content_theme_get_content_wide_options() {
  static $options = array();
  if (!$options) {
    $options['0'] = '- ' . t('System default theme') . ' -';
    $options += content_theme_get_themes();
  }
  return $options;
}
function content_theme_get_content_type_options() {
  static $options = array();
  if (!$options) {
    $options['0'] = '- ' . t('System default theme') . ' -';
    $options['-content_wide-'] = '- ' . t('Content wide theme') . ' -';
    $options += content_theme_get_themes();
  }
  return $options;
}
function content_theme_get_content_node_options() {
  static $options = array();
  if (!$options) {
    $options['0'] = '- ' . t('System default theme') . ' -';
    $options['-content_wide-'] = '- ' . t('Content wide theme') . ' -';
    $options['-content_type-'] = '- ' . t('Content type theme') . ' -';
    $options += content_theme_get_themes();
  }
  return $options;
}
function content_theme_get_themes($theme = NULL) {
  static $themes = array();
  if (!$themes) {
    foreach (system_theme_data() as $theme_data) {
      $compatible_core = isset($theme_data->info['core']) && $theme_data->info['core'] == DRUPAL_CORE_COMPATIBILITY;
      $compatible_php = version_compare(phpversion(), $theme_data->info['php'], '>=');
      if ($compatible_core && $compatible_php) {
        $themes[$theme_data->name] = $theme_data->info['name'];
      }
    }
    asort($themes);
  }
  return $theme ? $themes[$theme] : $themes;
}
function content_theme_get_theme_name($theme) {
  static $themes = array();
  if (!$themes) {
    $themes['0'] = t('%system_default_theme', array(
      '%system_default_theme' => 'System default theme',
    ));
    $themes['-content_wide-'] = t('%content_wide_theme', array(
      '%content_wide_theme' => 'Content wide theme',
    ));
    $themes['-content_type-'] = t('%content_type_theme', array(
      '%content_type_theme' => 'Content type theme',
    ));
    $themes += content_theme_get_themes();
  }
  return isset($themes[$theme]) ? $themes[$theme] : t('%none', array(
    '%none' => 'none',
  ));
}
function content_theme_get_info_theme_name($mode = 'system_default', $action = NULL, $type = NULL) {
  $name = $system_default_theme = content_theme_get_theme_name(variable_get('theme_default', 'garland'));
  if ($mode == 'content_wide' || $mode == 'content_type') {
    if ($action == 'edit' || $action == 'view') {
      $theme = variable_get('content_theme_content_wide_' . $action, '0');
      $name = $theme != '0' ? content_theme_get_theme_name($theme) : $system_default_theme;
      if ($mode == 'content_type') {
        $theme = variable_get('content_theme_content_type_' . $action . '_' . $type, '-content_wide-');
        if ($theme != '-content_wide-') {
          $name = $theme != '0' ? content_theme_get_theme_name($theme) : $system_default_theme;
        }
      }
    }
    else {
      $name = t('%none', array(
        '%none' => 'none',
      ));
    }
  }
  return $name;
}