viewport.module in Viewport 8
Same filename and directory in other branches
Viewport module.
Allows to add custom viewport settings for selected pages.
File
viewport.moduleView source
<?php
/**
* @file
* Viewport module.
*
* Allows to add custom viewport settings for selected pages.
*/
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
/**
* Implements hook_help().
*/
function viewport_help($route_name, RouteMatchInterface $route_match) {
$help_text = '';
switch ($route_name) {
// Help page.
case 'help.page.viewport':
$link_options = array(
'attributes' => array(
'target' => '_blank',
),
);
$module_settings_page = Link::createFromRoute(t("Viewport configuration page"), 'viewport.settings', array(), $link_options)
->toString();
$link_options['fragment'] = 'module-viewport';
$viewport_permissions = Link::createFromRoute(t('Administer Viewport settings'), 'user.admin_permissions', array(), $link_options)
->toString();
$help_text = '<h3>' . t('About') . '</h3>';
$help_text .= '<p>' . t('The Viewport module is a simple module that allows
administrators, or users with the <em>"!viewport_permissions"</em> permission,
to set a Viewport HTML metatag with the desired properties for one or several
pages that can be configured from the \'!settings_page\'.', array(
'!settings_page' => $module_settings_page,
'!viewport_permissions' => $viewport_permissions,
)) . '</p>';
$help_text .= '<h3>' . t('Uses') . '</h3>';
$help_text .= '<p>' . t('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.') . '</p>';
$help_text .= '<p>' . t('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 solve easily situations like that.') . '</p>';
$help_text .= '<strong>' . t('For more information on
the viewport metatag and its properties, refer to the following resources:') . '</strong>';
$resource_links = array(
Link::fromTextAndUrl(t('Safari developer docs'), Url::fromUri('https://developer.apple.com/library/safari/#documentation/appleapplications/reference/SafariHTMLRef/Articles/MetaTags.html#//apple_ref/doc/uid/TP40008193-SW7', array(
'attributes' => array(
'target' => '_blank',
),
))),
Link::fromTextAndUrl(t('Non-browser-specific information on viewport metatag'), Url::fromUri('http://www.html-5.com/metatags/index.html#viewport-meta-tag', array(
'attributes' => array(
'target' => '_blank',
),
))),
Link::fromTextAndUrl(t('Detailed browser comparison of the viewport properties'), Url::fromUri('http://www.quirksmode.org/mobile/tableViewport.html', array(
'attributes' => array(
'target' => '_blank',
),
))),
);
$item_list = array(
'#theme' => 'item_list',
'#items' => $resource_links,
);
$help_text .= \Drupal::service('renderer')
->render($item_list);
break;
// Settings page.
case 'viewport.settings':
$help_page_url = Link::createFromRoute(t("Viewport's help page"), 'help.page', [
'name' => 'viewport',
], array(
'attributes' => array(
'target' => '_blank',
),
))
->toString();
$help_text = t('The values for the viewport metatag will be output in the same way as entered here
for the pages selected below. <br/>
For numeric values with decimal digits, use the decimal point "." instead of a comma ",". <br/>
For detailed information about what these values are for, visit the \'!help_page\'.', array(
'!help_page' => $help_page_url,
));
break;
}
return $help_text;
}
/**
* Implements hook_page_attachments_alter().
*
* @see system_page_attachments()
*/
function viewport_page_attachments_alter(array &$attachments) {
// Use the viewport resolver service to see if a custom viewport is needed,
// and to generate it.
$viewport_resolver = Drupal::service('viewport.resolver');
if ($viewport_resolver
->isPathSelected()) {
$viewport_tag = $viewport_resolver
->generateViewportTagArray();
// Replace core's viewport values with our custom values.
// $head_array structure is "[$value, $key]" (e.g: [array(), 'viewport']).
foreach ($attachments['#attached']['html_head'] as $index => $head_array) {
if ($head_array[1] == 'viewport') {
$attachments['#attached']['html_head'][$index][0] = $viewport_tag;
$viewport_added = TRUE;
}
}
// Ensure tag is added if it's not already added by core.
if (empty($viewport_added)) {
$attachments['#attached']['html_head'][] = [
$viewport_tag,
'viewport',
];
}
}
}
Functions
Name | Description |
---|---|
viewport_help | Implements hook_help(). |
viewport_page_attachments_alter | Implements hook_page_attachments_alter(). |