<?php namespace Drupal\musin_core\Service; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Session\AccountInterface; class UserHelperService { /** * @var \Drupal\Core\Session\AccountInterface */ protected $currentUser; /** * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; public function __construct(AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager) { $this->currentUser = $current_user; $this->entityTypeManager = $entity_type_manager; } public static function getProfileCompleteness($user) { $field_weightings = [ 'field_first_name' => 10, 'field_last_name' => 10, 'field_city' => 10, 'field_age' => 10, 'user_picture' => 10, 'field_cover_image' => 10, 'field_genres' => 5, 'field_instruments' => 5, 'field_sample_tracks' => 5, 'field_short_biography' => 5, 'field_social_media_soundcloud' => 5, 'field_social_media_spotify' => 5, 'field_social_media_tiktok' => 5, 'field_social_media_youtube' => 5, ]; $completeness = 0; foreach ($field_weightings as $key => $value) { if ($user->hasField($key) && $user->get($key)->isEmpty() === FALSE) { $completeness = $completeness + $value; } } return $completeness; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter