You are here

custompage.module in Custom Page 7

Same filename and directory in other branches
  1. 6 custompage.module

Custom Page Module

File

custompage.module
View source
<?php

/**
 * @file
 * Custom Page Module
 **/
function custompage_init() {
  $mpath = dirname(__FILE__);
  require_once $mpath . '/custompage_util.inc';
  if (module_exists('context')) {

    //Context module integration
    include_once $mpath . '/custompage.context.inc';
  }

  /*
  Load CSS. Since util functions are useful
  and can be called from non-custompage pages
  as well (e.g. a block), we need to always load
  css, not - just when we are on a custom page.
  */
  $mod_uri = drupal_get_path('module', 'custompage');
  drupal_add_css($mod_uri . '/custompage.css');

  //For Development Only. Do NOT un-comment in production

  //drupal_flush_all_caches();
}

/**
 * Implementation of hook_permission().
 */
function custompage_permission() {
  return array(
    'administer custompage' => array(
      'title' => t('Administer Custom Pages'),
      'description' => t('Allow user to add and remove custom pages'),
    ),
    'edit custompage tiles' => array(
      'title' => t('Edit Custom Page Tiles'),
      'description' => t('Allow user to edit existing tiles'),
    ),
  );
}

/**
 * Implementation of hook_menu().
 *
 * Register menu callbacks for all custom page URLs.
 */
function custompage_menu() {
  $items = array();
  $items['admin/config/system/custompage/flushcache'] = array(
    'title' => 'Flush Cache',
    'page callback' => 'custompage_flushcache',
    'access arguments' => array(
      'administer custompage',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/system/custompage-settings'] = array(
    'title' => 'Custom Page Settings',
    'description' => 'Manage theme prefix and inline editing',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'custompage_settings',
    ),
    'access arguments' => array(
      'administer custompage',
    ),
    'file' => 'custompage.admin.inc',
  );
  $styled_paths = _custompage_get_mappings();
  foreach ($styled_paths as $path) {

    // only create callbacks for page-type components
    if ($path->type == 'block') {
      continue;
    }
    if ($path->enabled) {
      $access = array(
        'access content',
      );
    }
    else {
      $access = array(
        'administer custompage',
      );
    }
    $items[$path->path] = array(
      'title' => t($path->title),
      'page callback' => 'custompage_delegate',
      'page arguments' => array(
        $path->key,
      ),
      'access arguments' => $access,
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * Generate custom blocks
 * @param delta offset
 * @returns block HTML
 */
function custompage_block_info() {
  $styled_paths = _custompage_get_mappings();
  if (!is_array($styled_paths)) {
    $styled_paths = array();
  }
  $blocks = array();
  foreach ($styled_paths as $path) {

    // only create callbacks for page-type components
    if ($path->type != 'block') {
      continue;
    }
    $blocks[$path->key] = array(
      'info' => $path->title,
    );
  }
  return $blocks;
}
function custompage_block_view($delta = '') {
  $styled_paths = _custompage_get_mappings();
  if (!is_array($styled_paths)) {
    $styled_paths = array();
  }
  $block = array();
  foreach ($styled_paths as $path) {
    switch ($delta) {
      case $path->key:
        $block = array(
          'subject' => $path->title,
          'content' => theme($variables = array(
            custompage_prefix($path->key),
            FALSE,
          )),
        );
        break;
    }
  }
  return $block;
}

/**
 * Flush caches.
 */
function custompage_flushcache() {
  drupal_flush_all_caches();
  drupal_set_message('Cache cleared.');
  drupal_goto($_SERVER['HTTP_REFERER']);
}

/**
 * Implementation of hook_theme().
 *
 * Register theme tpl/functions for each custompage URL.
 */
function custompage_theme() {
  $styled_paths = _custompage_get_mappings();
  $themes = array();
  foreach ($styled_paths as $path) {
    $key = custompage_prefix($path->key);
    $themes[$key] = array(
      'variables' => array(
        'title' => '',
        'user' => '',
        'data' => '',
      ),
      'template' => $key,
    );
  }
  return $themes;
}

/**
 * @return
 *    The custompage key configured for theme prefix if necessary
 */
function custompage_prefix($key) {
  $prefix = variable_get('custompage_theme_prefix', '');
  if (!empty($prefix)) {
    return "{$prefix}_{$key}";
  }
  return $key;
}

/**
 * Find a theme function and/or template file and return content
 */
function custompage_delegate($arg) {
  global $user;

  //Context integration
  if (module_exists('context')) {

    // context 2
    if (function_exists('context_set_by_condition')) {
      context_set_by_condition('custompage', $arg);
    }
    else {
      if ($plugin = context_get_plugin('condition', 'custompage')) {
        $plugin
          ->execute($arg);
      }
    }
  }

  // Let's prepare some data for this baby.
  $data = module_invoke_all('customdata_' . $arg);
  $key = custompage_prefix($arg);
  $themed = theme($variables = array(
    $key,
    drupal_get_title(),
    $user,
    $data,
  ));
  if (trim($themed) == "") {
    drupal_set_message("custompage could not find an appropriate theming function or template file for this path [{$arg}].\n    <br /><b>The viable options (in ascending priority) are:</b>\n    <ul>\n      <li>'phptemplate_{$arg}' in any module\n      <li>'THEMENAME_{$arg}' in a theme with the name THEMENAME\n      <li> " . custompage_prefix($arg) . ".tpl.php template file in a current theme\n    </ul>\n    Please make sure at least one of these exist and returns a non-empty output" . custompage_clearcache_message(), 'error');
    return "&nbsp;";

    //Returning empty leads to undesired effect of getting a blank page
  }
  else {
    return $themed;
  }
}
function custompage_clearcache_message() {
  return "<div style=\"border-top: 1px solid silver;\"><b>ATTENTION:</b> Theme engine is strongly cached in Drupal version 7 and\n  if/when you add a new tpl.php or a theming function, you need to " . l(t('clear cache'), 'admin/config/system/custompage/flushcache') . " before you can see any changes.</div>";
}
function custompage_uri_load($id) {
  return TRUE;
}
function _custompage_get_mappings() {
  static $mappings;
  if (is_array($mappings)) {

    //performance optimization
    return $mappings;
  }
  $mappings = module_invoke_all('custompages');

  /*** Structure:
     $obj = new stdClass();

     $obj->uri = 'homepage';
     $obj->title = 'Home Page';
     $obj->key = 'homepage';

     $mappings[] = $obj;
     **/
  return $mappings;
}

/**
 * Make module compatible with context 3
 * implement hook_ctools_plugin_api().
 */
function custompage_ctools_plugin_api($module, $api) {

  // site using context 2, but using ctools
  if (function_exists('context_set_by_condition')) {
    return;
  }
  if ($module == 'context' && $api == 'plugins') {
    return array(
      'version' => 3,
    );
  }
}

/**
 * Make module compatible with context 3
 * Implement hook_context_plugins().
 */
function custompage_context_plugins() {
  $plugins = array();
  $plugins['custompage_context_condition_alias'] = array(
    'handler' => array(
      'path' => drupal_get_path('module', 'custompage') . '/plugins',
      'file' => 'custompage_context_condition_alias.inc',
      'class' => 'custompage_context_condition_alias',
      'parent' => 'context_condition',
    ),
  );
  return $plugins;
}

/**
 * Make module compatible with context 3
 * Implement hook_context_registry().
 */
function custompage_context_registry() {
  $registry['conditions'] = array(
    'custompage' => array(
      'title' => t('Custompage'),
      'description' => t('Set condition on visiting a custom page URL.'),
      'plugin' => 'custompage_context_condition_alias',
    ),
  );
  return $registry;
}

Functions

Namesort descending Description
custompage_block_info Generate custom blocks
custompage_block_view
custompage_clearcache_message
custompage_context_plugins Make module compatible with context 3 Implement hook_context_plugins().
custompage_context_registry Make module compatible with context 3 Implement hook_context_registry().
custompage_ctools_plugin_api Make module compatible with context 3 implement hook_ctools_plugin_api().
custompage_delegate Find a theme function and/or template file and return content
custompage_flushcache Flush caches.
custompage_init @file Custom Page Module
custompage_menu Implementation of hook_menu().
custompage_permission Implementation of hook_permission().
custompage_prefix
custompage_theme Implementation of hook_theme().
custompage_uri_load
_custompage_get_mappings