function template_preprocess_activity in Open Social 8
Same name and namespace in other branches
- 8.9 modules/custom/activity_creator/activity.page.inc \template_preprocess_activity()
- 8.2 modules/custom/activity_creator/activity.page.inc \template_preprocess_activity()
- 8.3 modules/custom/activity_creator/activity.page.inc \template_preprocess_activity()
- 8.4 modules/custom/activity_creator/activity.page.inc \template_preprocess_activity()
- 8.5 modules/custom/activity_creator/activity.page.inc \template_preprocess_activity()
- 8.6 modules/custom/activity_creator/activity.page.inc \template_preprocess_activity()
- 8.7 modules/custom/activity_creator/activity.page.inc \template_preprocess_activity()
- 8.8 modules/custom/activity_creator/activity.page.inc \template_preprocess_activity()
- 10.3.x modules/custom/activity_creator/activity.page.inc \template_preprocess_activity()
- 10.0.x modules/custom/activity_creator/activity.page.inc \template_preprocess_activity()
- 10.1.x modules/custom/activity_creator/activity.page.inc \template_preprocess_activity()
- 10.2.x modules/custom/activity_creator/activity.page.inc \template_preprocess_activity()
Prepares variables for Activity templates.
Default template: activity.html.twig.
Parameters
array $variables: An associative array containing:
- elements: An associative array containing the user information and any
- attributes: HTML attributes for the containing element.
File
- modules/
custom/ activity_creator/ activity.page.inc, line 23 - Contains activity.page.inc..
Code
function template_preprocess_activity(array &$variables) {
// Fetch Activity Entity Object.
/** @var \Drupal\activity_creator\Entity\Activity $activity */
$activity = $variables['elements']['#activity'];
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
// Get the url to the related entity.
$full_url = $activity
->getRelatedEntityUrl();
// Display activity created date in format 'time ago'.
$created_time_ago = \Drupal::service('date.formatter')
->formatTimeDiffSince($activity
->getCreatedTime(), [
'granularity' => 1,
'return_as_object' => TRUE,
]);
$date = t('@time ago', [
'@time' => $created_time_ago
->getString(),
]);
if (isset($variables['elements']['#view_mode']) && in_array($variables['content']['field_activity_output_text']['#view_mode'], [
'notification',
'notification_archive',
])) {
$variables['date'] = $date;
}
else {
$variables['date'] = Link::fromTextAndUrl($date, $full_url)
->toRenderable();
}
$variables['#cache']['max-age'] = $created_time_ago
->getMaxAge();
// To change user picture settings (e.g. image style), edit the
// 'compact_notification' view mode on the User entity. Note that the
// 'compact_notification' view mode might not be configured, so remember to
// always check the theme setting first.
$account = $activity
->getOwner();
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_notification');
if ($full_url == '') {
$variables['actor'] = $content;
}
else {
$variables['actor'] = Link::fromTextAndUrl($content, $account
->toUrl());
}
}
}
}
$variables['full_url'] = $full_url;
$status = $activity
->get('field_activity_status')
->getValue();
switch ($status['0']['value']) {
case ACTIVITY_STATUS_READ:
$status_class = 'bg-white';
break;
default:
$status_class = 'bg-gray-lightest';
break;
}
if (isset($status_class)) {
$variables['status_class'] = $status_class;
}
}