You are here

class Node in Open Social 8

Same name and namespace in other branches
  1. 8.9 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node
  2. 8.2 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node
  3. 8.3 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node
  4. 8.4 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node
  5. 8.5 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node
  6. 8.6 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node
  7. 8.7 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node
  8. 8.8 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node

Pre-processes variables for the "node" theme hook.

Plugin annotation

@BootstrapPreprocess("node");

Hierarchy

Expanded class hierarchy of Node

File

themes/socialbase/src/Plugin/Preprocess/Node.php, line 18

Namespace

Drupal\socialbase\Plugin\Preprocess
View source
class Node extends PreprocessBase {

  /**
   * {@inheritdoc}
   */
  protected function preprocessElement(Element $element, Variables $variables) {
    $node = $variables['node'];
    $account = $node
      ->getOwner();
    $variables['content_type'] = $node
      ->bundle();

    // We get the group link to the node if there is one,
    // will return NULL if not.
    $group_link = socialbase_group_link($node);
    if (!empty($group_link)) {
      $variables['group_link'] = $group_link;
    }

    // Display author information.
    if ($account) {

      // Author profile picture.
      $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;
        }
      }

      // Author name.
      $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');
    }

    // Get current node.
    $node = $variables['node'];

    // Get current user.
    $currentuser = \Drupal::currentUser();

    // Only add submitted data on teasers since we have the page hero block.
    if ($variables['view_mode'] === 'teaser') {

      // Not for AN..
      $is_anonymous = \Drupal::currentUser()
        ->isAnonymous();
      if (!$is_anonymous && $variables['node']
        ->id()) {

        // Only on Events & Topics.
        if ($variables['node']
          ->getType() == 'event' || $variables['node']
          ->getType() == 'topic') {

          // Add group name to the teaser (if it's part of a group).
          $group_content = GroupContent::loadByEntity($variables['node']);
          if (!empty($group_content)) {

            // It can only exist in one group.
            // So we get the first pointer out of
            // the array that gets returned from loading GroupContent.
            $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 formats.
    $date = $variables['node']
      ->getCreatedTime();
    if ($variables['view_mode'] === 'small_teaser') {
      $variables['date'] = \Drupal::service('date.formatter')
        ->format($date, 'social_short_date');
    }

    // Teasers and activity stream.
    $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');
    }

    // Content visibility.
    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');
    }

    // Let's see if we can remove comments from the content and render them in a
    // separate content_below array.
    $comment_field_name = '';
    $variables['comment_field_name'] = '';

    // Check on our node if we have the comment type field somewhere.
    $fields_on_node = $node
      ->getFieldDefinitions();
    foreach ($fields_on_node as $field) {
      if ($field
        ->getType() == 'comment') {
        $comment_field_name = $field
          ->getName();
      }
    }

    // Our node has a comment reference. Let's remove it from content array.
    $variables['below_content'] = [];
    if (!empty($comment_field_name)) {
      if (!empty($variables['content'][$comment_field_name])) {

        // Add it to our custom comments_section for the template purposes and
        // remove it.
        $variables['below_content'][$comment_field_name] = $variables['content'][$comment_field_name];
        unset($variables['content'][$comment_field_name]);
      }

      // If we have a comment and the status is
      // OPEN or CLOSED we can render icon for
      // comment count, and add the comment count to the node.
      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 it's closed, we only show the comment section when there are
        // comments placed. Closed means we show comments but you are not able
        // to add any comments.
        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;
        }
      }
    }

    // If we have the like and dislike widget available
    // for this node, we can print the count even for Anonymous.
    $enabled_types = \Drupal::config('like_and_dislike.settings')
      ->get('enabled_types');
    $variables['likes_count'] = NULL;
    if (in_array($variables['node']
      ->getType(), $enabled_types['node'])) {
      $variables['likes_count'] = _socialbase_node_get_like_count($variables['node']
        ->getEntityTypeId(), $variables['node']
        ->id());
    }

    // Add styles for nodes in preview.
    if ($node->in_preview) {
      $variables['#attached']['library'][] = 'socialbase/preview';
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
Node::preprocessElement protected function Preprocess the variables array if an element is present. Overrides PreprocessBase::preprocessElement
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::$theme protected property The currently set theme object.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
PreprocessBase::$hook protected property The theme hook invoked.
PreprocessBase::$info protected property The theme hook info array from the theme registry.
PreprocessBase::$variables protected property The Variables object.
PreprocessBase::preprocess public function Preprocess theme hook variables. Overrides PreprocessInterface::preprocess
PreprocessBase::preprocessAttributes protected function Ensures all attributes have been converted to an Attribute object.
PreprocessBase::preprocessDescription protected function Converts any set description variable into a traversable array.
PreprocessBase::preprocessVariables protected function Preprocess the variables array. 15
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.