You are here

single_page_site.module in Single Page Site 7

File

single_page_site.module
View source
<?php

/**
 * Holds hooks and general functionality.
 */

/**
 * Implements hook_help().
 */
function single_page_site_help($path, $arg) {
  switch ($path) {
    case 'admin/help#single_page_site':
      $output = file_get_contents(drupal_get_path('module', 'single_page_site') . '/README.txt');
      return module_exists('markdown') ? module_invoke('markdown', 'filter', 'process', 0, -1, $output) : '<pre>' . $output . '</pre>';
  }
}

/**
 * Implements hook_permission().
 */
function single_page_site_permission() {
  return array(
    'administer single page site' => array(
      'title' => t('Administer single page site'),
      'description' => t('Configure Single Page Site settings.'),
    ),
    'view single page site' => array(
      'title' => t('View single page site'),
      'description' => t('Decide who can view Single Page Site.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function single_page_site_menu() {
  $items = array();
  $items['single-page-site'] = array(
    'title' => 'Single Page Website',
    'page callback' => 'single_page_site_callback',
    'access arguments' => array(
      'view single page site',
    ),
    'file' => 'includes/single_page_site.callback.inc',
  );
  $items['admin/config/system/single-page-site'] = array(
    'title' => 'Single Page Site Settings',
    'description' => 'Configure menu\'s and classes etc.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'single_page_site_settings_form',
    ),
    'access arguments' => array(
      'administer single page site',
    ),
    'file' => 'includes/single_page_site.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function single_page_site_theme() {
  return array(
    'single_page_site' => array(
      'template' => 'single-page-site',
      'variables' => array(
        'items' => array(),
        'wrapper' => 'single-page-overall-wrapper',
      ),
    ),
  );
}

/**
 * Implements hook_page_build().
 */
function single_page_site_page_build(&$page) {
  global $language;
  $settings = variable_get('single_page_site_settings', array());
  if (!empty($settings)) {
    $path = drupal_get_path('module', 'single_page_site');
    $page['page_bottom']['single_page_site'] = array(
      '#attached' => array(),
    );
    $attached =& $page['page_bottom']['single_page_site']['#attached'];
    $options = array(
      'every_page' => TRUE,
    );

    // Add JS to create anchors.
    $attached['js'][$path . '/js/single-page-site-menu.js'] = $options;
    $settings_js = array(
      'className' => !empty($settings['class']) ? $settings['class'] : 'li',
      'menuClass' => $settings['menuclass'],
      'isFrontpage' => $settings['homepage'],
    );
    if (!$settings['homepage']) {
      $settings_js['singlePage']['urlAlias'] = drupal_get_path_alias('single-page-site', $language->language);
    }
    $attached['js'][] = array(
      'data' => array(
        'singlePage' => $settings_js,
      ),
      'type' => 'setting',
    );
  }
}

/**
 * Implements hook_html_head_alter().
 */
function single_page_site_html_head_alter(&$elements) {
  if (current_path() == 'single-page-site') {

    // Remove all meta tags added by rendering sub pages.
    foreach ($elements as $key => $element) {
      if (isset($element['#tag']) && $element['#tag'] == 'link' && (!isset($element['#theme']) || strpos($element['#theme'], 'metatag') === FALSE)) {
        if (isset($element['#attributes']['rel']) && ($element['#attributes']['rel'] == 'canonical' || $element['#attributes']['rel'] == 'shortlink')) {
          unset($elements[$key]);
        }
      }
    }
  }
}

/**
 * Builds anchor.
 */
function _single_page_site_generate_anchor($link) {
  $temp_link = str_replace(array(
    '/',
    '?q=',
  ), array(
    '',
    '',
  ), $link);

  // Now replace all weirdo chars from anchor.
  return preg_replace('/[^A-Za-z0-9]/', '', $temp_link);
}

/**
 * Returns single page item title.
 * @param type $item
 * @return type
 */
function _single_page_site_get_title($item) {
  return isset($item['#localized_options']['attributes']['name']) ? check_plain($item['#localized_options']['attributes']['name']) : check_plain($item['#title']);
}

/**
 * Implements hook_form_FROM_ID_alter().
 */
function single_page_site_form_menu_edit_item_alter(&$form, &$form_state) {

  // Add validation callback to menu that is used as single page menu.
  $settings = variable_get('single_page_site_settings', array());
  if ($settings && $settings['menu'] == $form['original_item']['#value']['menu_name']) {
    $form['#validate'][] = '_single_page_site_validate_menu_item';
  }
}

/**
 * Validates the menu item.
 */
function _single_page_site_validate_menu_item($form, &$form_state) {
  if ($form_state['values']['link_path'] == 'single-page-site') {

    // Don't allow to add "single-page-site" as menu item.
    form_set_error('link_path', t("You can't add this page to this menu, because it is used as a single page menu."));
  }
  elseif ($form_state['values']['link_path'] == '<front>') {

    // Don't allow to add "<front>" as menu item if it is "single-page-site".
    form_set_error('link_path', t("You can't add the frontpage in this menu, because it is your 'single page' page"));
  }
}