Melhorias no microsite e sincronização de fotos LDAP

Fotos LDAP:
- Ignora sync quando conta ainda não tem UID (evitava URI compartilhada)
- Filtra fotos abaixo do tamanho mínimo configurável (padrão 10 KB)
- Adiciona campo ldap_min_photo_size nas configurações e schema
- Update 10010: remove fotos placeholder já existentes
- Update 10011: remove mídias com URI ldap_photo_.{ext} sem UID

Bloco de cabeçalho do microsite:
- Exibe departamento abaixo do nome, sem label, com link para a entidade
- Exibe telefone de trabalho (work_phone) no lugar de phone (restrito)

Página de perfil:
- Título fixo "Perfil de @name" via callback profileTitle()
- Exclui rota profile da substituição de título pelo nó homepage

Subpáginas com URL amigável:
- Adiciona MicrositeSubpagePathProcessor (inbound + outbound)
- Inbound: /user/{username}/{subpage} → /user/{uid}/{subpage}
- Outbound: /user/{uid}/{subpage} → /user/{username}/{subpage}
- Busca alias em todos os idiomas para contornar limitação do AliasManager

Tema do microsite em rotas externas:
- MicrositeThemeNegotiator cobre rotas com parâmetro user sob /user/{user}/
- Cobre nós do structural_pages cujo alias começa com /user/{uid}/

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-23 15:29:40 -03:00
parent aa24bf79f8
commit 39de6a7493
13 changed files with 404 additions and 15 deletions

View File

@@ -18,12 +18,14 @@ function site_users_microsite_theme(): array {
'photo_alt' => '',
'name' => NULL,
'bio' => NULL,
'phone' => NULL,
'email' => NULL,
'homepage' => NULL,
'lattes_id' => NULL,
'orcid_id' => NULL,
'mathscinet_id' => NULL,
'department' => NULL,
'department_url' => NULL,
'work_phone' => NULL,
],
],
];
@@ -78,12 +80,19 @@ function site_users_microsite_preprocess_block(&$variables): void {
}
$route_match = \Drupal::routeMatch();
$route_name = $route_match->getRouteName();
$route_name = $route_match->getRouteName() ?? '';
// Rotas com título próprio não devem ser sobrescritas.
$excluded = [
'site_users_microsite.profile',
'site_users_microsite.settings',
'site_users_microsite.user_config',
];
$is_microsite = $route_name === 'entity.user.canonical'
|| str_starts_with($route_name, 'site_users_microsite.');
if (!$is_microsite) {
if (!$is_microsite || in_array($route_name, $excluded)) {
return;
}

View File

@@ -2,7 +2,7 @@ site_users_microsite.profile:
path: '/user/{user}/profile'
defaults:
_controller: '\Drupal\site_users_microsite\Controller\MicrositeHomeController::profile'
_title_callback: '\Drupal\site_users_microsite\Controller\MicrositeHomeController::title'
_title_callback: '\Drupal\site_users_microsite\Controller\MicrositeHomeController::profileTitle'
requirements:
_entity_access: 'user.view'
user: \d+

View File

@@ -1,6 +1,14 @@
services:
site_users_microsite.path_processor:
class: Drupal\site_users_microsite\PathProcessor\MicrositeSubpagePathProcessor
arguments: ['@path_alias.manager', '@language_manager']
tags:
- { name: path_processor_inbound, priority: 200 }
- { name: path_processor_outbound, priority: 200 }
site_users_microsite.theme_negotiator:
class: Drupal\site_users_microsite\Theme\MicrositeThemeNegotiator
arguments: ['@path_alias.manager']
tags:
- { name: theme_negotiator, priority: 100 }

View File

@@ -99,4 +99,11 @@ class MicrositeHomeController extends ControllerBase {
return $this->t('@name', ['@name' => $user->getDisplayName()]);
}
/**
* Callback de título para a página de perfil.
*/
public function profileTitle(UserInterface $user): TranslatableMarkup {
return $this->t('Perfil de @name', ['@name' => $user->getDisplayName()]);
}
}

View File

@@ -0,0 +1,98 @@
<?php
namespace Drupal\site_users_microsite\PathProcessor;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\path_alias\AliasManagerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Processa subpáginas do microsite para funcionar com aliases de usuário.
*
* Converte /user/{username}/{subpage} <-> /user/{uid}/{subpage} de forma
* transparente, complementando o alias exato /user/{username} do Pathauto.
*/
class MicrositeSubpagePathProcessor implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
public function __construct(
private AliasManagerInterface $aliasManager,
private LanguageManagerInterface $languageManager,
) {}
/**
* {@inheritdoc}
*
* Converte /user/{username}/{subpage} para /user/{uid}/{subpage}.
*/
public function processInbound($path, Request $request) {
if (!preg_match('#^/user/([^/]+)(/.+)$#', $path, $matches)) {
return $path;
}
$segment = $matches[1];
$rest = $matches[2];
// Segmento numérico já é UID — nada a fazer.
if (is_numeric($segment)) {
return $path;
}
$alias = '/user/' . $segment;
$system_path = $this->lookupSystemPath($alias);
if ($system_path !== $alias && preg_match('#^/user/\d+$#', $system_path)) {
return $system_path . $rest;
}
return $path;
}
/**
* {@inheritdoc}
*
* Converte /user/{uid}/{subpage} para /user/{username}/{subpage}.
*/
public function processOutbound($path, &$options = [], ?Request $request = NULL, ?BubbleableMetadata $bubbleable_metadata = NULL) {
if (!preg_match('#^/user/(\d+)(/.+)$#', $path, $matches)) {
return $path;
}
$uid = $matches[1];
$rest = $matches[2];
$alias = $this->aliasManager->getAliasByPath('/user/' . $uid);
if ($alias !== '/user/' . $uid) {
if ($bubbleable_metadata) {
$bubbleable_metadata->addCacheContexts(['url.path']);
}
return $alias . $rest;
}
return $path;
}
/**
* Busca o caminho interno para um alias tentando todos os idiomas.
*
* O alias manager só faz fallback para LANGCODE_NOT_SPECIFIED; aliases
* armazenados com 'en' não são encontrados quando o idioma atual é 'pt-br'.
*/
private function lookupSystemPath(string $alias): string {
$langcodes = array_keys($this->languageManager->getLanguages());
$langcodes[] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
foreach ($langcodes as $langcode) {
$system_path = $this->aliasManager->getPathByAlias($alias, $langcode);
if ($system_path !== $alias) {
return $system_path;
}
}
return $alias;
}
}

View File

@@ -66,12 +66,14 @@ class MicrositeHeaderBlock extends BlockBase implements ContainerFactoryPluginIn
'#photo_alt' => $this->getPhotoAlt($user),
'#name' => $this->getFieldValue($user, 'field_user_name') ?: $user->getDisplayName(),
'#bio' => $this->getProcessedValue($user, 'field_user_bio'),
'#phone' => $this->getFieldValue($user, 'field_user_phone'),
'#email' => $user->getEmail(),
'#homepage' => $this->getFieldUri($user, 'field_user_homepage'),
'#lattes_id' => $this->getFieldValue($user, 'field_user_id_lattes'),
'#orcid_id' => $this->getFieldValue($user, 'field_user_orcid'),
'#mathscinet_id' => $this->getFieldValue($user, 'field_user_mathscinetid'),
'#department' => $this->getReferencedEntityLabel($user, 'field_user_department'),
'#department_url' => $this->getReferencedEntityUrl($user, 'field_user_department'),
'#work_phone' => $this->getFieldValue($user, 'field_user_work_phone'),
'#cache' => [
'tags' => $user->getCacheTags(),
'contexts' => ['route'],
@@ -145,6 +147,36 @@ class MicrositeHeaderBlock extends BlockBase implements ContainerFactoryPluginIn
return NULL;
}
/**
* Retorna o label da entidade referenciada por um campo entity_reference.
*/
protected function getReferencedEntityLabel(UserInterface $user, string $field_name): ?string {
if (!$user->hasField($field_name) || $user->get($field_name)->isEmpty()) {
return NULL;
}
$entity = $user->get($field_name)->entity;
return $entity ? $entity->label() : NULL;
}
/**
* Retorna a URL canônica da entidade referenciada por um campo entity_reference.
*/
protected function getReferencedEntityUrl(UserInterface $user, string $field_name): ?string {
if (!$user->hasField($field_name) || $user->get($field_name)->isEmpty()) {
return NULL;
}
$entity = $user->get($field_name)->entity;
if (!$entity || !$entity->hasLinkTemplate('canonical')) {
return NULL;
}
try {
return $entity->toUrl('canonical')->toString();
}
catch (\Exception $e) {
return NULL;
}
}
/**
* Retorna o valor de texto de um campo do usuário.
*/

View File

@@ -4,27 +4,70 @@ namespace Drupal\site_users_microsite\Theme;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Theme\ThemeNegotiatorInterface;
use Drupal\path_alias\AliasManagerInterface;
use Drupal\user\UserInterface;
/**
* Aplica o tema microsite nas rotas de perfil de usuário e do micro-site.
*/
class MicrositeThemeNegotiator implements ThemeNegotiatorInterface {
public function __construct(
private AliasManagerInterface $aliasManager,
) {}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match): bool {
$route_name = $route_match->getRouteName();
$route_name = $route_match->getRouteName() ?? '';
if ($route_name === 'site_users_microsite.settings') {
// Rotas administrativas e de edição nunca recebem o tema do microsite.
$excluded = [
'site_users_microsite.settings',
'site_users_microsite.user_config',
'site_users_microsite.my_config',
];
if (in_array($route_name, $excluded, TRUE)) {
return FALSE;
}
foreach (['entity.user.edit_', 'entity.user.cancel', 'user.admin'] as $prefix) {
if (str_starts_with($route_name, $prefix)) {
return FALSE;
}
}
// Rota canônica e rotas próprias do microsite.
if ($route_name === 'entity.user.canonical') {
return TRUE;
}
if (str_starts_with($route_name, 'site_users_microsite.')) {
return TRUE;
}
return str_starts_with($route_name, 'site_users_microsite.');
// Qualquer rota com parâmetro 'user' (entidade) sob /user/{user}/.
$user = $route_match->getParameter('user');
if ($user instanceof UserInterface) {
$route = $route_match->getRouteObject();
$path = $route ? $route->getPath() : '';
if (str_starts_with($path, '/user/{user}/')) {
return TRUE;
}
}
// Nós cujo alias começa com /user/{uid}/ (ex.: structural_pages).
if ($route_name === 'entity.node.canonical') {
$node = $route_match->getParameter('node');
if ($node) {
$nid = is_object($node) ? $node->id() : $node;
$alias = $this->aliasManager->getAliasByPath('/node/' . $nid);
if (preg_match('#^/user/\d+/#', $alias)) {
return TRUE;
}
}
}
return FALSE;
}
/**

View File

@@ -37,16 +37,26 @@
<h1 class="msite-header-block__name">{{ name }}</h1>
{% if department %}
<div class="msite-header-block__department">
{% if department_url %}
<a href="{{ department_url }}">{{ department }}</a>
{% else %}
{{ department }}
{% endif %}
</div>
{% endif %}
{% if bio %}
<div class="msite-header-block__bio">{{ bio|raw }}</div>
{% endif %}
{% if phone or email %}
{% if work_phone or email %}
<ul class="msite-header-block__contact">
{% if phone %}
{% if work_phone %}
<li class="msite-header-block__contact-item">
<span class="msite-header-block__contact-label">{{ 'Telefone'|t }}:</span>
<a href="tel:{{ phone }}">{{ phone }}</a>
<a href="tel:{{ work_phone }}">{{ work_phone }}</a>
</li>
{% endif %}
{% if email %}