You are here

function commons_groups_field_formatter_view in Drupal Commons 7.3

Implements hook_field_formatter_view().

File

modules/commons/commons_groups/commons_groups.module, line 1058

Code

function commons_groups_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  global $user;
  $account = clone $user;
  if ($display['type'] != 'commons_groups_group_subscribe') {
    return;
  }
  if (!og_is_group($entity_type, $entity)) {
    return;
  }
  if (!empty($entity->uid) && $entity->uid == $account->uid) {

    // User is the group manager.
    $element[0] = array(
      '#markup' => t('You are the group manager'),
    );
    return $element;
  }
  list($id, , $bundle) = entity_extract_ids($entity_type, $entity);

  // The user has a pending membership request. Let her know that
  // her request is pending review.
  if (og_is_member($entity_type, $id, 'user', $account, array(
    OG_STATE_PENDING,
  ))) {
    $element[0] = array(
      '#markup' => '<div class="subscription-type">' . t('Your membership request is pending review by a group organizer.') . '</div>',
    );
    return $element;
  }

  // If user is blocked, they should not be able to apply for membership.
  if (og_is_member($entity_type, $id, 'user', $account, array(
    OG_STATE_BLOCKED,
  ))) {
    return;
  }
  if (og_is_member($entity_type, $id, 'user', $account)) {

    // The user has an active membership.
    if (og_user_access($entity_type, $id, 'unsubscribe', $account)) {

      // The user has the permission to unsubscribe himself,
      // otherwise don't display a "Leave" link since the user can't leave
      // anyways.
      // For groups where anyone can contribute without joining, don't display
      // a "Leave" link since users never went through
      // the separate step of joining.
      if (og_is_member($entity_type, $id, 'user', $account, array(
        OG_STATE_ACTIVE,
      )) && $entity->field_og_subscribe_settings[LANGUAGE_NONE][0]['value'] != 'anyone') {
        $links['title'] = t('Leave group');
        $links['href'] = "group/{$entity_type}/{$id}/unsubscribe";
      }
    }
  }
  else {

    // Check if user can subscribe to the field.
    if (empty($settings['field_name']) && ($audience_field_name = og_get_best_group_audience_field('user', $account, $entity_type, $bundle))) {
      $settings['field_name'] = $audience_field_name;
    }
    if (!$settings['field_name']) {
      return;
    }
    $field_info = field_info_field($settings['field_name']);

    // Check if entity is referencable.
    if ($field_info['settings']['target_type'] != $entity_type) {

      // Group type doesn't match.
      return;
    }
    if (!empty($field_info['settings']['handler_settings']['target_bundles']) && !in_array($bundle, $field_info['settings']['handler_settings']['target_bundles'])) {

      // Bundles don't match.
      return;
    }
    if (!og_check_field_cardinality('user', $account, $settings['field_name'])) {
      $element[0] = array(
        '#markup' => format_plural($field_info['cardinality'], 'You are already registered to another group', 'You are already registered to @count groups'),
      );
      return $element;
    }
    $url = "group/{$entity_type}/{$id}/subscribe";
    if ($settings['field_name']) {
      $url .= '/' . $settings['field_name'];
    }

    // Set the needs update hook if we end up with a group call that is missing
    // the subscribe settings. We also check the variable first, because we
    // don't want to reset the variable cache if we don't have to.
    // See https://drupal.org/node/2059857#comment-7733465 for more info.
    if (empty($entity->field_og_subscribe_settings)) {
      if (!variable_get('commons_groups_needs_update', FALSE)) {
        variable_set('commons_groups_needs_update', TRUE);
      }
    }
    else {
      if ($entity->field_og_subscribe_settings[LANGUAGE_NONE][0]['value'] != 'anyone') {
        if ($entity->field_og_subscribe_settings[LANGUAGE_NONE][0]['value'] == 'approval') {
          $subscription_type = t('Moderated group');
          $links['title'] = t('Join group');
          if ($account->uid) {
            $links['href'] = $url;
          }
          else {
            $links['href'] = 'user/login';
            $links['options'] = array(
              'query' => array(
                'destination' => $url,
              ),
            );
          }
        }
        else {
          $element[0] = array(
            '#markup' => '<div class="subscription-type">' . t('Invite-only group') . '</div>',
          );
          return $element;
        }
      }
    }
  }
  if (!empty($links['title'])) {
    $links += array(
      'options' => array(),
    );
    $element[0] = array(
      '#type' => 'link',
      '#title' => $links['title'],
      '#href' => $links['href'],
      '#options' => $links['options'],
    );
    if (!empty($subscription_type)) {
      $element[0]['#prefix'] = '<div class="subscription-type">' . $subscription_type . '</div>';
    }
    return $element;
  }
}