You are here

function social_core_preprocess_node in Open Social 10.2.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_core/social_core.module \social_core_preprocess_node()
  2. 8.6 modules/social_features/social_core/social_core.module \social_core_preprocess_node()
  3. 8.7 modules/social_features/social_core/social_core.module \social_core_preprocess_node()
  4. 8.8 modules/social_features/social_core/social_core.module \social_core_preprocess_node()
  5. 10.3.x modules/social_features/social_core/social_core.module \social_core_preprocess_node()
  6. 10.0.x modules/social_features/social_core/social_core.module \social_core_preprocess_node()
  7. 10.1.x modules/social_features/social_core/social_core.module \social_core_preprocess_node()

Implements hook_preprocess_HOOK().

File

modules/social_features/social_core/social_core.module, line 119
The Social core module.

Code

function social_core_preprocess_node(&$variables) {

  /** @var \Drupal\node\NodeInterface $node */
  $node = $variables['node'];

  // Add common variable for teaser images.
  $image_field = "field_{$node->getType()}_image";

  // If machine name too long or using another image field.
  if (!$node
    ->hasField($image_field)) {
    $node_fields = $node
      ->getFields();
    $image_fields = array_filter($node_fields, '_social_core_find_image_field');

    // Get the first image field of all the fields.
    if ($image_fields) {
      $image_field = reset($image_fields)
        ->getName();
    }
    else {
      $image_field = NULL;
    }
  }
  if (!empty($variables['content'][$image_field]['#theme'])) {
    $variables['node_image'] = $variables['content'][$image_field];
    $variables['no_image'] = FALSE;
  }
  else {
    $variables['no_image'] = TRUE;
  }
  if ($variables['view_mode'] === 'hero') {

    // Add node edit url for management.
    // Get the current route name to check if the user is on the
    // edit or delete page.
    $route = \Drupal::routeMatch()
      ->getRouteName();
    if (!in_array($route, [
      'entity.node.edit_form',
      'entity.node.delete_form',
    ])) {
      $account = \Drupal::currentUser();
      if ($node
        ->access('update', $account)) {
        $variables['node_edit_url'] = $node
          ->toUrl('edit-form')
          ->toString();

        // Ensure the cache varies correctly depending upon access of the user.
        $variables['#cache']['contexts'][] = 'user.node_grants';
      }
    }

    // Add the hero styled image.
    if ($node
      ->hasField($image_field) && !empty($node->{$image_field}->entity)) {
      $variables['hero_styled_image_url'] = ImageStyle::load('social_xx_large')
        ->buildUrl($node->{$image_field}->entity
        ->getFileUri());
    }
  }
}