You are here

class ViewportResolver in Viewport 8

Provides a ViewportResolver.

Hierarchy

Expanded class hierarchy of ViewportResolver

1 file declares its use of ViewportResolver
ViewportResolverTest.php in tests/src/Unit/ViewportResolverTest.php
1 string reference to 'ViewportResolver'
viewport.services.yml in ./viewport.services.yml
viewport.services.yml
1 service uses ViewportResolver
viewport.resolver in ./viewport.services.yml
Drupal\viewport\ViewportResolver

File

src/ViewportResolver.php, line 12

Namespace

Drupal\viewport
View source
class ViewportResolver implements ViewportResolverInterface {

  /**
   * The patch matcher service.
   *
   * @var \Drupal\Core\Path\PathMatcherInterface
   */
  private $pathMatcher;

  /**
   * The current path stack service.
   *
   * @var \Drupal\Core\Path\CurrentPathStack
   */
  private $currentPathStack;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  private $viewportSettings;

  /**
   * Creates a new ViewportResolver.
   *
   * @param \Drupal\Core\Path\PathMatcherInterface $pathMatcher
   *   A path matcher service.
   * @param \Drupal\Core\Path\CurrentPathStack $currentPathStack
   *   The current path stack.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactoryInterface
   *   The configuration factory interface.
   */
  public function __construct(PathMatcherInterface $pathMatcher, CurrentPathStack $currentPathStack, ConfigFactoryInterface $configFactoryInterface) {
    $this->pathMatcher = $pathMatcher;
    $this->currentPathStack = $currentPathStack;
    $this->viewportSettings = $configFactoryInterface
      ->get('viewport.settings');
  }

  /**
   * {@inheritdoc}
   */
  public function isPathSelected($path = NULL) {
    if (is_null($path)) {
      $path = $this->currentPathStack
        ->getPath();
    }

    // Normalise the pages selected and the path looked for.
    $path = strtolower($path);
    $viewport_pages = strtolower($this->viewportSettings
      ->get('selected_pages'));

    // Check if current path is in the pages selected to have a custom viewport.
    $page_match = $this->pathMatcher
      ->matchPath($path, $viewport_pages);
    return $page_match;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ViewportResolver::$currentPathStack private property The current path stack service.
ViewportResolver::$pathMatcher private property The patch matcher service.
ViewportResolver::$viewportSettings private property The config factory service.
ViewportResolver::generateViewportTagArray public function Generates and returns an html_head tag array for use as page #attachment. Overrides ViewportResolverInterface::generateViewportTagArray
ViewportResolver::isPathSelected public function Checks if the given path (or current path) needs a custom viewport tag. Overrides ViewportResolverInterface::isPathSelected
ViewportResolver::__construct public function Creates a new ViewportResolver.