You are here

function social_core_preprocess_node in Open Social 8.6

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_core/social_core.module \social_core_preprocess_node()
  2. 8.7 modules/social_features/social_core/social_core.module \social_core_preprocess_node()
  3. 8.8 modules/social_features/social_core/social_core.module \social_core_preprocess_node()
  4. 10.3.x modules/social_features/social_core/social_core.module \social_core_preprocess_node()
  5. 10.0.x modules/social_features/social_core/social_core.module \social_core_preprocess_node()
  6. 10.1.x modules/social_features/social_core/social_core.module \social_core_preprocess_node()
  7. 10.2.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 42
The Social core module.

Code

function social_core_preprocess_node(&$variables) {

  /** @var \Drupal\node\Entity\NodeInterface $node */
  $node = $variables['node'];
  if ($variables['view_mode'] === 'hero') {

    // Get current user.
    $account = \Drupal::currentUser();
    if (empty($variables['event_enrollment'])) {
      if ($node
        ->getType() == 'event') {
        $form = \Drupal::formBuilder()
          ->getForm('Drupal\\social_event\\Form\\EnrollActionForm');
        $render_array = [
          'enroll_action_form' => $form,
        ];
        $variables['event_enrollment'] = $render_array;
      }
    }

    // Add node edit url for management.
    if ($node instanceof NodeInterface) {

      // 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',
      ])) {
        if ($node
          ->access('update', $account)) {
          $variables['node_edit_url'] = $node
            ->toUrl('edit-form')
            ->toString();
        }
      }
    }

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

      // If machine name too long or using another 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.
      $field = reset($image_fields);
      if ($field !== NULL && $field !== FALSE) {
        if ($field
          ->getFieldDefinition()
          ->get("field_type") === 'image') {
          if (!empty($node
            ->get($field
            ->getName())->entity)) {
            $variables['hero_styled_image_url'] = ImageStyle::load('social_xx_large')
              ->buildUrl($node
              ->get($field
              ->getName())->entity
              ->getFileUri());
          }
        }
      }
    }
  }
}