You are here

function social_tour_page_bottom in Open Social 8.5

Same name and namespace in other branches
  1. 8.9 modules/custom/social_tour/social_tour.module \social_tour_page_bottom()
  2. 8 modules/custom/social_tour/social_tour.module \social_tour_page_bottom()
  3. 8.2 modules/custom/social_tour/social_tour.module \social_tour_page_bottom()
  4. 8.3 modules/custom/social_tour/social_tour.module \social_tour_page_bottom()
  5. 8.4 modules/custom/social_tour/social_tour.module \social_tour_page_bottom()
  6. 8.6 modules/custom/social_tour/social_tour.module \social_tour_page_bottom()
  7. 8.7 modules/custom/social_tour/social_tour.module \social_tour_page_bottom()
  8. 8.8 modules/custom/social_tour/social_tour.module \social_tour_page_bottom()
  9. 10.3.x modules/custom/social_tour/social_tour.module \social_tour_page_bottom()
  10. 10.0.x modules/custom/social_tour/social_tour.module \social_tour_page_bottom()
  11. 10.1.x modules/custom/social_tour/social_tour.module \social_tour_page_bottom()
  12. 10.2.x modules/custom/social_tour/social_tour.module \social_tour_page_bottom()

Implements hook_page_bottom().

Because the social_tour weight is higher than the core tour.module weight we make sure this code runs after tour_page_bottom(). Thus we can remove any tours a user has already seen.

File

modules/custom/social_tour/social_tour.module, line 146
The Social Tour module.

Code

function social_tour_page_bottom(array &$page_bottom) {
  if (!_social_tour_init()) {
    return;
  }

  // Load all of the items and match on route name.
  $route_match = \Drupal::routeMatch();
  $route_name = $route_match
    ->getRouteName();
  $seen_before = FALSE;

  // Get all the tours available for the current route name.
  $results = \Drupal::entityQuery('tour')
    ->condition('routes.*.route_name', $route_name)
    ->execute();
  if (!empty($results) && ($tours = Tour::loadMultiple(array_keys($results)))) {
    foreach ($tours as $id => $tour) {

      // EDGE CASE:
      // THIS MUST REMAIN ON TOP OF THE FOREACH.
      // social-profile  needs to made available on your own profile only.
      // So it must be removed from the array, but not marked as seen before,
      // when you visit the profile of others.
      if ($id === 'social-profile' && isset($page_bottom['tour'][$id])) {

        /** @var \Drupal\user\Entity\User $profile */
        $profile = $route_match
          ->getParameter('user');
        if ($profile
          ->id() != \Drupal::currentUser()
          ->id()) {

          // If it's not your own profile, we don't show this tour.
          unset($tours[$id]);
          unset($page_bottom['tour'][$id]);
          continue;
        }
      }

      // Now we check the user data, if it's set for the current tour we don't
      // have to render it anymore. because that means the user has already
      // seen it, either they finished the tour or they skipped the tour.
      // But in any way we don't want the user to see the tour twice.
      if (\Drupal::currentUser()
        ->id() !== 0) {

        // For logged in users of any role we add it to the user data.
        // For each tour that is rendered we set the tour id to TRUE.
        $seen_before = \Drupal::service('user.data')
          ->get('social_tour', \Drupal::currentUser()
          ->id(), $id);
      }
      else {

        // For Anonymous we put it in a session. Probably they won't even see
        // a tour.
        $seen_before = $_SESSION['social_tour'][$id];
      }
      if ($seen_before) {

        // Remove the tour from the render array.
        unset($tours[$id]);

        // Remove the tour from the page bottom array if the tour module already
        // added it. See tour_page_bottom() in tour.module.
        unset($page_bottom['tour'][$id]);
      }
    }
    if (!empty($tours)) {

      // Ah so we rendered the tour in the bottom of the page, we can also set
      // it to the user data so the user doesn't see it anymore.
      foreach ($tours as $id => $tour) {
        if (\Drupal::currentUser()
          ->id() !== 0) {

          // For logged in users of any role we add it to the user data.
          // For each tour that is rendered we set the tour id to TRUE.
          \Drupal::service('user.data')
            ->set('social_tour', \Drupal::currentUser()
            ->id(), $id, TRUE);
        }
        else {

          // For Anonymous we put it in a session. Probably they won't even see
          // a tour.
          $_SESSION['social_tour'][$id] = TRUE;
        }
      }
    }
  }
}