0

How can I get the page title by its URL, for example http://example.com/en/news-events, which is not for the current page?

3

2 Answers 2

0

From below code example you can get the page title as your requirement

 $path = \Drupal::service('path.alias_manager')->getPathByAlias('/news-events'); if(preg_match('/node\/(\d+)/', $path, $matches)) { $node = \Drupal\node\Entity\Node::load($matches[1]); } echo $node->getTitle(); 

Hope this helps. :)

3
  • There is an extra ")" in your last line code. I used your code in my custom block hook and get the error: Call to a member function getTitle() on null Commented Jul 18, 2017 at 7:37
  • removed extra ")" Commented Jul 18, 2017 at 8:46
  • Yep. I removed extra ")" and still got same error. Commented Jul 18, 2017 at 8:49
-1

Try this solution for get title from view page url.

in your custom module ==> .module file

use Drupal\views\ViewExecutable; function display_form_mode_views_post_render(ViewExecutable $view) { print($view->getTitle($title)); } 

Hope this helps.

3
  • Your answer is too specific, since OP never mentioned whether it was on a Views page or not Commented Nov 29, 2023 at 14:53
  • Richard mentioned in his comment it was his view page URL Commented Nov 30, 2023 at 4:47
  • The question also says the URL is not for the current page, which means that any hook invoked for the current page is not invoked for the view associated to the given URL. Commented Dec 14, 2023 at 18:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.