ViewportResolver.php in Viewport 8
File
src/ViewportResolver.php
View source
<?php
namespace Drupal\viewport;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Path\PathMatcherInterface;
class ViewportResolver implements ViewportResolverInterface {
private $pathMatcher;
private $currentPathStack;
private $viewportSettings;
public function __construct(PathMatcherInterface $pathMatcher, CurrentPathStack $currentPathStack, ConfigFactoryInterface $configFactoryInterface) {
$this->pathMatcher = $pathMatcher;
$this->currentPathStack = $currentPathStack;
$this->viewportSettings = $configFactoryInterface
->get('viewport.settings');
}
public function isPathSelected($path = NULL) {
if (is_null($path)) {
$path = $this->currentPathStack
->getPath();
}
$path = strtolower($path);
$viewport_pages = strtolower($this->viewportSettings
->get('selected_pages'));
$page_match = $this->pathMatcher
->matchPath($path, $viewport_pages);
return $page_match;
}
public function generateViewportTagArray() {
$viewportSettings = $this->viewportSettings;
$values_string = '';
$values_string .= ($width = $viewportSettings
->get('width')) ? "width={$width}, " : '';
$values_string .= ($height = $viewportSettings
->get('height')) ? "height={$height}, " : '';
$values_string .= ($initial_scale = $viewportSettings
->get('initial_scale')) ? "initial-scale={$initial_scale}, " : '';
$values_string .= ($minimum_scale = $viewportSettings
->get('minimum_scale')) ? "minimum-scale={$minimum_scale}, " : '';
$values_string .= ($maximum_scale = $viewportSettings
->get('maximum_scale')) ? "maximum-scale={$maximum_scale}, " : '';
$values_string .= $viewportSettings
->get('user_scalable') == TRUE ? "user-scalable=yes" : 'user-scalable=no';
$viewport_tag = array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'viewport',
'content' => $values_string,
),
);
return $viewport_tag;
}
}