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?
- There is no easy solution, see drupal.stackexchange.com/questions/178790/get-title-from-route. You can see what you would need to do in the path based breadcrumb builder: api.drupal.org/api/drupal/…4uk4– 4uk42017-07-17 09:30:32 +00:00Commented Jul 17, 2017 at 9:30
- Is that URL alias of views page OR node URL alias ?parth.k– parth.k2017-07-17 10:51:47 +00:00Commented Jul 17, 2017 at 10:51
- @parth.k This is an URL alias to my view page.Richard– Richard2017-07-18 07:39:06 +00:00Commented Jul 18, 2017 at 7:39
Add a comment |
2 Answers
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. :)
- 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 nullRichard– Richard2017-07-18 07:37:28 +00:00Commented Jul 18, 2017 at 7:37
-
- Yep. I removed extra ")" and still got same error.Richard– Richard2017-07-18 08:49:09 +00:00Commented Jul 18, 2017 at 8:49
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.
- Your answer is too specific, since OP never mentioned whether it was on a Views page or notmisterdidi– misterdidi2023-11-29 14:53:14 +00:00Commented Nov 29, 2023 at 14:53
- Richard mentioned in his comment it was his view page URLhetal chavda– hetal chavda2023-11-30 04:47:52 +00:00Commented 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.2023-12-14 18:12:28 +00:00Commented Dec 14, 2023 at 18:12