You are here

function template_preprocess_page_hero_data in Open Social 8

Same name and namespace in other branches
  1. 8.2 modules/social_features/social_core/social_core.module \template_preprocess_page_hero_data()
  2. 8.3 modules/social_features/social_core/social_core.module \template_preprocess_page_hero_data()
  3. 8.4 modules/social_features/social_core/social_core.module \template_preprocess_page_hero_data()
  4. 8.5 modules/social_features/social_core/social_core.module \template_preprocess_page_hero_data()

Prepares variables for the social page hero data.

Default template: page-hero-data.html.twig.

Parameters

array $variables: An associative array containing:

  • title: Page title as a string
  • author_name: Author as a string
  • created_date: Timestamp
  • created_date_formatted: Formatted date as a string
  • topic_type: List of topic types as an array or NULL
  • hero_node: Rendered hero display of the current active node or NULL.

File

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

Code

function template_preprocess_page_hero_data(array &$variables) {

  // Get current user.
  $account = \Drupal::currentUser();

  // Get current node object or node id.
  $node = \Drupal::routeMatch()
    ->getParameter('node');
  if (!is_object($node) && !is_null($node)) {
    $node = \Drupal::service('entity_type.manager')
      ->getStorage('node')
      ->load($node);
  }
  if (is_object($node)) {
    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 Node) {

      // 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());
          }
        }
      }
    }
  }
}