开发者

drupal path alias exist or not

How to find whether url is exist or not ? i.e url that leads to 'page not found' error . for ex: finding test/testpage is exist or not

i mean that to check whether given relative path or full path is really exist on the site or it leads to page not found开发者_如何学JAVA error


You can use the menu_valid_path() function for this. This returns TRUE or FALSE based on 1. Whether the menu path exists and 2. if the current user has permission to view the item.

You call it like this:

$item_exists = menu_valid_path(array('link_path' => $some_path));

Where $some_path is the path you want to test.


If you want to know does only an alias exist or not, use:

$path_exist = drupal_lookup_path('alias',$path);

But if you want to know does one of system path or alias is exist, use:

$path_exist = drupal_lookup_path('alias',$path) || drupal_lookup_path('source',$path);


Since Drupal 8.8, Path Alias is a content entity. So we can use

$pathStorage = \Drupal::entityTypeManager()->getStorage('path_alias');
$aliases = $pathStorage->loadByProperties(['alias' => $alias]);
// or 
// $pathStorage->loadByProperties(['path' => $path]);
$existingAlias = count($aliases) > 0;


Refer to this URL for Drupal 8.

You can use:

\Drupal::service('path.alias_storage')->aliasExists('current_path', 'en')

And it will load "1" if alias exists in system.


Also you can use requesting page: http://api.drupal.org/api/function/drupal_http_request/6
On error you will get not empty $result->error.


For Drupal 9 \Drupal::service('path.alias_storage')->aliasExists() is not longer available.

But you can use:

    $path_alias_repository = \Drupal::service('path_alias.repository');
    if ($path_alias_repository->lookupByAlias($alias, 'en')) {
        return TRUE;
    }

Solution is taken from here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜