eternalistic
10/22/2015 - 4:21 AM

template.php

<?php

/**
 * Implements hook_preprocess_HOOK().
 */
function cii_preprocess_node(&$vars, $hook) {
  // Optionally, run node-type-specific preprocess functions, like
  // STARTERKIT_preprocess_node_page() or STARTERKIT_preprocess_node_story().
  $function = __FUNCTION__ . '_' . $vars['node']->type;
  if (function_exists($function)) {
    $function($vars, $hook);
  }

  // Title.
  $title = isset($vars['title']) ? $vars['title'] : NULL;

  // Theme path.
  $theme_path = drupal_get_path('theme', $GLOBALS['theme']);

  // Add a striping class.
  $vars['classes_array'][] = 'node-' . $vars['zebra'];

  // Add $unpublished variable.
  $vars['unpublished'] = (!$vars['status']) ? TRUE : FALSE;

  // Merge first/last class (from cii_preprocess_page) into classes array of current node object.
  $node = $vars['node'];
  if (!empty($node->classes_array)) {
    $vars['classes_array'] = array_merge($vars['classes_array'], $node->classes_array);
  }

  // Custom submitted text.
  $day = format_date($vars['node']->created, 'custom', 'j');
  $month = format_date($vars['node']->created, 'custom', 'F');
  $year = format_date($vars['node']->created, 'custom', 'Y');
  $date_display = format_date($vars['node']->created, 'custom', 'n/j/Y');
  $vars['submitted'] = '<time datetime=' . $year . '-' . $month . '-' . $day . ' pubdate>' . $date_display . '</time>';

  // Date.
  $month = format_date($vars['node']->created, 'custom', 'F');
  $day = format_date($vars['node']->created, 'custom', 'j');
  $year = format_date($vars['node']->created, 'custom', 'Y');

  // Set default for stellar to be null.
  $vars['stellar'] = NULL;

  // If "Read more" link is set.
  if (isset($vars['content']['links']['node']['#links']['node-readmore'])) {
    // Change "Read more" link title.
    $vars['content']['links']['node']['#links']['node-readmore']['title'] = t('Read more &raquo;');
  }

  // If user's block exists then remove link to it.
  if (isset($vars['content']['links']['blog']['#links']['blog_usernames_blog'])) {
    unset($vars['content']['links']['blog']['#links']['blog_usernames_blog']);
  }

  // Node types.
  switch ($node->type) {
    // Type: Homepage Content.
    case 'homepage_content':
      switch ($vars['view_mode']) {
        // Type: Homepage Content - Teaser.
        case 'teaser':
          // Remove title.
          if (isset($vars['title'])) {
            $vars['title'] = NULL;
          }
          // If style is chosen then add it to the node class array.
          if (isset($vars['content']['field_hpc_section_style'][0]['#markup'])) {
            $bg_style = $vars['content']['field_hpc_section_style'][0]['#markup'];
            $vars['classes_array'][] = 'style-' . $bg_style;
            // Apply Stellar to specific styles.
            in_array($bg_style, array('header', 'bg_connected', 'company_logos', 'bg_type_of_help'))
              ? $vars['stellar'] = 'data-stellar-background-ratio=".5"'
              : NULL;
            // Set a new variable to output a rendered view for use by the
            // theme. This is used for specific sections that output views
            // listings of images. This is a way to output a view as part of
            // the node output without using a contrib module like viewreference
            // or insert_view.
            switch ($bg_style) {
              case 'company_logos':
                $vars['content']['hpc_section_view'][0]['#markup'] = views_embed_view('members', 'block_2');
                break;
              case 'faq_link':
                $block = block_load('block', '1'); // Get block by ID.
                $block_render = _block_get_renderable_array(_block_render_blocks(array($block))); // Create render array.
                $vars['content']['hpc_section_view'][0]['#markup'] = drupal_render($block_render);
                break;
              case 'news_announcements':
                $vars['content']['hpc_section_view'][0]['#markup'] = views_embed_view('news_and_announcements', 'block_2');
                break;
              case 'security_experts':
                $vars['content']['hpc_section_view'][0]['#markup'] = views_embed_view('members', 'block_5');
                break;
            }
            // Display this field below the node body.
            $vars['content']['hpc_section_view']['#weight'] = 10;
            // Unset style as not needed now.
            unset($vars['content']['field_hpc_section_style'][0]);
          }
        break;
      }
    break;

    // Type: Announcement and Blog.
    case 'announcement':
    case 'blog':
      switch ($vars['view_mode']) {

        // Type: Announcement and Blog - Teaser.
        case "teaser":

          // TODO:
          // Need a better way to do this so it cuts at last word rather than
          // at the last character.
          if (strlen ($vars['title']) > 75) {
            $vars['title'] = substr($vars['title'], 0, 75) . '...';
          }

          // Submitted.
          $vars['submitted'] = '<time datetime=' . $year . '-' . $month . '-' . $day . ' pubdate>' . $date_display . '</time>';

        break;

        // Type: Announcement and Blog - Full.
        case 'full':

          // Submitted.
          $vars['submitted'] = '<em>' . t('Date Published') . '</em> ' . '<time datetime=' . $day . '-' . $month . '-' . $year . ' pubdate>' . format_date($vars['node']->created, 'custom', 'F j, Y') . '</time>';

          // Unset links.
          $vars['content']['links'] = NULL;

        break;

      }
    break;

    // Type: FAQ.
    case 'faq':
      switch ($vars['view_mode']) {

        // Type: FAQ - Full.
        case 'teaser':

          // Remove submitted byline.
          $vars['display_submitted'] = NULL;

          // Remove node URL.
          if (isset($vars['title'])) {
            $vars['node_url'] = '#';
          }

        break;
      }
    break;

    // Type: Member.
    case 'member':

      // Member type.
      $member_type = (isset($vars['field_member_type'][0]['value']))
        ? $vars['field_member_type'][0]['value']
        : NULL;

      // If "Member type" is "Advisory Board" then display image or placeholder.
      if ($member_type == 'Advisory Board') {

        $image = NULL;
        // Grab image and title and rewrite that to output image with content.
        if (isset($vars['field_member_logo'][0]['uri'])) {
          $path = $vars['field_member_logo'][0]['uri'];

          // Image with preset.
          $image = '<figure>' . theme_image_style(
            array(
              'style_name' => 'security_expert',
              'path' => $path,
              'alt' => $title,
              'width' => NULL,
              'height' => NULL,
            )
          ) . '</figure>';
        }
        else {
          // Placeholder image.
          $path = $theme_path . '/images/member-placeholder.png';
          $image = '<figure>' . theme_image(
            array(
              'path' => $path,
              'attributes' => array('class' => array('placeholder'))
            )
          ) . '</figure>';
        }
      }

      switch ($vars['view_mode']) {
        // Type: Member - Teaser mini.
        case 'teaser_mini':

          switch ($member_type) {
            case 'Member':

              // Grab logo, title, and website and rewrite that to output image with link.
              $path = $vars['field_member_logo'][0]['uri'];
              $logo = '<img src="' . file_create_url($path) . '" title="' . $title . '" alt="' . $title . '"/>';

              // URL.
              $url = (isset($vars['content']['field_member_website']['#items'][0]['url']))
                ? $vars['content']['field_member_website']['#items'][0]['url']
                : NULL;

              // Logo.
              $vars['content']['logo'] = array(
                '#markup' => '<figure>' . l($logo, $url, array('html' => TRUE)) . '</figure>',
              );

              // Remove title since already used.
              if (isset($vars['title'])) {
                $vars['title'] = NULL;
              }

              // Unset content as not needed now.
              unset($vars['content']['field_member_type'][0]);
              unset($vars['content']['field_member_logo'][0]);
              unset($vars['content']['field_member_website'][0]);

              break;

            case 'Advisory Board':

              // Job title.
              $path = (isset($vars['field_member_logo'][0]['uri']))
                ? $vars['field_member_logo'][0]['uri']
                : NULL;

              // Job title.
              $jobtitle = (isset($vars['content']['field_job_title'][0]['#markup']))
                ? '<div class="field-job-title">' . $vars['content']['field_job_title'][0]['#markup'] . '</div>'
                : NULL;

              // Logo.
              $vars['content']['image'] = array(
                '#markup' =>
                  '<figure>' .
                    $image .
                    '<figcaption>' .
                      '<h2 class="title">' . $title . '</h2>' .
                      $jobtitle .
                    '</figcaption>' .
                  '</figure>',
              );

              // Remove title since already used.
              if (isset($vars['title'])) {
                $vars['title'] = NULL;
              }

              // Unset content as not needed now.
              unset($vars['content']['field_member_type'][0]);
              unset($vars['content']['field_job_title']);
              unset($vars['content']['field_member_logo']);

              break;
          }

        break;

        // Type: Member - Teaser.
        case 'teaser':

          if (isset($vars['field_member_type'][0]['value'])) {
            $member_type = $vars['field_member_type'][0]['value'];

            switch ($member_type) {

              case 'Steering Committee':
                // Steering Committee don't have images. Avoid PHP notice.
                $image = NULL;

              case 'Advisory Board':

                // Job title.
                $jobtitle = (isset($vars['content']['field_job_title'][0]['#markup']))
                  ? '<div class="field-job-title">' . $vars['content']['field_job_title'][0]['#markup'] . '</div>'
                  : NULL;

                // Body.
                $body = (isset($vars['body'][0]))
                  ? '<div class="body">' . $vars['body'][0]['safe_value'] . '</div>'
                  : NULL;

                // Website.
                $website = (isset($vars['content']['field_member_website'][0]['#element']['url']))
                  ? l(t($vars['content']['field_member_website'][0]['#element']['url']), $vars['content']['field_member_website'][0]['#element']['url'])
                  : NULL;

                // Content.
                $vars['content']['member']['#markup'] =
                  $image .
                  '<div class="content">' .
                    '<h2 class="title">' . $title . '</h2>' .
                    $jobtitle .
                    $body .
                    $website .
                  '</div>';

                // Remove title since already used.
                if (isset($vars['title'])) {
                  $vars['title'] = NULL;
                }

                // Unset content as not needed now.
                unset($vars['content']['field_job_title']);
                unset($vars['content']['field_member_website']);
                unset($vars['content']['field_member_logo']);
                unset($vars['content']['body']);
                unset($vars['content']['field_member_type']);

                break;
            }
          }
        break;

      break;
    }
  }
}