Lunghezza caratteri username
Ciao,
mi trovo alle prese con la necessità di allungare il numero di carateri visualizzati nei nomi utente.
Attualmente Drupal 8 tronca i nomi a 15 caratteri mentre a me serve allungare questo numero a 21.
Temporaneamente ho risolto modificando manualmente i numeri di caratteri in user.module:
<code>
// Set the name to a formatted name that is safe for printing and
// that won't break tables by being too long. Keep an unshortened,
// unsanitized version, in case other preprocess functions want to implement
// their own shortening logic or add markup. If they do so, they must ensure
// that $variables['name'] is safe for printing.
$name = $account->getDisplayName();
$variables['name_raw'] = $account->getUsername();
if (Unicode::strlen($name) > 20) {
$name = Unicode::truncate($name, 15, FALSE, TRUE);
$variables['truncated'] = TRUE;
}
else {
$variables['truncated'] = FALSE;
}
$variables['name'] = $name;
$variables['profile_access'] = \Drupal::currentUser()->hasPermission('access user profiles');
$external = FALSE;
</code>
In rete ho trovato come intervenire dal tema ma solo per Drupal 7, qualcuno sa come si può fare in Drupal 8?
Risposte
Ciao, credo tu possa agire
Ciao, credo tu possa agire sia a livello di preprocessing usando l'hook template_preprocess_username (https://api.drupal.org/api/drupal/core!modules!user!user.module/function/template_preprocess_username) oppure sovrascrivendo il template di default username.html.twig (https://api.drupal.org/api/drupal/core%21modules%21user%21templates%21us...): nel template puoi usare la variabile name_raw che come puoi vedere anche sui commenti del template di default è il nome dell'utente non troncato.
Risolto
Risolto con username.html.twig grazie :-)
Semplifico nel caso serva ad altri che magari non sanno dove mettere le mani.
{% if link_path -%}
<a{{ attributes }}>{{ name }}{{ extra }}</a>
{%- else -%}
<span{{ attributes }}>{{ name }}{{ extra }}</span>
{%- endif -%}
in:
{% if link_path -%}
<a{{ attributes }}>{{ name_raw }}{{ extra }}</a>
{%- else -%}
<span{{ attributes }}>{{ name_raw }}{{ extra }}</span>
{%- endif -%}
Questa soluzione, dato che mostra il nome utente completo, va bene solamente se - come nel mio caso - ci sono un ristretto numero di utenti con una lunghezza del nome conosciuta o non vi importa se i nome utente possano arrivare a 60 caratteri. (lunghezza massima consentita per un nome utente)
Grazie a te per aver postato
Grazie a te per aver postato l'intera soluzione passo passo con le spiegazioni.