You are here

function discussthis_field_formatter_view in Discuss This! 7.2

Implements hook_field_formatter_view().

File

./discussthis.fields.inc, line 92
DiscussThis field declaration using Field Types API.

Code

function discussthis_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $return_links = array();

  // Lookup for topic nid, if it exists (otherwise we get 0).
  $topic_nid = _discussthis_get_topic($entity->nid);
  switch ($display['type']) {

    // This formatter simply outputs the link to the discussion.
    case 'field_discussthis_link':
      foreach ($items as $delta => $item) {
        $links = array();

        // Verify that the type has a valid discussion forum selected.
        $forum_tid = _discussthis_get_forum($entity->nid, $entity->type);
        if (!$forum_tid) {
          return $links;
        }

        // Get the DiscusssThis! link text.
        $discussthis_link = variable_get('discussthis_link', '');
        if (!$discussthis_link) {
          $discussthis_link = t('Discuss This!');
        }

        // Get the participate in DiscusssThis! link text.
        $participate_link = variable_get('discussthis_participate', '');
        if (!$participate_link) {
          $participate_link = t('Participate in this discussion');
        }

        // If the user is connected and have permission
        if ($entity->uid && user_access('access discuss this links')) {
          if ($topic_nid) {
            $all = _discussthis_comment_num_all($topic_nid);
            if (variable_get('discussthis_showcounts', 1)) {
              $new = comment_num_new($topic_nid);
              $counts = t(' (@new new/@all total)', array(
                '@new' => $new,
                '@all' => $all,
              ));
            }
            $links = array(
              // use "Discuss This!" when no discussion started yet
              '#type' => 'link',
              '#title' => $all == 0 ? $discussthis_link : $participate_link . $counts,
              '#href' => 'node/' . $topic_nid,
              '#attributes' => array(
                '#class' => 'discussthis-link',
                '#title' => t('Participate to the discussion about this page'),
              ),
            );
          }
          elseif ($topic_nid == 0) {

            // Check whether the user has permission to initiate a new topics.
            if (user_access('initiate discuss this topics')) {
              $links = array(
                '#type' => 'link',
                '#title' => $discussthis_link,
                '#href' => 'discussthis/create/' . $entity->nid,
                '#attributes' => array(
                  '#class' => 'discussthis-link',
                  '#title' => t('Start a discussion about this page'),
                  '#rel' => 'nofollow',
                ),
              );
            }
          }
        }
        else {

          // This happens for anonymous users
          // If the user is not logged in, offer him to do so.
          // Otherwise, would have access to the link.
          if (variable_get('discussthis_login', 1) && user_access('access discuss this links')) {
            if ($topic_nid) {
              $all = _discussthis_comment_num_all($topic_nid);
              if (variable_get('discussthis_showcounts', 1)) {
                $new = comment_num_new($topic_nid);
                $counts = t(' (@new new/@all total)', array(
                  '@new' => $new,
                  '@all' => $all,
                ));
              }
              $destination = 'destination=node/' . $topic_nid;
              $appeal = $all == 0 ? $discussthis_link : $participate_link . $counts;
            }
            else {

              // The topic does not exist.
              $destination = 'destination=discussthis/new/' . $entity->nid;
              $appeal = $discussthis_link;
            }
            $attributes = array(
              'class' => 'discussthis-login',
              'title' => t('Log in or register and start a discussion about this page'),
              'rel' => 'nofollow',
            );
            if (variable_get('user_register', 1)) {
              if (variable_get('user_email_verification', 1)) {

                // Users can register themselves but have to go through e-mail verification process
                $href = t('!login or !register to @appeal', array(
                  '!login' => l(t('Log in'), 'user/login', array(
                    'query' => drupal_get_destination(),
                    'attributes' => $attributes,
                  )),
                  '!register' => l(t('Register'), 'user/register'),
                  '@appeal' => $appeal,
                ));
              }
              else {

                // Users can register themselves without e-mail verification
                $href = t('!login or !register to @appeal', array(
                  '!login' => l(t('Log in'), 'user/login', array(
                    'query' => drupal_get_destination(),
                  )),
                  '!register' => l(t('Register'), 'user/register', array(
                    'query' => drupal_get_destination(),
                    'attributes' => $attributes,
                  )),
                  '@appeal' => $appeal,
                ));
              }
            }
            else {

              // Only admins can add new users, no public registration.
              $href = t('!login to @appeal', array(
                '!login' => l('Log in', 'user/login', array(
                  'query' => drupal_get_destination(),
                  'attributes' => $attributes,
                )),
                '@appeal' => $appeal,
              ));
            }
            $links = array(
              '#type' => 'link',
              '#title' => $href,
              '#href' => '',
              '#options' => array(
                'html' => TRUE,
              ),
            );
          }
        }
        $return_links[$delta] = $links;
      }
      break;
  }
  return $return_links;
}