You are here

function get_link_to_event_from_enrollment in Open Social 10.3.x

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

Render a link to an event as full link with title or just absolute as string.

Parameters

int $object_id: The ID of the event enrollment.

bool $as_link: True if you want the full link otherwise just a canonical URL string.

Return value

string|null the Url.

1 call to get_link_to_event_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 103
Builds placeholder replacement tokens for Social Event module.

Code

function get_link_to_event_from_enrollment($object_id, $as_link = FALSE) {
  $entity_storage = \Drupal::entityTypeManager()
    ->getStorage('event_enrollment');

  /** @var \Drupal\social_event\Entity\EventEnrollment $entity */
  $entity = $entity_storage
    ->load($object_id);
  if ($entity !== NULL) {
    $event_id = $entity
      ->getFieldValue('field_event', 'target_id');
    $storage = \Drupal::entityTypeManager()
      ->getStorage('node');

    /** @var \Drupal\node\Entity\Node $event */
    $event = $storage
      ->load($event_id);

    // Check if the event still exists.
    if ($event !== NULL) {
      if ($as_link) {
        $url_string = $event
          ->toUrl('canonical')
          ->toString(TRUE);
        return $url_string
          ->getGeneratedUrl();
      }
      return Link::fromTextAndUrl($event
        ->getTitle(), $event
        ->toUrl('canonical'))
        ->toString();
    }
  }
  return NULL;
}