Skip to content
Snippets Groups Projects
UwNodeContent.php 10.9 KiB
Newer Older
<?php

namespace Drupal\uw_cfg_common\Service;

use Drupal\node\Entity\Node;

/**
 * Class UwNodeContent.
 *
 * Gets the content out of a node.
 *
 * @package Drupal\uw_cfg_common\Service
 */
class UwNodeContent {

  /**
   * The UW node data service.
   *
   * @var UwNodeData
   */
  private $uwNodeData;

  /**
   * Default constructor.
   *
   * @param UwNodeData $uwNodeData
   *   The UW node data service.
   */
  public function __construct(UwNodeData $uwNodeData) {
    $this->uwNodeData = $uwNodeData;
  }

  /**
   * Gets the content of a node.
   *
   * @param \Drupal\node\Entity\Node $node
   *   The node.
   * @param string $view_mode
   *   The view mode.
   * @param string $content
   *   The content to get (either layout builder or summary)
   * @param array $featured_image
   *   Whether this node has a featured image or not.
   *
   * @return array
   *   Array of content to get from the node.
   */
  public function getNodeContent(Node $node, string $view_mode, string $content, array $featured_image): array {

    // Get the flags for the node.
    $node_flags = $this->getNodeFlags($node, $view_mode, $content, $featured_image);

    // Setup the node data array, based on flags.
        $content_data = $this->getBlogContent($node_flags);
        break;

      case 'uw_ct_event':
        $content_data = $this->getEventContent($node_flags);
        break;

      case 'uw_ct_news_item':
        $content_data = $this->getNewsContent($node_flags);
        $content_data = $this->getWebPageContent($node_flags);
        $content_data = $this->getCatalogItemContent($node_flags);
        $content_data = $this->getContactContent($node_flags);
        $content_data = $this->getProfileContent($node_flags);
        break;
    }

    return $this->uwNodeData->getNodeData($node, $view_mode, $content_data);
  }

  /**
   * Get the flags for the node.
   *
   * @param \Drupal\node\Entity\Node $node
   *   The node.
   * @param string $view_mode
   *   The view mode.
   * @param string $content
   *   The content to get (layout builder or summary).
   * @param array $featured_image
   *   Whether this node has a featured image.
   *
   * @return array
   *   Array of flags for the node.
   */
  public function getNodeFlags(Node $node, string $view_mode, string $content, array $featured_image): array {

    // Flags for getting teaser content.
    $node_flags['get_header'] = FALSE;
    $node_flags['get_footer'] = FALSE;
    $node_flags['get_image'] = FALSE;
    $node_flags['get_content'] = FALSE;
    $node_flags['get_title'] = TRUE;

    // Setup flags based on teaser content argument.
    if ($content == 'all') {
      $node_flags['get_header'] = TRUE;
      $node_flags['get_footer'] = TRUE;
      $node_flags['get_content'] = TRUE;

      if ($view_mode == 'teaser') {
        $node_flags['get_image'] = TRUE;
      }

      if ($view_mode == 'full') {
        $node_flags['get_title'] = FALSE;

        if (in_array($node->getType(), array_keys($featured_image))) {
          $node_flags['get_image'] = TRUE;
          $node_flags['get_title'] = TRUE;
        }
      }
    }
    else {
      if ($content == 'header') {
        $node_flags['get_header'] = TRUE;
        $node_flags['get_image'] = TRUE;
      }

      if ($content == 'footer') {
        $node_flags['get_footer'] = TRUE;
        $node_flags['get_title'] = FALSE;
      }
    }

    return $node_flags;
  }

  /**
   * Get the node content for blog content type.
   *
   * @param array $node_flags
   *   The flags for the node.
   *
   * @return array
   *   Array of content to get from the node.
   */
  public function getBlogContent(array $node_flags): array {

    // The list of tags for blogs.
    $tag_list = [
      'field_uw_blog_tags',
      'field_uw_audience',
    ];

    return [
      'title' => $node_flags['get_title'] ? TRUE : NULL,
      'url' => TRUE,
      'date' => $node_flags['get_header'] ? 'field_uw_blog_date' : NULL,
      'author' => $node_flags['get_header'] ? TRUE : NULL,
      'sources' => $node_flags['get_image'] ? 'field_uw_blog_listing_page_image' : NULL,
      'hero' => $node_flags['get_image'] ? 'field_uw_hero_image' : NULL,
      'content' => $node_flags['get_content'] ? 'field_uw_blog_summary' : NULL,
      'tags' => $node_flags['get_footer'] ? $tag_list : NULL,
    ];
  }

  /**
   * Get the node content for event content type.
   *
   * @param array $node_flags
   *   The flags for the node.
   *
   * @return array
   *   Array of content to get from the node.
   */
  public function getEventContent(array $node_flags): array {

    $content_data['url'] = [
      'type' => 'url',
    ];

    if ($node_flags['get_title']) {
      $content_data['title'] = [
        'type' => 'title',
      ];
    }

    if ($node_flags['get_header']) {
      $content_data['date'] = [
        'type' => 'date',
        'field' => 'field_uw_event_date',
      ];
    }

    if ($node_flags['get_image']) {
      $content_data['sources'] = [
        'type' => 'sources',
        'field' => 'field_uw_event_listing_page_img',
      ];
    }

    if ($node_flags['get_content']) {
      $content_data['content'] = [
        'type' => 'content',
        'field' => 'field_uw_event_summary',
      ];
    }

    if ($node_flags['get_footer']) {
      $content_data['additional_info'] = [
        'has_children' => TRUE,
        'host' => [
          'type' => 'link',
          'field' => 'field_uw_event_host',
          'label' => 'Host',
        ],
        'event_website' => [
          'type' => 'link',
          'field' => 'field_uw_event_website',
          'label' => 'Event website',
        ],
        'cost' => [
          'type' => 'plain_text',
          'field' => 'field_uw_event_cost',
          'label' => 'Cost',
        ],
      ];

      $content_data['location_info'] = [
        'has_children' => TRUE,
        'address' => [
          'type' => 'address',
          'field' => 'field_uw_event_location_address',
          'name' => 'Location address',
        ],
        'map' => [
          'type' => 'map',
          'field' => 'field_uw_event_location_coord',
          'name' => 'Location coordinates',
        ],
      ];

      $content_data['tags'] = [
        'type' => 'terms',
        'field' => [
          'field_uw_event_tags',
          'field_uw_audience',
          'field_uw_event_type',
        ],
      ];
    }

    return $content_data;
  }

  /**
   * Get the node content for news content type.
   *
   * @param array $node_flags
   *   The flags for the node.
   *
   * @return array
   *   Array of content to get from the node.
   */
  public function getNewsContent(array $node_flags): array {

    // The list of tags for news.
    $tag_list = [
      'field_uw_news_tags',
      'field_uw_audience',
    ];

    return [
      'title' => $node_flags['get_title'] ? TRUE : NULL,
      'url' => TRUE,
      'date' => $node_flags['get_header'] ? 'field_uw_news_date' : NULL,
      'sources' => $node_flags['get_image'] ? 'field_uw_news_listing_page_image' : NULL,
      'hero' => $node_flags['get_image'] ? 'field_uw_hero_image' : NULL,
      'content' => $node_flags['get_content'] ? 'field_uw_news_summary' : NULL,
      'tags' => $node_flags['get_footer'] ? $tag_list : NULL,
    ];
  }

  /**
   * Get the node content for web page content type.
   *
   * @param array $node_flags
   *   The flags for the node.
   *
   * @return array
   *   Array of content to get from the node.
   */
  public function getWebPageContent(array $node_flags): array {

    return [
      'title' => $node_flags['get_title'] ? TRUE : NULL,
      'content' => $node_flags['get_content'] ? 'layout_builder__layout' : NULL,
    ];
  }

  /**
   * Get the node content for catalog item content type.
   *
   * @param array $node_flags
   *   The flags for the node.
   *
   * @return array
   *   Array of content to get from the node.
   */
  public function getCatalogItemContent(array $node_flags): array {

    $tags = [
      'Category' => 'field_uw_catalog_category',
      'Faculty' => 'field_uw_catalog_faculty',
      'Audience' => 'field_uw_audience',
    ];

    return [
      'title' => $node_flags['get_title'] ? TRUE : NULL,
      'content' => $node_flags['get_content'] ? 'layout_builder__layout' : NULL,
      'catalog_tags' => $node_flags['get_footer'] ? $tags : NULL,
    ];
  }

  /**
   * Get the node content for catalog item content type.
   *
   * @param array $node_flags
   *   The flags for the node.
   *
   * @return array
   *   Array of content to get from the node.
   */
  public function getContactContent(array $node_flags): array {

    return [
      'title' => $node_flags['get_title'] ? TRUE : NULL,
      'position' => $node_flags['get_header'] ? 'field_uw_ct_contact_title' : NULL,
      'affiliation' => $node_flags['get_header'] ? 'field_uw_ct_contact_affiliation' : NULL,
      'image' => $node_flags['get_image'] ? 'field_uw_ct_contact_image' : NULL,
      'content' => $node_flags['get_content'] ? 'layout_builder__layout' : NULL,
      'email' => $node_flags['get_footer'] ? 'field_uw_ct_contact_email' : NULL,
      'location' => $node_flags['get_footer'] ? 'field_uw_ct_contact_location' : NULL,
      'phone' => $node_flags['get_footer'] ? 'field_uw_ct_contact_phone' : NULL,
      'additional_info' => $node_flags['get_footer'] ? 'field_uw_ct_contact_info' : NULL,
      'link_profile' => $node_flags['get_footer'] ? 'field_uw_ct_contact_link_profile' : NULL,
      'personal_webpage' => $node_flags['get_footer'] ? 'field_uw_ct_contact_link_persona' : NULL,
      'contact_for' => $node_flags['get_footer'] ? 'field_uw_ct_contact_contact_for' : NULL,
      'groups' => $node_flags['get_footer'] ? 'field_uw_ct_contact_group' : NULL,
      'url' => TRUE,
    ];
  }

  /**
   * Get the node content for profile content type.
   *
   * @param array $node_flags
   *   The flags for the node.
   *
   * @return array
   *   Array of content to get from the node.
   */
  public function getProfileContent(array $node_flags): array {

    $tag_list = [
      'field_uw_ct_profile_type',
    ];

    return [
      'title' => $node_flags['get_title'] ? TRUE : NULL,
      'position' => $node_flags['get_header'] ? 'field_uw_ct_profile_title' : NULL,
      'affiliation' => $node_flags['get_header'] ? 'field_uw_ct_profile_affiliation' : NULL,
      'content' => $node_flags['get_content'] ? 'field_uw_profile_summary' : NULL,
      'image' => $node_flags['get_header'] ? 'field_uw_ct_profile_image' : NULL,
      'sources' => $node_flags['get_image'] ? 'field_uw_ct_profile_image' : NULL,
      'tags' => $node_flags['get_footer'] ? $tag_list : NULL,
      'link_profile' => $node_flags['get_footer'] ? 'field_uw_ct_profile_info_link' : NULL,
      'personal_webpage' => $node_flags['get_footer'] ? 'field_uw_ct_profile_link_persona' : NULL,
      'url' => TRUE,
    ];
  }