You are here

function opigno_calendar_views_pre_render in Opigno calendar 8

Same name and namespace in other branches
  1. 3.x opigno_calendar.module \opigno_calendar_views_pre_render()

Implements hook_views_pre_render().

File

./opigno_calendar.module, line 291
Contains opigno_calendar.module.

Code

function opigno_calendar_views_pre_render(ViewExecutable $view) {
  if ($view
    ->id() !== 'opigno_calendar') {
    return;
  }
  $account = Drupal::currentUser();
  if (!empty($view->result)) {
    foreach ($view->result as $key => $result) {
      if (empty($result->_entity)) {
        continue;
      }

      // Allow access for admins and other user with according permissions.
      if ($account
        ->hasPermission('manage group members in any group') || $account
        ->hasPermission('manage group content in any group')) {
        continue;
      }

      // If current user is a direct member of the calendar event.
      if ($result->_entity
        ->hasField('field_calendar_event_members')) {

        // If there are no members - continue.
        if (!count($result->_entity
          ->get('field_calendar_event_members')
          ->getValue())) {
          continue;
        }
        $members = array_map(function ($member) {
          return (int) $member['target_id'];
        }, $result->_entity
          ->get('field_calendar_event_members')
          ->getValue());
        if (in_array($account
          ->id(), $members)) {
          continue;
        }
      }
      else {
        continue;
      }
      unset($view->result[$key]);
    }
  }
}