You are here

class SocialProfileFieldsHelper in Open Social 8.6

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsHelper.php \Drupal\social_profile_fields\SocialProfileFieldsHelper
  2. 8.3 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsHelper.php \Drupal\social_profile_fields\SocialProfileFieldsHelper
  3. 8.4 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsHelper.php \Drupal\social_profile_fields\SocialProfileFieldsHelper
  4. 8.5 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsHelper.php \Drupal\social_profile_fields\SocialProfileFieldsHelper
  5. 8.7 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsHelper.php \Drupal\social_profile_fields\SocialProfileFieldsHelper
  6. 8.8 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsHelper.php \Drupal\social_profile_fields\SocialProfileFieldsHelper
  7. 10.3.x modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsHelper.php \Drupal\social_profile_fields\SocialProfileFieldsHelper
  8. 10.0.x modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsHelper.php \Drupal\social_profile_fields\SocialProfileFieldsHelper
  9. 10.1.x modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsHelper.php \Drupal\social_profile_fields\SocialProfileFieldsHelper
  10. 10.2.x modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsHelper.php \Drupal\social_profile_fields\SocialProfileFieldsHelper

Class SocialProfileFieldsHelper.

Hierarchy

Expanded class hierarchy of SocialProfileFieldsHelper

1 file declares its use of SocialProfileFieldsHelper
SocialProfileFieldsSettingsForm.php in modules/social_features/social_profile/modules/social_profile_fields/src/Form/SocialProfileFieldsSettingsForm.php
1 string reference to 'SocialProfileFieldsHelper'
social_profile_fields.services.yml in modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.services.yml
modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.services.yml
1 service uses SocialProfileFieldsHelper
social_profile_fields.helper in modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.services.yml
Drupal\social_profile_fields\SocialProfileFieldsHelper

File

modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsHelper.php, line 10

Namespace

Drupal\social_profile_fields
View source
class SocialProfileFieldsHelper {

  /**
   * Drupal\Core\Entity\EntityTypeManager definition.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * Constructs a new SocialProfileFieldsHelper object.
   */
  public function __construct(EntityTypeManager $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Functions fetches profile fields from a profile type.
   *
   * @param string $profile_type_id
   *   The profile bundle.
   *
   * @return array
   *   An array of fields.
   */
  public function getProfileFields($profile_type_id) {
    $fields = [];

    // Use storage to get only the profile fields of the current bundle type.
    try {
      $profile_fields = $this->entityTypeManager
        ->getStorage('field_config')
        ->loadByProperties([
        'entity_type' => 'profile',
        'bundle' => $profile_type_id,
      ]);
    } catch (\Exception $e) {
      return $fields;
    }

    // Loop through the fields and return the necessary values.

    /** @var \Drupal\Core\Field\FieldConfigInterface $profile_field */
    foreach ($profile_fields as $profile_field) {

      // Rewrite the ID a bit, since otherwise config thinks it's an array.
      $id = str_replace('.', '_', $profile_field
        ->id());

      // Build the array.
      $fields[$id] = [
        'id' => $id,
        'name' => $profile_field
          ->getName(),
        'label' => $profile_field
          ->getLabel(),
      ];
    }

    // Return the array of fields.
    return $fields;
  }

  /**
   * Get the user export plugin ids for a given field.
   *
   * @param string $field_id
   *   The field id, e.g. profile_profile_field_profile_address.
   *
   * @return array
   *   An array of plugins.
   */
  public function getUserExportPluginIdForField($field_id) {
    $mapping = $this
      ->mapProfileFieldsToUserExportPlugin();
    return array_keys($mapping, $field_id);
  }

  /**
   * Map profile fields to user export plugins.
   *
   * @return array
   *   An array of fields and user export plugins.
   */
  public function mapProfileFieldsToUserExportPlugin() {
    return [
      'user_first_name' => 'profile_profile_field_profile_first_name',
      'user_last_name' => 'profile_profile_field_profile_last_name',
      'user_address_country_code' => 'profile_profile_field_profile_address',
      'user_address_administrative' => 'profile_profile_field_profile_address',
      'user_address_locality' => 'profile_profile_field_profile_address',
      'user_address_postal_code' => 'profile_profile_field_profile_address',
      'user_address_line1' => 'profile_profile_field_profile_address',
      'user_address_line2' => 'profile_profile_field_profile_address',
      'user_phone_number' => 'profile_profile_field_profile_phone_number',
      'user_organization' => 'profile_profile_field_profile_organization',
      'user_function' => 'profile_profile_field_profile_function',
      'user_skills' => 'profile_profile_field_profile_expertise',
      'user_interests' => 'profile_profile_field_profile_interests',
      'user_profile_tag' => 'profile_profile_field_profile_profile_tag',
      'user_nickname' => 'profile_profile_field_profile_nick_name',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SocialProfileFieldsHelper::$entityTypeManager protected property Drupal\Core\Entity\EntityTypeManager definition.
SocialProfileFieldsHelper::getProfileFields public function Functions fetches profile fields from a profile type.
SocialProfileFieldsHelper::getUserExportPluginIdForField public function Get the user export plugin ids for a given field.
SocialProfileFieldsHelper::mapProfileFieldsToUserExportPlugin public function Map profile fields to user export plugins.
SocialProfileFieldsHelper::__construct public function Constructs a new SocialProfileFieldsHelper object.