You are here

function socialbase_preprocess_form in Open Social 8.5

Same name and namespace in other branches
  1. 8.9 themes/socialbase/includes/form.inc \socialbase_preprocess_form()
  2. 8 themes/socialbase/includes/form.inc \socialbase_preprocess_form()
  3. 8.2 themes/socialbase/includes/form.inc \socialbase_preprocess_form()
  4. 8.3 themes/socialbase/includes/form.inc \socialbase_preprocess_form()
  5. 8.4 themes/socialbase/includes/form.inc \socialbase_preprocess_form()
  6. 8.6 themes/socialbase/includes/form.inc \socialbase_preprocess_form()
  7. 8.7 themes/socialbase/includes/form.inc \socialbase_preprocess_form()
  8. 8.8 themes/socialbase/includes/form.inc \socialbase_preprocess_form()

Implements template_preprocess_form().

File

themes/socialbase/includes/form.inc, line 15
The form inc file for the Social base theme.

Code

function socialbase_preprocess_form(&$variables) {
  $element = $variables['element'];

  // If this is a search content form set a variable for twig.
  if ($element['#form_id'] === 'search_content_form' || $element['#form_id'] === 'search_hero_form' || $element['#form_id'] === 'geolocation_search_form' || $element['#form_id'] === 'geolocation_search_content_form') {
    $variables['is_search_form'] = TRUE;
  }

  // We have set the region via propress block, now pass this variable on
  // to be used in twig. In twig we can distinguish the nav bar form from
  // the hero form as they both have the same ID's.
  if (isset($element['#region'])) {
    if ($element['#region'] === 'hero') {
      $variables['in_hero_region'] = TRUE;
    }
    if ($element['#region'] === 'content-top') {
      $variables['in_content_top_region'] = TRUE;
    }
  }
  $variables['attributes']['class'][] = 'clearfix';
  if ($element['#form_id'] === 'comment_comment_form' || $element['#form_id'] === 'comment_post_comment_form' || $element['#form_id'] === 'private_message_add_form') {
    $current_user = \Drupal::currentUser();
    if ($current_user) {
      $storage = \Drupal::entityTypeManager()
        ->getStorage('profile');
      if (!empty($storage)) {
        $user_profile = $storage
          ->loadByUser($current_user, 'profile');
        if ($user_profile) {
          $content = \Drupal::entityTypeManager()
            ->getViewBuilder('profile')
            ->view($user_profile, 'compact');
          $variables['current_user_picture'] = $content;
        }
      }
    }

    // Comment edit form.
    if (\Drupal::routeMatch()
      ->getRouteName() === 'entity.comment.edit_form') {
      $comment = \Drupal::routeMatch()
        ->getParameter('comment');
      if (is_object($comment)) {

        // Display comment created date in format 'time ago'.
        $created_time_ago = \Drupal::service('date.formatter')
          ->formatTimeDiffSince($comment
          ->getCreatedTime(), [
          'granularity' => 1,
          'return_as_object' => TRUE,
        ]);
        $submitted = t('@time ago', [
          '@time' => $created_time_ago
            ->getString(),
        ]);
        $variables['submitted'] = Link::fromTextAndUrl($submitted, $comment
          ->urlInfo('canonical'));
        $variables['#cache']['max-age'] = $created_time_ago
          ->getMaxAge();

        // Display author information.
        $account = $comment
          ->getOwner();
        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 ($element['#form_id'] === 'social_post_entity_form') {
    if (\Drupal::routeMatch()
      ->getRouteName() === 'entity.post.edit_form') {
      $post_id = $element['#post_id'];

      /** @var \Drupal\social_post\Entity\Post $post */
      $post = entity_load('post', $post_id);

      // 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']['#markup'] = $date;
      $variables['#cache']['max-age'] = $created_time_ago
        ->getMaxAge();

      // 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) {
        $author_name = $account
          ->getDisplayName();
        $variables['author_name']['#markup'] = $author_name;
        $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;
          }
        }
      }
    }
  }
}