You are here

socialbase.theme in Open Social 8.5

The primary PHP file for the Social base theme.

File

themes/socialbase/socialbase.theme
View source
<?php

/**
 * @file
 * The primary PHP file for the Social base theme.
 */
use Drupal\comment\Entity\Comment;
use Drupal\Component\Utility\Html as HtmlUtility;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\node\Entity\Node;
use Drupal\Core\Language\LanguageInterface;

// Include all files from the includes directory.
$includes_path = dirname(__FILE__) . '/includes/*.inc';
foreach (glob($includes_path) as $filename) {
  require_once dirname(__FILE__) . '/includes/' . basename($filename);
}

/**
 * Implements hook_library_info_alter().
 */
function socialbase_library_info_alter(&$libraries, $extension) {

  // When the social_comment_upload module is enabled we want to load the
  // photoswipe styling with the comment styling.
  if ($extension === 'socialbase' && isset($libraries['comment']) && \Drupal::moduleHandler()
    ->moduleExists('social_comment_upload')) {
    $libraries['comment']['dependencies'][] = 'socialbase/photoswipe-gallery';
  }
}

/**
 * Prepare group link when an event or topic belongs to one group.
 */
function socialbase_group_link($node) {
  $group = _social_group_get_current_group($node);
  $group_link = NULL;

  // Exclude nodes without ids (Preview).
  if (!empty($node
    ->id()) && !empty($group)) {
    $group_content = \Drupal::entityTypeManager()
      ->getStorage('group_content')
      ->loadByProperties([
      'entity_id' => $node
        ->id(),
    ]);
    if (!empty($group_content)) {
      $curr_langcode = \Drupal::languageManager()
        ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
        ->getId();
      if ($group
        ->isTranslatable() && $group
        ->hasTranslation($curr_langcode)) {
        $group = $group
          ->getTranslation($curr_langcode);
      }
      $group_link = $group
        ->link();
    }
  }
  return $group_link;
}

/**
 * Helper function to retrieve the icon string for a visibility title.
 *
 * @param string $title
 *   The title for the icon.
 *
 * @return string
 *   The icon connected to the title.
 */
function _socialbase_get_visibility_icon($title) {

  // Set the materialize icon.
  switch ($title) {
    case 'Community':
      $icon = 'community';
      break;
    case 'Recipient':
      $icon = 'community';
      break;
    case 'Group members':
      $icon = 'lock';
      break;
    default:
      $icon = strtolower(HtmlUtility::escape($title));
  }
  return $icon;
}

/**
 * Get comment count for a node.
 */
function _socialbase_node_get_comment_count(Node $node, $comment_field_name) {
  $count = 0;
  $cids = \Drupal::entityQuery('comment')
    ->condition('entity_id', $node
    ->id())
    ->condition('entity_type', 'node')
    ->sort('cid', 'DESC')
    ->execute();
  $comments = Comment::loadMultiple($cids);
  foreach ($comments as $comment) {

    /* @var \Drupal\comment\Entity\Comment $comment */
    if ($comment
      ->isPublished()) {

      // Published main comments or published replies on published main comments
      // are counted for users without 'administer comments' permission.
      if (!$comment
        ->hasParentComment() || $comment
        ->hasParentComment() && $comment
        ->getParentComment()
        ->isPublished()) {
        $count++;
      }
      elseif (\Drupal::currentUser()
        ->hasPermission('administer comments')) {

        // User with 'administer comments' permission can also see the comment
        // as being unpublished, so let's count it.
        $count++;
      }
    }
    elseif (\Drupal::currentUser()
      ->hasPermission('administer comments')) {

      // User with 'administer comments' permission can also see the comment
      // as being unpublished, so let's count it.
      $count++;
    }
  }
  return $count;
}

/**
 * Get like count for a node.
 */
function _socialbase_node_get_like_count($type, $id) {
  $count = 0;

  // The result function service needs entity type and entity id in order
  // to get proper results.
  if (!empty($type) && !empty($id)) {
    $manager = Drupal::service('plugin.manager.votingapi.resultfunction');
    $results = $manager
      ->getResults($type, $id);

    // Lets see if our results carry the sum of all votes.
    if (!empty($results['like']['vote_sum'])) {
      $count = $results['like']['vote_sum'];
    }
  }
  return $count;
}

/**
 * Implements theme_preprocess_username().
 */
function socialbase_preprocess_username(&$variables) {
  $account = $variables['account'] ?: new AnonymousUserSession();

  // Override the default drupal truncate function for all user names,
  // so the whole name will be displayed.
  $variables['name'] = $account
    ->getDisplayName();
}

Functions

Namesort descending Description
socialbase_group_link Prepare group link when an event or topic belongs to one group.
socialbase_library_info_alter Implements hook_library_info_alter().
socialbase_preprocess_username Implements theme_preprocess_username().
_socialbase_get_visibility_icon Helper function to retrieve the icon string for a visibility title.
_socialbase_node_get_comment_count Get comment count for a node.
_socialbase_node_get_like_count Get like count for a node.