SocialProfilePrivacyHelper.php in Open Social 10.2.x
File
modules/social_features/social_profile/modules/social_profile_privacy/src/Service/SocialProfilePrivacyHelper.php
View source
<?php
namespace Drupal\social_profile_privacy\Service;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldConfigInterface;
use Drupal\Core\Session\AccountInterface;
class SocialProfilePrivacyHelper implements SocialProfilePrivacyHelperInterface {
protected $entityTypeManager;
protected $entityFieldManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
}
public function getFieldOptions(AccountInterface $account = NULL) {
$display = $this->entityTypeManager
->getStorage('entity_form_display')
->load('profile.profile.default');
$definitions = $this->entityFieldManager
->getFieldDefinitions('profile', 'profile');
$handler = $this->entityTypeManager
->getAccessControlHandler('profile');
if ($account) {
$storage = $this->entityTypeManager
->getStorage('profile');
$profile = $storage
->loadByUser($account, 'profile');
}
$options = [];
foreach (array_keys($display
->getComponents()) as $field) {
$definition = $definitions[$field];
if ($definition instanceof FieldConfigInterface) {
$items = $account ? $profile
->get($field) : NULL;
$options[$field] = [
'label' => $definition
->label(),
'access' => $handler
->fieldAccess('edit', $definition, $account, $items),
];
}
}
return $options;
}
}