Node.php in Open Social 8.4        
                          
                  
                        
  
  
  
  
File
  themes/socialbase/src/Plugin/Preprocess/Node.php
  
    View source  
  <?php
namespace Drupal\socialbase\Plugin\Preprocess;
use Drupal\bootstrap\Plugin\Preprocess\PreprocessBase;
use Drupal\bootstrap\Utility\Element;
use Drupal\bootstrap\Utility\Variables;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\group\Entity\GroupContent;
class Node extends PreprocessBase {
  
  protected function preprocessElement(Element $element, Variables $variables) {
    
    $node = $variables['node'];
    $account = $node
      ->getOwner();
    $variables['content_type'] = $node
      ->bundle();
    
    $group_link = socialbase_group_link($node);
    if (!empty($group_link)) {
      $variables['group_link'] = $group_link;
    }
    
    if ($account) {
      
      $storage = \Drupal::entityTypeManager()
        ->getStorage('profile');
      if (!empty($storage)) {
        $user_profile = $storage
          ->loadByUser($account, 'profile');
        if ($user_profile) {
          $content = \Drupal::entityTypeManager()
            ->getViewBuilder('profile')
            ->view($user_profile, 'compact');
          $variables['author_picture'] = $content;
        }
      }
      
      $username = [
        '#theme' => 'username',
        '#account' => $account,
      ];
      $variables['author'] = drupal_render($username);
    }
    if (isset($variables['elements']['#node']) && !isset($variables['created_date_formatted'])) {
      $variables['created_date_formatted'] = \Drupal::service('date.formatter')
        ->format($variables['elements']['#node']
        ->getCreatedTime(), 'social_long_date');
    }
    
    $node = $variables['node'];
    
    $currentuser = \Drupal::currentUser();
    
    if ($variables['view_mode'] === 'teaser') {
      
      $is_anonymous = \Drupal::currentUser()
        ->isAnonymous();
      if (!$is_anonymous && $variables['node']
        ->id()) {
        
        if ($variables['node']
          ->getType() == 'event' || $variables['node']
          ->getType() == 'topic') {
          
          $group_content = GroupContent::loadByEntity($variables['node']);
          if (!empty($group_content)) {
            
            $group = reset($group_content)
              ->getGroup();
            if (!empty($group)) {
              $variables['content']['group_name'] = $group
                ->label();
            }
          }
        }
      }
      $variables['display_submitted'] = TRUE;
    }
    if ($variables['view_mode'] === 'hero') {
      unset($variables['label']);
    }
    
    $date = $variables['node']
      ->getCreatedTime();
    if ($variables['view_mode'] === 'small_teaser') {
      $variables['date'] = \Drupal::service('date.formatter')
        ->format($date, 'social_short_date');
    }
    
    $teaser_view_modes = [
      'teaser',
      'activity',
      'activity_comment',
      'featured',
    ];
    if (in_array($variables['view_mode'], $teaser_view_modes)) {
      $variables['date'] = \Drupal::service('date.formatter')
        ->format($date, 'social_medium_date');
    }
    
    if (isset($node->field_content_visibility) && !$currentuser
      ->isAnonymous()) {
      $node_visibility_value = $node->field_content_visibility
        ->getValue();
      $content_visibility = reset($node_visibility_value);
      switch ($content_visibility['value']) {
        case 'community':
          $variables['visibility_icon'] = 'community';
          $variables['visibility_label'] = t('community');
          break;
        case 'public':
          $variables['visibility_icon'] = 'public';
          $variables['visibility_label'] = t('public');
          break;
        case 'group':
          $variables['visibility_icon'] = 'lock';
          $variables['visibility_label'] = t('group');
          break;
      }
    }
    if ($node->status->value == NODE_NOT_PUBLISHED) {
      $variables['status_label'] = t('unpublished');
    }
    
    $comment_field_name = '';
    $variables['comment_field_name'] = '';
    
    $fields_on_node = $node
      ->getFieldDefinitions();
    foreach ($fields_on_node as $field) {
      if ($field
        ->getType() == 'comment') {
        $comment_field_name = $field
          ->getName();
      }
    }
    
    $variables['below_content'] = [];
    if (!empty($comment_field_name)) {
      if (!empty($variables['content'][$comment_field_name])) {
        
        $variables['below_content'][$comment_field_name] = $variables['content'][$comment_field_name];
        unset($variables['content'][$comment_field_name]);
      }
      
      if ($node->{$comment_field_name}->status != CommentItemInterface::HIDDEN) {
        $comment_count = _socialbase_node_get_comment_count($node, $comment_field_name);
        $t_args = [
          ':num_comments' => $comment_count,
        ];
        $variables['below_content'][$comment_field_name]['#title'] = t('Comments (:num_comments)', $t_args);
        
        if ($node->{$comment_field_name}->status == CommentItemInterface::CLOSED && $comment_count > 0 || $node->{$comment_field_name}->status == CommentItemInterface::OPEN) {
          $variables['comment_field_status'] = $comment_field_name;
          $variables['comment_count'] = $comment_count;
        }
      }
    }
    
    $enabled_types = \Drupal::config('like_and_dislike.settings')
      ->get('enabled_types');
    $variables['likes_count'] = NULL;
    if (in_array($node
      ->getType(), $enabled_types['node'])) {
      $variables['likes_count'] = _socialbase_node_get_like_count($node
        ->getEntityTypeId(), $node
        ->id());
    }
    
    if ($node->in_preview) {
      $variables['#attached']['library'][] = 'socialbase/preview';
    }
    
    $variables['no_image'] = TRUE;
    $image_field = "field_{$node->getType()}_image";
    if (!empty($node->{$image_field}->entity)) {
      $variables['no_image'] = FALSE;
    }
    else {
      
      $node_fields = $node
        ->getFields();
      $image_fields = array_filter($node_fields, '_social_core_find_image_field');
      
      $field = reset($image_fields);
      if ($field !== NULL && $field !== FALSE) {
        if ($field
          ->getFieldDefinition()
          ->get("field_type") === 'image') {
          if (!empty($node
            ->get($field
            ->getName())->entity)) {
            $variables['no_image'] = FALSE;
          }
        }
      }
    }
    
    if ($node
      ->getType() === 'landing_page') {
      $variables['no_image'] = FALSE;
      $image = _social_landing_page_get_hero_image($node);
      if (empty($image)) {
        $variables['no_image'] = TRUE;
      }
    }
  }
}
 
Classes
        
  
  
      
      
         
      
                  | Name   | Description | 
    
    
          
                  | Node | Pre-processes variables for the "node" theme hook. |