diff --git a/site_users.info.yml b/site_users.info.yml index cf1c69a..b1f4ed8 100644 --- a/site_users.info.yml +++ b/site_users.info.yml @@ -9,3 +9,4 @@ dependencies: - drupal:text - drupal:media - drupal:media_library + - site_tools:site_tools diff --git a/site_users.module b/site_users.module index 6c0fdc9..7d09fc6 100644 --- a/site_users.module +++ b/site_users.module @@ -12,8 +12,10 @@ use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Url; use Drupal\field\FieldConfigInterface; use Drupal\media\MediaInterface; +use Drupal\site_users\Plugin\Field\FieldType\SocialLinkItem; use Drupal\user\UserInterface; /** @@ -329,6 +331,49 @@ function site_users_user_presave(UserInterface $user) { } } +/** + * Implements hook_site_tools_share_links(). + */ +function site_users_site_tools_share_links(): array { + $user = \Drupal::routeMatch()->getParameter('user'); + + if (!($user instanceof UserInterface)) { + return []; + } + + if (!$user->hasField('field_user_social_links') || $user->get('field_user_social_links')->isEmpty()) { + return []; + } + + $links = []; + $networks = SocialLinkItem::getNetworks(); + + foreach ($user->get('field_user_social_links') as $delta => $item) { + if ($item->isEmpty()) { + continue; + } + + $network_label = $networks[$item->network] ?? $item->network; + + $links['social_' . $item->network] = [ + 'content' => [ + '#type' => 'link', + '#title' => $network_label, + '#url' => Url::fromUri($item->url), + '#attributes' => [ + 'class' => ['social-link', 'social-link--' . $item->network], + 'target' => '_blank', + 'rel' => 'noopener noreferrer', + ], + ], + 'weight' => $delta, + 'provider' => 'site_users', + ]; + } + + return $links; +} + /** * Obtém a foto padrão de um usuário. *