class Node in Open Social 8
Same name and namespace in other branches
- 8.9 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node
- 8.2 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node
- 8.3 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node
- 8.4 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node
- 8.5 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node
- 8.6 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node
- 8.7 themes/socialbase/src/Plugin/Preprocess/Node.php \Drupal\socialbase\Plugin\Preprocess\Node
- 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\bootstrap\Plugin\PluginBase
- class \Drupal\bootstrap\Plugin\Preprocess\PreprocessBase implements PreprocessInterface
- class \Drupal\socialbase\Plugin\Preprocess\Node
- class \Drupal\bootstrap\Plugin\Preprocess\PreprocessBase implements PreprocessInterface
- class \Drupal\bootstrap\Plugin\PluginBase
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Node
File
- themes/
socialbase/ src/ Plugin/ Preprocess/ Node.php, line 18
Namespace
Drupal\socialbase\Plugin\PreprocessView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
Node:: |
protected | function |
Preprocess the variables array if an element is present. Overrides PreprocessBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
protected | property | The currently set theme object. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
1 |
PreprocessBase:: |
protected | property | The theme hook invoked. | |
PreprocessBase:: |
protected | property | The theme hook info array from the theme registry. | |
PreprocessBase:: |
protected | property | The Variables object. | |
PreprocessBase:: |
public | function |
Preprocess theme hook variables. Overrides PreprocessInterface:: |
|
PreprocessBase:: |
protected | function | Ensures all attributes have been converted to an Attribute object. | |
PreprocessBase:: |
protected | function | Converts any set description variable into a traversable array. | |
PreprocessBase:: |
protected | function | Preprocess the variables array. | 15 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |