You are here

function _social_landing_page_get_hero_image in Open Social 8.7

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_landing_page/social_landing_page.module \_social_landing_page_get_hero_image()
  2. 8.2 modules/social_features/social_landing_page/social_landing_page.module \_social_landing_page_get_hero_image()
  3. 8.3 modules/social_features/social_landing_page/social_landing_page.module \_social_landing_page_get_hero_image()
  4. 8.4 modules/social_features/social_landing_page/social_landing_page.module \_social_landing_page_get_hero_image()
  5. 8.5 modules/social_features/social_landing_page/social_landing_page.module \_social_landing_page_get_hero_image()
  6. 8.6 modules/social_features/social_landing_page/social_landing_page.module \_social_landing_page_get_hero_image()
  7. 8.8 modules/social_features/social_landing_page/social_landing_page.module \_social_landing_page_get_hero_image()
  8. 10.3.x modules/social_features/social_landing_page/social_landing_page.module \_social_landing_page_get_hero_image()
  9. 10.0.x modules/social_features/social_landing_page/social_landing_page.module \_social_landing_page_get_hero_image()
  10. 10.1.x modules/social_features/social_landing_page/social_landing_page.module \_social_landing_page_get_hero_image()
  11. 10.2.x modules/social_features/social_landing_page/social_landing_page.module \_social_landing_page_get_hero_image()

Fetches the first available hero section image from a landing page.

Parameters

\Drupal\node\Entity\Node $node: The landing page.

Return value

array Render array of the image with a link.

Throws

\Drupal\Core\Entity\EntityMalformedException

2 calls to _social_landing_page_get_hero_image()
Node::preprocessElement in themes/socialbase/src/Plugin/Preprocess/Node.php
Preprocess the variables array if an element is present.
social_landing_page_preprocess_node in modules/social_features/social_landing_page/social_landing_page.module
Implements hook_preprocess_HOOK().

File

modules/social_features/social_landing_page/social_landing_page.module, line 256
The Social landing page module.

Code

function _social_landing_page_get_hero_image(Node $node) {

  // Must be a valid node.
  if (!$node instanceof Node || $node
    ->getType() !== 'landing_page') {
    return [];
  }

  // Loop over the landing page sections of the landing page.
  foreach ($node
    ->get('field_landing_page_section') as $section) {

    // Get the referenced paragraph.
    $referenced = $section
      ->getValue();
    $paragraph_id = $referenced['target_id'];

    // First paragraph is always of type section.
    $paragraph_section = Paragraph::load($paragraph_id);

    // Get the related paragraph (the one with the actual content)
    $section_id = $paragraph_section
      ->get('field_section_paragraph')->target_id;
    $paragraph_content = Paragraph::load($section_id);

    // Must be of type hero.
    if ($paragraph_content && $paragraph_content
      ->getType() === 'hero') {
      $fid = $paragraph_content
        ->get('field_hero_image')->target_id;
      $file = File::load($fid);

      // Check if it's an existing file.
      if ($file instanceof File) {

        // Build an image render array.
        $image = [
          '#theme' => 'image_style',
          '#style_name' => 'social_featured',
          '#uri' => $file
            ->getFileUri(),
        ];

        // Build a link render array.
        $build = [
          '#title' => render($image),
          '#type' => 'link',
          '#url' => $node
            ->toUrl('canonical'),
        ];
      }

      // We immediately return the 1st found hero.
      return render($build);
    }
  }
  return [];
}