You are here

viewport.module in Viewport 7

Same filename and directory in other branches
  1. 8 viewport.module

viewport module Allows to set a viewport metatag with custom settings for a selected set of pages.

File

viewport.module
View source
<?php

/**
 * @file
 * viewport module
 * Allows to set a viewport metatag with custom settings for a selected set of
 * pages.
 */

/**
 * Implements hook_help().
 */
function viewport_help($path, $arg) {
  $help_text = '';
  switch ($path) {
    case 'admin/help#viewport':
      $help_text = t('<p>The Viewport module is a simple module that allows
        administrators, or users with the "Administer Viewport Settings" permission,
        to set a Viewport HTML metatag with the desired properties for one or several
        pages that can be configured from the Settings page of the module.</p>
        <p>
        The aim of the module is to provide an easy way to debug or test websites or
        apps, as well as responsive designs on smartphones and tablets. This is *NOT* a
        complete suite of tools to work with when delivering this kind of design / apps,
        but a small utility to help with one specific aspect of responsive design.</p>
        <p>
        Sometimes, one may need to set specific viewport values for a specific page on
        the site (e.g when embedding a game for smartphones / tablets). This tool
        helps to chase easily situations like that.</p>
        <p><strong>For more information on the viewport metatag and its
        properties, check the following resources: </strong></p>');
      $resource_links = array(
        'items' => array(
          l(t('Safari developer docs'), 'https://developer.apple.com/library/safari/#documentation/appleapplications/reference/SafariHTMLRef/Articles/MetaTags.html#//apple_ref/doc/uid/TP40008193-SW7', array(
            'attributes' => array(
              'target' => '_blank',
            ),
          )),
          l(t('Non-browser-specific information on viewport metatag'), 'http://www.html-5.com/metatags/index.html#viewport-meta-tag', array(
            'attributes' => array(
              'target' => '_blank',
            ),
          )),
          l(t('Detailed browser comparison of the viewport properties'), 'http://www.quirksmode.org/mobile/tableViewport.html', array(
            'attributes' => array(
              'target' => '_blank',
            ),
          )),
        ),
      );
      $help_text .= theme('item_list', $resource_links);
      break;
  }
  return $help_text;
}

/**
 * Implements hook_permission().
 */
function viewport_permission() {
  return array(
    'administer viewport' => array(
      'title' => t('Administer Viewport Settings'),
      'description' => t('Configure viewport settings for a selected set of pages'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function viewport_menu() {
  $items = array();
  $items['admin/config/user-interface/viewport'] = array(
    'title' => 'Viewport Settings',
    'description' => 'Configure viewport settings for a selected set of pages.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'viewport_settings_form',
    ),
    'access arguments' => array(
      'administer viewport',
    ),
    'file' => 'viewport.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_preprocess_html().
 */
function viewport_preprocess_html(&$vars) {

  // Check if the viewport tag needs to be present.
  if (viewport_path_is_selected()) {
    $values_string = '';
    $values_string .= ($width = variable_get('viewport_width', FALSE)) ? "width={$width}, " : '';
    $values_string .= ($height = variable_get('viewport_height', FALSE)) ? "height={$height}, " : '';
    $values_string .= ($initial_scale = variable_get('viewport_initial_scale', FALSE)) ? "initial-scale={$initial_scale}, " : '';
    $values_string .= ($minimum_scale = variable_get('viewport_minimum_scale', FALSE)) ? "minimum-scale={$minimum_scale}, " : '';
    $values_string .= ($maximum_scale = variable_get('viewport_maximum_scale', FALSE)) ? "maximum-scale={$maximum_scale}, " : '';
    $values_string .= variable_get('viewport_user_scalable', TRUE) ? "user-scalable=yes" : 'user-scalable=no';
    $html_tag = array(
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => array(
        'name' => 'viewport',
        'content' => $values_string,
      ),
    );
    drupal_add_html_head($html_tag, 'viewport');
  }
}

/**
 * Checks whether a given path (or the current path if none is provided) has
 * been configured to display a viewport tag.
 *
 * @param string $path
 *   The path or alias of a page to be checked against the selected pages for
 *   the viewport.
 *
 * @return bool
 *   TRUE if the patch is present in the selected values, FALSE otherwise.
 */
function viewport_path_is_selected($path = NULL) {

  // Use the variable of $_GET['q'] as the default $path.
  if (is_null($path)) {
    $path = current_path();
  }
  $viewport_pages = variable_get('viewport_selected_pages', '');

  // Normalise the pages selected and the path looked for.
  $path_alias = drupal_strtolower(drupal_get_path_alias($path));
  $path = strtolower($path);
  $viewport_pages = strtolower($viewport_pages);

  // Page will match if current patch matches, or its alias (if any) matches.
  $page_match = drupal_match_path($path, $viewport_pages);
  if ($path_alias != $path && !$page_match) {
    $page_match = drupal_match_path($path_alias, $viewport_pages);
  }
  return $page_match;
}

Functions

Namesort descending Description
viewport_help Implements hook_help().
viewport_menu Implements hook_menu().
viewport_path_is_selected Checks whether a given path (or the current path if none is provided) has been configured to display a viewport tag.
viewport_permission Implements hook_permission().
viewport_preprocess_html Implements hook_preprocess_html().