From 39de6a7493590769baf90630ff6fcff0d2b1b4be Mon Sep 17 00:00:00 2001 From: "Quintino A. G. Souza" Date: Mon, 23 Mar 2026 15:29:40 -0300 Subject: [PATCH] =?UTF-8?q?Melhorias=20no=20microsite=20e=20sincroniza?= =?UTF-8?q?=C3=A7=C3=A3o=20de=20fotos=20LDAP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- config/install/site_users.settings.yml | 1 + config/schema/site_users.schema.yml | 3 + .../site_users_microsite.module | 15 +- .../site_users_microsite.routing.yml | 2 +- .../site_users_microsite.services.yml | 8 + .../Controller/MicrositeHomeController.php | 7 + .../MicrositeSubpagePathProcessor.php | 98 +++++++++++ .../src/Plugin/Block/MicrositeHeaderBlock.php | 34 +++- .../src/Theme/MicrositeThemeNegotiator.php | 49 +++++- .../microsite-header-block.html.twig | 16 +- site_users.install | 159 +++++++++++++++++- src/Form/SiteUsersSettingsForm.php | 17 +- src/Service/LdapPhotoSyncService.php | 10 ++ 13 files changed, 404 insertions(+), 15 deletions(-) create mode 100644 modules/site_users_microsite/src/PathProcessor/MicrositeSubpagePathProcessor.php diff --git a/config/install/site_users.settings.yml b/config/install/site_users.settings.yml index f02de05..9c54c13 100644 --- a/config/install/site_users.settings.yml +++ b/config/install/site_users.settings.yml @@ -2,6 +2,7 @@ photos: max_count: 5 ldap_attribute: 'jpegPhoto' ldap_sync_enabled: false + ldap_min_photo_size: 10240 user_editable_fields: field_user_name: true field_user_phone: true diff --git a/config/schema/site_users.schema.yml b/config/schema/site_users.schema.yml index d14adf0..e38fa12 100644 --- a/config/schema/site_users.schema.yml +++ b/config/schema/site_users.schema.yml @@ -15,6 +15,9 @@ site_users.settings: ldap_sync_enabled: type: boolean label: 'Enable LDAP photo synchronization' + ldap_min_photo_size: + type: integer + label: 'Minimum LDAP photo size in bytes' user_editable_fields: type: sequence label: 'User-editable profile fields' diff --git a/modules/site_users_microsite/site_users_microsite.module b/modules/site_users_microsite/site_users_microsite.module index 6698f8d..18bde3a 100644 --- a/modules/site_users_microsite/site_users_microsite.module +++ b/modules/site_users_microsite/site_users_microsite.module @@ -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; } diff --git a/modules/site_users_microsite/site_users_microsite.routing.yml b/modules/site_users_microsite/site_users_microsite.routing.yml index f5d3787..e6d93b8 100644 --- a/modules/site_users_microsite/site_users_microsite.routing.yml +++ b/modules/site_users_microsite/site_users_microsite.routing.yml @@ -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+ diff --git a/modules/site_users_microsite/site_users_microsite.services.yml b/modules/site_users_microsite/site_users_microsite.services.yml index ed1b93b..da26279 100644 --- a/modules/site_users_microsite/site_users_microsite.services.yml +++ b/modules/site_users_microsite/site_users_microsite.services.yml @@ -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 } diff --git a/modules/site_users_microsite/src/Controller/MicrositeHomeController.php b/modules/site_users_microsite/src/Controller/MicrositeHomeController.php index d10985d..80fd38a 100644 --- a/modules/site_users_microsite/src/Controller/MicrositeHomeController.php +++ b/modules/site_users_microsite/src/Controller/MicrositeHomeController.php @@ -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()]); + } + } diff --git a/modules/site_users_microsite/src/PathProcessor/MicrositeSubpagePathProcessor.php b/modules/site_users_microsite/src/PathProcessor/MicrositeSubpagePathProcessor.php new file mode 100644 index 0000000..0716414 --- /dev/null +++ b/modules/site_users_microsite/src/PathProcessor/MicrositeSubpagePathProcessor.php @@ -0,0 +1,98 @@ + /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; + } + +} diff --git a/modules/site_users_microsite/src/Plugin/Block/MicrositeHeaderBlock.php b/modules/site_users_microsite/src/Plugin/Block/MicrositeHeaderBlock.php index e4b599c..989fcb2 100644 --- a/modules/site_users_microsite/src/Plugin/Block/MicrositeHeaderBlock.php +++ b/modules/site_users_microsite/src/Plugin/Block/MicrositeHeaderBlock.php @@ -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. */ diff --git a/modules/site_users_microsite/src/Theme/MicrositeThemeNegotiator.php b/modules/site_users_microsite/src/Theme/MicrositeThemeNegotiator.php index 2a46d71..cccfef1 100644 --- a/modules/site_users_microsite/src/Theme/MicrositeThemeNegotiator.php +++ b/modules/site_users_microsite/src/Theme/MicrositeThemeNegotiator.php @@ -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; } /** diff --git a/modules/site_users_microsite/templates/microsite-header-block.html.twig b/modules/site_users_microsite/templates/microsite-header-block.html.twig index 6ea4a37..3855cfb 100644 --- a/modules/site_users_microsite/templates/microsite-header-block.html.twig +++ b/modules/site_users_microsite/templates/microsite-header-block.html.twig @@ -37,16 +37,26 @@

{{ name }}

+ {% if department %} +
+ {% if department_url %} + {{ department }} + {% else %} + {{ department }} + {% endif %} +
+ {% endif %} + {% if bio %}
{{ bio|raw }}
{% endif %} - {% if phone or email %} + {% if work_phone or email %}