Skip to content
Snippets Groups Projects
Commit 3cfff373 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2301975 by kim.pepper: Move drupal_is_front_page to PathMatcher service.

parent 7fe5f2ac
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -15,20 +15,12 @@
*
* @return
* Boolean value: TRUE if the current page is the front page; FALSE if otherwise.
*
* @deprecated as of Drupal 8.0. Use
* \Drupal\Core\Path\PathMatcherInterface::isFrontPage() instead.
*/
function drupal_is_front_page() {
// Use the advanced drupal_static() pattern, since this is called very often.
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['is_front_page'] = &drupal_static(__FUNCTION__);
}
$is_front_page = &$drupal_static_fast['is_front_page'];
if (!isset($is_front_page)) {
$is_front_page = (current_path() == \Drupal::config('system.site')->get('page.front'));
}
return $is_front_page;
return \Drupal::service('path.matcher')->isFrontPage();
}
/**
......
......@@ -14,6 +14,13 @@
*/
class PathMatcher implements PathMatcherInterface {
/**
* Whether the current page is the front page.
*
* @var bool
*/
protected $isCurrentFrontPage;
/**
* The default front page.
*
......@@ -51,10 +58,6 @@ public function __construct(ConfigFactoryInterface $config_factory) {
public function matchPath($path, $patterns) {
if (!isset($this->regexes[$patterns])) {
// Lazy-load front page config.
if (!isset($this->frontPage)) {
$this->frontPage = $this->configFactory->get('system.site')->get('page.front');
}
// Convert path settings to a regular expression.
$to_replace = array(
// Replace newlines with a logical 'or'.
......@@ -67,11 +70,37 @@ public function matchPath($path, $patterns) {
$replacements = array(
'|',
'.*',
'\1' . preg_quote($this->frontPage, '/') . '\2',
'\1' . preg_quote($this->getFrontPagePath(), '/') . '\2',
);
$patterns_quoted = preg_quote($patterns, '/');
$this->regexes[$patterns] = '/^(' . preg_replace($to_replace, $replacements, $patterns_quoted) . ')$/';
}
return (bool) preg_match($this->regexes[$patterns], $path);
}
/**
* {@inheritdoc}
*/
public function isFrontPage() {
// Cache the result as this is called often.
if (!isset($this->isCurrentFrontPage)) {
$this->isCurrentFrontPage = (current_path() == $this->getFrontPagePath());
}
return $this->isCurrentFrontPage;
}
/**
* Gets the current front page path.
*
* @return string
* The front page path.
*/
protected function getFrontPagePath() {
// Lazy-load front page config.
if (!isset($this->frontPage)) {
$this->frontPage = $this->configFactory->get('system.site')
->get('page.front');
}
return $this->frontPage;
}
}
......@@ -25,4 +25,12 @@ interface PathMatcherInterface {
*/
public function matchPath($path, $patterns);
/**
* Checks if the current page is the front page.
*
* @return bool
* TRUE if the current page is the front page.
*/
public function isFrontPage();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment