get('current_route_match'), $container->get('entity_type.manager'), ); } /** * {@inheritdoc} */ public function defaultConfiguration(): array { return ['label_display' => '0'] + parent::defaultConfiguration(); } /** * {@inheritdoc} */ public function build(): array { $user = $this->getUser(); if (!$user instanceof UserInterface) { return []; } return [ '#theme' => 'microsite_header_block', '#photo_url' => $this->getPhotoUrl($user), '#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'), '#cache' => [ 'tags' => $user->getCacheTags(), 'contexts' => ['route'], ], ]; } /** * Retorna o usuário da rota atual. */ protected function getUser(): ?UserInterface { $user = $this->routeMatch->getParameter('user'); if ($user instanceof UserInterface) { return $user; } if (is_numeric($user)) { return $this->entityTypeManager->getStorage('user')->load($user); } return NULL; } /** * Retorna a URL absoluta da foto padrão do usuário. */ protected function getPhotoUrl(UserInterface $user): ?string { if (!function_exists('site_users_get_default_photo')) { return NULL; } $media = site_users_get_default_photo($user); if (!$media) { return NULL; } $source_field = $media->getSource()->getConfiguration()['source_field']; if (!$media->hasField($source_field) || $media->get($source_field)->isEmpty()) { return NULL; } $file = $media->get($source_field)->entity; if (!$file) { return NULL; } return \Drupal::service('file_url_generator')->generateAbsoluteString($file->getFileUri()); } /** * Retorna o texto alternativo da foto padrão. */ protected function getPhotoAlt(UserInterface $user): string { if (!function_exists('site_users_get_default_photo')) { return ''; } $media = site_users_get_default_photo($user); return $media ? $media->label() : ''; } /** * Retorna a URI de um campo link do usuário. */ protected function getFieldUri(UserInterface $user, string $field_name): ?string { if ($user->hasField($field_name) && !$user->get($field_name)->isEmpty()) { return $user->get($field_name)->uri; } return NULL; } /** * Retorna o valor de texto de um campo do usuário. */ protected function getFieldValue(UserInterface $user, string $field_name): ?string { if ($user->hasField($field_name) && !$user->get($field_name)->isEmpty()) { return $user->get($field_name)->value; } return NULL; } /** * Retorna o valor processado (HTML filtrado) de um campo text_long. * * Usa ->processed, que aplica o formato de texto configurado e retorna um * objeto Markup — o Twig renderiza como HTML sem escape adicional. */ protected function getProcessedValue(UserInterface $user, string $field_name): ?string { if ($user->hasField($field_name) && !$user->get($field_name)->isEmpty()) { return $user->get($field_name)->processed; } return NULL; } /** * {@inheritdoc} */ public function getCacheTags(): array { $user = $this->getUser(); if ($user instanceof UserInterface) { return Cache::mergeTags(parent::getCacheTags(), $user->getCacheTags()); } return parent::getCacheTags(); } /** * {@inheritdoc} */ public function getCacheContexts(): array { return Cache::mergeContexts(parent::getCacheContexts(), ['route']); } }