You are here

public function UserProfilePage::evaluate in Context 8.4

Evaluates the condition and returns TRUE or FALSE accordingly.

Return value

bool TRUE if the condition has been met, FALSE otherwise.

Overrides ConditionInterface::evaluate

File

src/Plugin/Condition/UserProfilePage.php, line 147

Class

UserProfilePage
Provides a 'User profile page status' condition.

Namespace

Drupal\context\Plugin\Condition

Code

public function evaluate() {
  $route = $this->currentRouteMatch
    ->getCurrentRouteMatch();
  $configuration = $this
    ->getConfiguration();

  // Check if no option is checked.
  if (empty($configuration['user_status']) || !array_filter($configuration['user_status'])) {
    return TRUE;
  }
  else {
    foreach ($configuration['user_status'] as $key => $value) {
      if (empty($value)) {
        unset($configuration['user_status'][$key]);
      }
    }
    $user_conf = $configuration['user_status'];
  }

  // Match all entity.user.* routes having user parameter,
  // which include regular user profile view (entity.user.canonical),
  // user edit form (entity.user.edit_form),...
  if (strpos($route
    ->getRouteName(), 'entity.user.') === 0) {
    $user_id = $this->currentRouteMatch
      ->getRawParameter('user');
    if ($user_id === NULL) {
      return FALSE;
    }
    if (in_array("viewing_profile", $user_conf)) {
      return TRUE;
    }
    else {
      if (in_array("logged_viewing_profile", $user_conf) && $this->currentUser
        ->isAuthenticated()) {
        return TRUE;
      }
      else {
        if (in_array("own_page_true", $user_conf) && $this->currentUser
          ->isAuthenticated() && $user_id == $this->currentUser
          ->id()) {
          return TRUE;
        }
        else {
          if (in_array("field_value", $user_conf)) {
            $user = User::load($user_id);

            // Check if field is entity_reference or normal field with values.
            $field_target = $user
              ->get($configuration['user_fields'])->target_id;
            if ($field_target) {
              $field = $field_target;
            }
            else {
              $field = $user
                ->get($configuration['user_fields'])->value;
            }

            // Condition check.
            if ($field || !$field == 0) {
              return TRUE;
            }
          }
        }
      }
    }
  }
  return FALSE;
}