You are here

function template_preprocess_post in Open Social 8.3

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_post/post.page.inc \template_preprocess_post()
  2. 8 modules/social_features/social_post/post.page.inc \template_preprocess_post()
  3. 8.2 modules/social_features/social_post/post.page.inc \template_preprocess_post()
  4. 8.4 modules/social_features/social_post/post.page.inc \template_preprocess_post()
  5. 8.5 modules/social_features/social_post/post.page.inc \template_preprocess_post()
  6. 8.6 modules/social_features/social_post/post.page.inc \template_preprocess_post()
  7. 8.7 modules/social_features/social_post/post.page.inc \template_preprocess_post()
  8. 8.8 modules/social_features/social_post/post.page.inc \template_preprocess_post()
  9. 10.3.x modules/social_features/social_post/post.page.inc \template_preprocess_post()
  10. 10.0.x modules/social_features/social_post/post.page.inc \template_preprocess_post()
  11. 10.1.x modules/social_features/social_post/post.page.inc \template_preprocess_post()
  12. 10.2.x modules/social_features/social_post/post.page.inc \template_preprocess_post()

Prepares variables for Post templates.

Default template: post.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/social_features/social_post/post.page.inc, line 23
Contains post.page.inc..

Code

function template_preprocess_post(array &$variables) {

  // Fetch Post Entity Object.
  $post = $variables['elements']['#post'];

  // Helpful $content variable for templates.
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }

  // Display Post created date in format 'time ago'.
  $created_time_ago = \Drupal::service('date.formatter')
    ->formatTimeDiffSince($post
    ->getCreatedTime(), [
    'granularity' => 1,
    'return_as_object' => TRUE,
  ]);
  $date = t('%time ago', [
    '%time' => $created_time_ago
      ->getString(),
  ]);
  $variables['date'] = Link::fromTextAndUrl($date, $post
    ->urlInfo('canonical'));

  // To change user picture settings (e.g. image style), edit the 'compact'
  // view mode on the User entity. Note that the 'compact' view mode might
  // not be configured, so remember to always check the theme setting first.
  $account = $post
    ->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');
        $variables['author_picture'] = $content;
      }
    }
  }

  // Show a post has been edited.
  if ($post
    ->getCreatedTime() != $post
    ->getChangedTime()) {
    $variables['modified'] = TRUE;
  }
  $variables['published'] = $post
    ->isPublished();
}