You are here

socialbase.theme in Open Social 8

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\Component\Utility\Html as HtmlUtility;
use Drupal\bootstrap\Bootstrap;
use Drupal\node\Entity\Node;

// 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_theme_suggestions_alter().
 */
function socialbase_theme_suggestions_alter(&$data, &$context1 = NULL, &$context2 = NULL) {
  Bootstrap::alter(__FUNCTION__, $data, $context1, $context2);
}

/**
 * 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_type_id = $group
      ->getGroupType()
      ->id();
    $group_content = \Drupal::entityTypeManager()
      ->getStorage('group_content')
      ->loadByProperties([
      'entity_id' => $node
        ->id(),
      'type' => $group_type_id . '-group_node-' . $node
        ->getType(),
    ]);
    if (!empty($group_content)) {
      $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;
  $comment_count = $node->{$comment_field_name}->comment_count;
  if ($comment_count) {
    $count = $comment_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_preprocess_username Implements theme_preprocess_username().
socialbase_theme_suggestions_alter Implements hook_theme_suggestions_alter().
_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.