Skip to content
Snippets Groups Projects
UwNodeContent.php 15.58 KiB
<?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).
   *
   * @return array
   *   Array of content to get from the node.
   */
  public function getNodeContent(Node $node, string $view_mode, string $content): array {

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

    // Setup the node data array, based on flags.
    switch ($node->getType()) {

      case 'uw_ct_blog':
        $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);
        break;

      case 'uw_ct_web_page':
        $content_data = $this->getWebPageContent($node_flags);
        break;

      case 'uw_ct_catalog_item':
        $content_data = $this->getCatalogItemContent($node_flags);
        break;

      case 'uw_ct_contact':
        $content_data = $this->getContactContent($node_flags);
        break;

      case 'uw_ct_profile':
        $content_data = $this->getProfileContent($node_flags);
        break;
    }

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

  /**
   * Get the content types that have hero images.
   *
   * @return string[]
   *   Array of content types that can have hero images.
   */
  public function getHeroImageContentTypes() {
    return [
      'uw_ct_blog' => 'field_uw_hero_image',
      'uw_ct_event' => 'field_uw_hero_image',
      'uw_ct_news_item' => 'field_uw_hero_image',
    ];
  }

  /**
   * 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).
   *
   * @return array
   *   Array of flags for the node.
   */
  public function getNodeFlags(Node $node, string $view_mode, string $content): array {

    // Get the content types that have hero images.
    $hero_image = $this->getHeroImageContentTypes();

    // 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'] = FALSE;
    $node_flags['get_hero'] = FALSE;
    $node_flags['get_listing_image'] = FALSE;

    // 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 == 'full') {
        $node_flags['get_title'] = TRUE;
        $node_flags['get_hero'] = TRUE;
      }
      elseif ($view_mode == 'teaser') {
        $node_flags['get_footer'] = FALSE;
        $node_flags['get_listing_image'] = 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;
  }

  /**
   * Common elements for content data.
   *
   * @param array $node_flags
   *   The flags for the node.
   *
   * @return array
   *   Array of common elements for content data.
   */
  public function setupContentData(array $node_flags): array {

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

    $content_data['header'] = [
      'has_children' => TRUE,
    ];

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

    return $content_data;
  }

  /**
   * Functiont to add to content data array.
   *
   * @param string $type
   *   The type of field.
   * @param mixed $field_name
   *   The actual field name(s).
   * @param string $label
   *   The label to be used with the field.
   *
   * @return string[]
   *   Array to add to the content data.
   */
  public function addToContentData(string $type, $field_name, string $label = NULL): array {
    return [
      'type' => $type,
      'field' => $field_name,
      'label' => $label,
    ];
  }

  /**
   * 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 {

    // Get the content data.
    $content_data = $this->setupContentData($node_flags);

    // Setup the header content.
    if ($node_flags['get_header']) {
      $content_data['header']['date'] = $this->addToContentData('date', 'field_uw_blog_date');
      $content_data['header']['author'] = $this->addToContentData('author', 'field_author');
    }

    // Get the hero image.
    if ($node_flags['get_hero']) {
      $content_data['hero_image'] = $this->addToContentData('sources', 'field_uw_hero_image');
    }

    // Get the listing image.
    if ($node_flags['get_listing_image']) {
      $content_data['listing_image'] = $this->addToContentData('sources', 'field_uw_blog_listing_page_image');
    }

    // Setup the actual content.
    if ($node_flags['get_content']) {
      $content_data['content'] = $this->addToContentData('content', 'field_uw_blog_summary');
    }

    // Setup the footer.
    if ($node_flags['get_footer']) {

      $terms = [
        'field_uw_blog_tags',
        'field_uw_audience',
      ];

      $content_data['footer']['tags'] = $this->addToContentData('terms', $terms);
    }

    return $content_data;
  }

  /**
   * 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 {

    // Setup the content data array.
    $content_data = $this->setupContentData($node_flags);

    // Setup the header content.
    if ($node_flags['get_header']) {
      $content_data['header']['date'] = $this->addToContentData('date', 'field_uw_event_date');
    }

    // Get hero image.
    if ($node_flags['get_hero']) {
      $content_data['hero_image'] = $this->addToContentData('sources', 'field_uw_hero_image');
    }

    // Get listing image.
    if ($node_flags['get_listing_image']) {
      $content_data['listing_image'] = $this->addToContentData('sources', 'field_uw_event_listing_page_img');
    }

    // Setup the actual content.
    if ($node_flags['get_content']) {
      $content_data['content'] = $this->addToContentData('content', 'field_uw_event_summary');
    }

    // Setup the footer content.
    if ($node_flags['get_footer']) {

      $content_data['footer']['additional_info'] = [
        'has_children' => TRUE,
        'host' => $this->addToContentData('link', 'field_uw_event_host', 'Host'),
        'event_website' => $this->addToContentData('link', 'field_uw_event_website', 'Event website'),
        'cost' => $this->addToContentData('plain_text', 'field_uw_event_cost', 'Cost'),
      ];

      $content_data['footer']['location_info'] = [
        'has_children' => TRUE,
        'address' => $this->addToContentData('address', 'field_uw_event_location_address', 'Location address'),
        'map' => $this->addToContentData('map', 'field_uw_event_location_coord', 'Location coordinates'),
      ];

      $content_data['footer']['tags'] = $this->addToContentData(
        'terms',
        [
          '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 {

    // Get the content data.
    $content_data = $this->setupContentData($node_flags);

    // Setup the header content.
    if ($node_flags['get_header']) {
      $content_data['header']['date'] = $this->addToContentData('date', 'field_uw_news_date');
    }

    // Get hero image.
    if ($node_flags['get_hero']) {
      $content_data['hero_image'] = $this->addToContentData('sources', 'field_uw_hero_image');
    }

    // Get listing image.
    if ($node_flags['get_listing_image']) {
      $content_data['listing_image'] = $this->addToContentData('sources', 'field_uw_news_listing_page_image');
    }

    // Setup the actual content.
    if ($node_flags['get_content']) {
      $content_data['content'] = $this->addToContentData('content', 'field_uw_news_summary');
    }

    // Get the footer content.
    if ($node_flags['get_footer']) {
      $content_data['footer']['tags'] = $this->addToContentData(
        'terms',
        [
          'field_uw_news_tags',
          'field_uw_audience',
        ]
      );
    }

    return $content_data;
  }

  /**
   * 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 {

    // Get the content data.
    $content_data = $this->setupContentData($node_flags);

    // Setup the actual content.
    if ($node_flags['get_content']) {
      $content_data['content'] = $this->addToContentData('content', 'layout_builder__layout');
    }

    return $content_data;
  }

  /**
   * 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 {

    // Get the content data.
    $content_data = $this->setupContentData($node_flags);

    // Setup the actual content.
    if ($node_flags['get_content']) {
      $content_data['content'] = $this->addToContentData('content', 'field_uw_news_summary');
    }

    // Get the footer for catalog items.
    if ($node_flags['get_footer']) {

      // Get the catalog terms.
      $content_data['footer']['additional_info']['has_children'] = TRUE;
      $content_data['footer']['additional_info']['tags'] = $this->addToContentData(
        'catalog_terms',
        [
          'Category' => 'field_uw_catalog_category',
          'Faculty' => 'field_uw_catalog_faculty',
          'Audience' => 'field_uw_audience',
        ]
      );
    }

    return $content_data;
  }

  /**
   * 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 {

    // Get the content data.
    $content_data = $this->setupContentData($node_flags);

    // Setup the header content.
    if ($node_flags['get_header']) {
      $content_data['header']['position'] = $this->addToContentData('plain_text', 'field_uw_ct_contact_title');
    }

    // Setup the actual content.
    if ($node_flags['get_content']) {
      $content_data['content'] = $this->addToContentData('content', 'field_uw_news_summary');
    }

    // Get the footer data.
    if ($node_flags['get_footer']) {

      // Get the additional info.
      $content_data['footer']['additional_info']['has_children'] = TRUE;
      $content_data['footer']['additional_info']['info'] = $this->addToContentData('formatted_text', 'field_uw_ct_contact_info');

      // Get the contact information.
      $content_data['footer']['contact_info']['has_children'] = TRUE;
      $content_data['footer']['contact_info']['email'] = $this->addToContentData('plain_text', 'field_uw_ct_contact_email');
      $content_data['footer']['contact_info']['phone'] = $this->addToContentData('plain_text', 'field_uw_ct_contact_phone');
      $content_data['footer']['contact_info']['location'] = $this->addToContentData('plain_text', 'field_uw_ct_contact_location', 'Location');

      // Get the links for the profile.
      $content_data['footer']['links']['has_children'] = TRUE;
      $content_data['footer']['links']['profile'] = $this->addToContentData('link', 'field_uw_ct_contact_link_profile', 'Link to profile');
      $content_data['footer']['links']['webpage'] = $this->addToContentData('link', 'field_uw_ct_contact_link_persona', 'Link to personal webpage');

      // Get the contact for, for the profile.
      $content_data['footer']['contact_for']['has_children'] = TRUE;
      $content_data['footer']['contact_for']['contact'] = $this->addToContentData('plain_text', 'field_uw_ct_contact_contact_for');

      // Get the groups for the profile.
      $content_data['footer']['groups']['has_children'] = TRUE;
      $content_data['footer']['groups']['groups'] = $this->addToContentData('terms', ['field_uw_ct_contact_group']);
    }

    return $content_data;
  }

  /**
   * 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,
    ];
  }

  /**
   * Gets the most recent date.
   *
   * @param array $date
   *   The date.
   * @param string $type
   *   The type of date.
   *
   * @return string
   *   The actual date with proper format.
   */
  public function getDate(array $date, string $type): string {

    // The all day case, duration is always 1439.
    if ($date['duration'] == '1439') {
      $return_date = date('l, F j, Y', $date['value']) . ' (all day)';
    }
    else {

      // If this is the same day, get the date and the start
      // and end times.
      if ($date['duration'] < '1439') {
        $start_date = date('l, F j, Y g:i A', $date['value']);
        $end_date = date('g:i A', $date['end_value']);
      }
      // This is not the day, get the start and end date with time.
      else {
        $start_date = date('l, F j, Y g:i A', $date['value']);
        $end_date = date('l, F j, Y g:i A', $date['end_value']);
      }

      // Add the start and end date with timezone.
      $return_date = $start_date . ' - ' . $end_date . ' ' . date('T', $date['end_value']);
    }

    return $return_date;
  }

}