You are here

class SocialProfileFieldsHelper in Open Social 10.3.x

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.6 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsHelper.php \Drupal\social_profile_fields\SocialProfileFieldsHelper
  6. 8.7 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsHelper.php \Drupal\social_profile_fields\SocialProfileFieldsHelper
  7. 8.8 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

Defines the helper service.

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 11

Namespace

Drupal\social_profile_fields
View source
class SocialProfileFieldsHelper {

  /**
   * Entity type manager for loading entities.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a new SocialProfileFieldsHelper object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager for loading entities.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   Module handler service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler) {
    $this->entityTypeManager = $entity_type_manager;
    $this->moduleHandler = $module_handler;
  }

  /**
   * 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() {
    $mapping = [
      '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_address_field_city',
      'user_address_postal_code' => 'profile_address_field_postalcode',
      'user_address_administrative_area' => 'profile_address_field_administrative_area',
      'user_address_line1' => 'profile_address_field_address',
      'user_address_line2' => 'profile_profile_field_profile_address',
      'user_phone_number' => 'profile_profile_field_profile_phone_number',
      'user_nationality' => 'profile_profile_field_profile_nationality',
      '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',
    ];

    // Allow other modules to alter the mapping.
    $this->moduleHandler
      ->alter('profile_field_export_mapping', $mapping);
    return $mapping;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SocialProfileFieldsHelper::$entityTypeManager protected property Entity type manager for loading entities.
SocialProfileFieldsHelper::$moduleHandler protected property Module handler service.
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.