Change the title of a View to show a name based on its numeric contextual filter
- twig conditional based on path drupal 8
- load username in twig from uid
This is how NOT to do it.
/**
* Implements hook_preprocess_page_title();
*
* @param $variables
*/
function geofresco_preprocess_page_title(&$variables) {
// WE SHALL BE SHAMELESS.
if (!isset($variables['title']) || !is_array($variables['title'])) {
// The pages we're interested in have title as markup, so bail if it's not.
return;
}
$string = isset($variables['title']['#markup']) ? $variables['title']['#markup'] : FALSE;
if ($string && $pos = strpos($string, "'s blog", -7)) {
$uid = substr($string, 0, $pos);
if (is_numeric($uid) && $account = \Drupal\user\Entity\User::load($uid)) {
$variables['title'] = [
'#markup' => t("@username's blog",
['@username' => $account->getDisplayName()])
];
}
}
}
The approach in the answer here is what we’re doing, the path having the username is handled with an alias. But that doesn’t solve autocreating aliases (which we still need to solve) and it doesn’t solve the title of the view.
Just realizing that the html_page_title will still be wrong… will fix that later, and this abomination of a function will also be gone
- drupal 8 blog view per user rss
- drupal 8 generate path aliases for views
https://www.webwash.net/custom-tab-user-profile-page-views-drupal-8/
https://www.drupal.org/project/drupal/issues/2904908
https://www.drupal.org/project/issues/views_url_alias?categories=All
https://envisioninteractive.com/drupal/using-the-pathauto-api-drupal-module-a-simple-example/