class PrintableCssInclude in Printer and PDF versions for Drupal 8+ 8
Same name and namespace in other branches
- 2.x src/PrintableCssInclude.php \Drupal\printable\PrintableCssInclude
Helper class for the printable module.
Hierarchy
- class \Drupal\printable\PrintableCssInclude implements PrintableCssIncludeInterface
Expanded class hierarchy of PrintableCssInclude
1 file declares its use of PrintableCssInclude
- PrintableCssIncludeTest.php in tests/
src/ Unit/ PrintableCssIncludeTest.php
1 string reference to 'PrintableCssInclude'
1 service uses PrintableCssInclude
File
- src/
PrintableCssInclude.php, line 11
Namespace
Drupal\printableView source
class PrintableCssInclude implements PrintableCssIncludeInterface {
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* The theme handler service.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;
/**
* Constructs a new PrintableCssInclude object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory service.
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* The theme handler service.
*/
public function __construct(ConfigFactoryInterface $config_factory, ThemeHandlerInterface $theme_handler) {
$this->configFactory = $config_factory;
$this->themeHandler = $theme_handler;
}
/**
* {@inheritdoc}
*/
public function getCssIncludePath() {
if ($include_path = $this->configFactory
->get('printable.settings')
->get('css_include')) {
if ($token = $this
->extractCssIncludeToken($include_path)) {
list(, $theme) = explode(':', trim($token, '[]'));
$include_path = str_replace($token, $this
->getThemePath($theme), $include_path);
}
return $include_path;
}
}
/**
* Extract the theme token from a CSS include path.
*
* @param string $path
* An include path (optionally) with a taken to extract in the form:
* "[theme:theme_machine_name]".
*
* @return string|null
* The extracted token in the form "[theme:theme_machine_name]" or NULL if
* no token exists in the string.
*/
protected function extractCssIncludeToken($path) {
$start = '[theme:';
$end = ']';
// Fail fast.
if (strpos($path, $start) === FALSE) {
return NULL;
}
$index = strpos($path, $start);
// Here strpos is zero indexed.
$length = strpos($path, $end, $index) + 1;
return substr($path, $index, $length);
}
/**
* Get the path to a theme.
*
* @param string $theme
* The machine name of the theme to get the path for.
*
* @return string
* The path to the given theme.
*
* @todo replace this with an injectable version of drupal_get_path() when/if
* it lands.
*/
protected function getThemePath($theme) {
$info = $this->themeHandler
->listInfo();
$path = '';
if (isset($info[$theme])) {
$path = $info[$theme]
->getPath();
}
return $path;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PrintableCssInclude:: |
protected | property | The config factory service. | |
PrintableCssInclude:: |
protected | property | The theme handler service. | |
PrintableCssInclude:: |
protected | function | Extract the theme token from a CSS include path. | |
PrintableCssInclude:: |
public | function |
Get the configured CSS include path for printable pages. Overrides PrintableCssIncludeInterface:: |
|
PrintableCssInclude:: |
protected | function | Get the path to a theme. | |
PrintableCssInclude:: |
public | function | Constructs a new PrintableCssInclude object. |