You are here

function get_name_from_enrollment in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/social_event.tokens.inc \get_name_from_enrollment()
  2. 8.4 modules/social_features/social_event/social_event.tokens.inc \get_name_from_enrollment()
  3. 8.5 modules/social_features/social_event/social_event.tokens.inc \get_name_from_enrollment()
  4. 8.6 modules/social_features/social_event/social_event.tokens.inc \get_name_from_enrollment()
  5. 8.7 modules/social_features/social_event/social_event.tokens.inc \get_name_from_enrollment()
  6. 8.8 modules/social_features/social_event/social_event.tokens.inc \get_name_from_enrollment()
  7. 10.3.x modules/social_features/social_event/social_event.tokens.inc \get_name_from_enrollment()
  8. 10.1.x modules/social_features/social_event/social_event.tokens.inc \get_name_from_enrollment()
  9. 10.2.x modules/social_features/social_event/social_event.tokens.inc \get_name_from_enrollment()

Get the user display name from an Enrollment also for AN.

Parameters

int $id: the Enrollment entity ID.

Return value

array|\Drupal\Component\Render\MarkupInterface|mixed|null|string Markup for the Username.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to get_name_from_enrollment()
social_event_tokens in modules/social_features/social_event/social_event.tokens.inc
Implements hook_tokens().

File

modules/social_features/social_event/social_event.tokens.inc, line 140
Builds placeholder replacement tokens for Social Event module.

Code

function get_name_from_enrollment($id) {
  $entity_storage = \Drupal::entityTypeManager()
    ->getStorage('event_enrollment');

  /** @var \Drupal\social_event\Entity\EventEnrollment $entity */
  $enrollment = $entity_storage
    ->load($id);

  // User is AN by default.
  $enrollment_name = \Drupal::configFactory()
    ->get('user.settings')
    ->get('anonymous');
  if ($enrollment instanceof EventEnrollmentInterface) {

    // If there is a Uid. Lets load the user and return his display name.
    if ($enrollment !== NULL && $enrollment
      ->hasField('field_account') && $enrollment
      ->getFieldValue('field_account', 'target_id') > 0) {
      $entity_storage = \Drupal::entityTypeManager()
        ->getStorage('user');

      /** @var \Drupal\user\Entity\User $user */
      $user = $entity_storage
        ->load($enrollment
        ->getFieldValue('field_account', 'target_id'));
      return $user
        ->getDisplayName();
    }
  }

  // User is AN but has enrollment settings available, lets see if we can get
  // the Firstname and lastname to show up in the notification.
  if ($enrollment !== NULL && $enrollment
    ->hasField('field_first_name') && $enrollment
    ->getFieldValue('field_first_name', 'value') !== NULL && $enrollment
    ->getFieldValue('field_last_name', 'value') !== NULL) {

    // Create the Name from AN Attendees if possible.
    $enrollment_name = $enrollment
      ->getFieldValue('field_first_name', 'value') . ' ' . $enrollment
      ->getFieldValue('field_last_name', 'value');
  }
  return $enrollment_name;
}