<?php

use \Drupal\taxonomy\Entity\Term;
use \Drupal\taxonomy\Entity\Vocabulary;

/**
 * @file
 * Bootstrap sub-theme.
 *
 * Place your custom PHP code in this file.
 */

function asc_bootstrap_bux_preprocess_page(&$vars, $hook)
{
  // Render the library as laid out in herchel.libraries.yml
  $libraries['#attached']['library'][] = 'asc_bootstrap/asc_bootstrap-corescripts';
  \Drupal::service('renderer')->render($libraries);

  //adds site name variable which is used as logo alt and title text
  $vars['site_name'] = \Drupal::config('system.site')->get('name');

  $vars['osu_logo'] = theme_get_setting('osu_logo', 'asc_bootstrap_bux');
}

function asc_bootstrap_bux_preprocess_node(&$vars, $hook)
{
  $vars['site_mail'] = \Drupal::config('system.site')->get('mail');
  $vars['site_name'] = \Drupal::config('system.site')->get('name');

  //get pathauto url alias for event
  $config = \Drupal::service('config.factory')->get('pathauto.pattern.events');
  $evpattern = explode('/', $config->get('pattern'));
  $vars['evpattern'] = $evpattern[0];

  //get research landing page url alias
  $nids = \Drupal::entityQuery('node')->condition('type', 'research_landing_page')->accessCheck(TRUE)->execute();
  $lid = array_shift($nids);
  $vars['resrch_landing'] = \Drupal::service('path_alias.manager')->getAliasByPath('/node/' . $lid);

  //get research gallery landing page url alias
  $nids2 = \Drupal::entityQuery('node')->condition('type', 'research_gallery_landing_page')->accessCheck(TRUE)->execute();
  $lid2 = array_shift($nids2);
  $vars['resrch_gall_landing'] = \Drupal::service('path_alias.manager')->getAliasByPath('/node/' . $lid2);
}

/*
* adds content type to theme suggestions so that landing page doesn't default to page.html.twig
*/
function asc_bootstrap_bux_theme_suggestions_page_alter(array &$suggestions, array $variables)
{
  // Add content type suggestions.
  if (($node = \Drupal::request()->attributes->get('node')) && (strpos($_SERVER['REQUEST_URI'], "revisions") == false)) {
    array_splice($suggestions, 1, 0, 'page__' . $node->getType());
  }
}

/*
* adds content type to variable to be used in paragraph twig files.
*/
function asc_bootstrap_bux_preprocess_paragraph(&$variables)
{
  if (\Drupal::routeMatch()->getRouteName() == 'entity.node.canonical') {
    $node = \Drupal::routeMatch()->getParameter('node');
    $variables['page_content_type'] = $node->getType();
  }
}


function asc_bootstrap_bux_preprocess_block(&$vars)
{
  $vars['site_mail'] = \Drupal::config('system.site')->get('mail');
  $vars['site_name'] = \Drupal::config('system.site')->get('name');
}


/*
* alters news and events filters so that AJAX doesn't remove headings, etc. upon request
*/
function asc_bootstrap_bux_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
{
  /*
  * events and news filters
  */
  if (
    ($form['#id'] == 'views-exposed-form-events-page-1')
    || ($form['#id'] == 'views-exposed-form-news-page-1')
    || ($form['#id'] == 'views-exposed-form-gallery-directory-page-1')
  ) {
    /*
    * add new HTML content to filters (headers and spacer)
    * and setting weights for where they should appear in the form hierarchy
    */
    $form['filter_heading'] = array(
      '#markup' => '<h2 class="filter-heading">Filter</h2>',
      '#weight' => -10,
    );
    $form['filter_search_heading'] = array(
      '#markup' => '<div class="filter-linebreak"></div>',
      '#weight' => 4,
    );
    $form['spacer'] = array(
      '#markup' => '<div class="filter-spacer"></div>',
      '#weight' => 1.5,
    );
    /*
    * setting up weights for form elements
    */
    $form['field_evt_date_range_value']['#weight'] = 1;
    $form['end_date']['#weight'] = 1;
    $form['field_evt_taxonomy_target_id_1']['#weight'] = 2;
    $form['field_news_terms_1_target_id']['#weight'] = 2.5;
    $form['field_evt_taxonomy2_target_id']['#weight'] = 3;
    $form['field_news_terms_2_target_id']['#weight'] = 3.5;
    $form['title']['#weight'] = 5;
    $form['title']['#attributes']['placeholder'] = t('Search Keywords');
  }

  /*
  * research filters
  */
  if (
    ($form['#id'] == 'views-exposed-form-research-project-directory-page-1')
    || ($form['#id'] == 'views-exposed-form-research-project-gallery-page-1')
    || ($form['#id'] == 'views-exposed-form-research-project-directory-mobile-page-1')
    || ($form['#id'] == 'views-exposed-form-research-project-directory-block-1')
    || ($form['#id'] == 'views-exposed-form-research-project-gallery-block-1')
    || ($form['#id'] == 'views-exposed-form-research-project-directory-mobile-block-1')
  ) {
    /*
    * add new HTML content to filters (headers and spacer)
    * and setting weights for where they should appear in the form hierarchy
    */
    $form['filter_heading'] = array(
      '#markup' => '<h2 class="filter-heading">Filter</h2>',
      '#weight' => -10,
    );
    $form['filter_search_heading'] = array(
      '#markup' => '<div class="filter-linebreak"></div>',
      '#weight' => 4,
    );
    $form['spacer'] = array(
      '#markup' => '<div class="filter-spacer"></div>',
      '#weight' => 1.5,
    );
    /*
    * setting up weights for form elements
    */
    $form['title']['#weight'] = 5;
    $form['title']['#attributes']['placeholder'] = t('Search Keywords');
  }

  /*
    * checking if taxonomy has vocabulary - if not, don't display field
    */

  //courses
  if ($form['#id'] == 'views-exposed-form-courses-page-2') {
    /*
    * add new HTML content to filters (headers and spacer)
    * and setting weights for where they should appear in the form hierarchy
    */
    if ((count($form['field_course_term_1_target_id']['#options']) > 1)
      || (count($form['field_course_term_2_target_id']['#options']) > 1)
      || (count($form['field_course_term_3_target_id']['#options']) > 1)
    ) {
      $form['filter_heading'] = array(
        '#markup' => '<h2 class="filter-heading">Filter</h2>',
        '#weight' => -10,
      );
    }
    $form['filter_search_heading'] = array(
      '#markup' => '<div class="filter-linebreak"></div>',
      '#weight' => 4,
    );
    $form['spacer'] = array(
      '#markup' => '<div class="filter-spacer"></div>',
      '#weight' => 1.5,
    );
    /*
    * setting up weights for form elements
    */
    $form['title']['#weight'] = 5;
    $form['title']['#attributes']['placeholder'] = t('Search Keywords');


    if (count($form['field_course_term_1_target_id']['#options']) == 1) {
      $form['field_course_term_1_target_id']['#access'] = FALSE;
    }

    if (count($form['field_course_term_2_target_id']['#options']) == 1) {
      $form['field_course_term_2_target_id']['#access'] = FALSE;
    }

    if (count($form['field_course_term_3_target_id']['#options']) == 1) {
      $form['field_course_term_3_target_id']['#access'] = FALSE;
    }

    if (isset($form['field_course_term_1_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_course_term_1_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_course_term_1_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }

    if (isset($form['field_course_term_2_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_course_term_2_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_course_term_2_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }

    if (isset($form['field_course_term_3_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_course_term_3_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_course_term_3_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }
  }


  //courses mobile
  if ($form['#id'] == 'views-exposed-form-courses-page-3') {
    if ((count($form['field_course_term_1_target_id']['#options']) > 1)
      || (count($form['field_course_term_2_target_id']['#options']) > 1)
      || (count($form['field_course_term_3_target_id']['#options']) > 1)
    ) {
      $form['filter_heading'] = array(
        '#markup' => '<h2 class="filter-heading">Filter</h2>',
        '#weight' => -10,
      );
    }
    $form['filter_search_heading'] = array(
      '#markup' => '<div class="filter-linebreak"></div>',
      '#weight' => 4,
    );
    $form['spacer'] = array(
      '#markup' => '<div class="filter-spacer"></div>',
      '#weight' => 1.5,
    );
    /*
    * setting up weights for form elements
    */
    $form['title']['#weight'] = 5;
    $form['title']['#attributes']['placeholder'] = t('Search Keywords');

    if (count($form['field_course_term_1_target_id']['#options']) == 1) {
      $form['field_course_term_1_target_id']['#access'] = FALSE;
    }

    if (count($form['field_course_term_2_target_id']['#options']) == 1) {
      $form['field_course_term_2_target_id']['#access'] = FALSE;
    }

    if (count($form['field_course_term_3_target_id']['#options']) == 1) {
      $form['field_course_term_3_target_id']['#access'] = FALSE;
    }

    if (isset($form['field_course_term_1_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_course_term_1_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_course_term_1_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }

    if (isset($form['field_course_term_2_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_course_term_2_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_course_term_2_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }

    if (isset($form['field_course_term_3_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_course_term_3_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_course_term_3_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }
  }


  //departments and centers
  if ($form['#id'] == 'views-exposed-form-departments-centers-page-1') {
    if (count($form['field_dc_type_target_id']['#options']) == 1) {
      $form['field_dc_type_target_id']['#access'] = FALSE;
    }
    if (count($form['field_dc_type_target_id']['#options']) > 1) {
      $form['filter_heading'] = array(
        '#markup' => '<h2 class="filter-heading">Filter</h2>',
        '#weight' => -10,
      );
    }

    if (isset($form['field_dc_type_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_dc_type_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      $term = Term::load($tid);
      if (!empty($term)) {
        $vid = array_shift($term->vid->getValue())['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_dc_type_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }
  }


  //events
  if ($form['#id'] == 'views-exposed-form-events-page-1') {
    if (count($form['field_evt_taxonomy_target_id_1']['#options']) == 1) {
      $form['field_evt_taxonomy_target_id_1']['#access'] = FALSE;
    }

    if (count($form['field_evt_taxonomy2_target_id']['#options']) == 1) {
      $form['field_evt_taxonomy2_target_id']['#access'] = FALSE;
    }

    if (isset($form['field_evt_taxonomy_target_id_1']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_evt_taxonomy_target_id_1']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_evt_taxonomy_target_id_1']['#options']['All'] = $vocab_label;
        }
      }
    }

    if (isset($form['field_evt_taxonomy2_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_evt_taxonomy2_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_evt_taxonomy2_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }
  }

  //gallery
  if ($form['#id'] == 'views-exposed-form-gallery-directory-page-1') {
    if (count($form['field_gallery_terms_1_target_id']['#options']) == 1) {
      $form['field_gallery_terms_1_target_id']['#access'] = FALSE;
    }

    if (count($form['field_gallery_terms_2_target_id']['#options']) == 1) {
      $form['field_gallery_terms_2_target_id']['#access'] = FALSE;
    }

    if (count($form['field_gallery_terms_3_target_id']['#options']) == 1) {
      $form['field_gallery_terms_3_target_id']['#access'] = FALSE;
    }

    if (isset($form['field_gallery_terms_1_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_gallery_terms_1_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_gallery_terms_1_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }

    if (isset($form['field_gallery_terms_2_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_gallery_terms_2_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_gallery_terms_2_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }

    if (isset($form['field_gallery_terms_3_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_gallery_terms_3_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_gallery_terms_3_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }
  }

  //news
  if ($form['#id'] == 'views-exposed-form-news-page-1') {
    if (count($form['field_news_terms_1_target_id']['#options']) == 1) {
      $form['field_news_terms_1_target_id']['#access'] = FALSE;
    }

    if (count($form['field_news_terms_2_target_id']['#options']) == 1) {
      $form['field_news_terms_2_target_id']['#access'] = FALSE;
    }


    if (isset($form['field_news_terms_1_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_news_terms_1_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_news_terms_1_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }

    if (isset($form['field_news_terms_2_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_news_terms_2_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_news_terms_2_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }
  }

  //research filters
  if (
    ($form['#id'] == 'views-exposed-form-research-project-directory-block-1')
    || ($form['#id'] == 'views-exposed-form-research-project-directory-page-1')
    || ($form['#id'] == 'views-exposed-form-research-project-gallery-block-1')
    || ($form['#id'] == 'views-exposed-form-research-project-gallery-page-1')
  ) {
    if (count($form['field_research_departments_target_id']['#options']) == 1) {
      $form['field_research_departments_target_id']['#access'] = FALSE;
    }

    if (count($form['field_research_other_target_id']['#options']) == 1) {
      $form['field_research_other_target_id']['#access'] = FALSE;
    }

    if (count($form['field_research_year_target_id']['#options']) == 1) {
      $form['field_research_year_target_id']['#access'] = FALSE;
    }

    if (isset($form['field_research_departments_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_research_departments_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_research_departments_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }

    if (isset($form['field_research_other_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_research_other_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_research_other_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }

    if (isset($form['field_research_year_target_id']['#options']['All'])) {
      $tarr = array_slice(array_keys($form['field_research_year_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_research_year_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }
  }

  //research - mobile
  if (($form['#id'] == 'views-exposed-form-research-project-directory-mobile-block-1') || ($form['#id'] == 'views-exposed-form-research-project-directory-mobile-page-1')) {
    if (count($form['field_research_departments_target_id']['#options']) == 0) {
      $form['field_research_departments_target_id']['#access'] = FALSE;
    }

    if (count($form['field_research_other_target_id']['#options']) == 0) {
      $form['field_research_other_target_id']['#access'] = FALSE;
    }

    if (count($form['field_research_year_target_id']['#options']) == 0) {
      $form['field_research_year_target_id']['#access'] = FALSE;
    }

    if ($form['field_research_departments_target_id']['#theme_wrappers']['details']['#title'] == 'Departments') {
      $tarr = array_slice(array_keys($form['field_research_departments_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_research_departments_target_id']['#theme_wrappers']['details']['#title'] = $vocab_label;
        }
      }
    }

    if ($form['field_research_other_target_id']['#theme_wrappers']['details']['#title'] == 'Other') {
      $tarr = array_slice(array_keys($form['field_research_other_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_research_other_target_id']['#theme_wrappers']['details']['#title'] = $vocab_label;
        }
      }
    }

    if ($form['field_research_year_target_id']['#theme_wrappers']['details']['#title'] == 'Year') {
      $tarr = array_slice(array_keys($form['field_research_year_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_research_year_target_id']['#theme_wrappers']['details']['#title'] = $vocab_label;
        }
      }
    }
  }


  /*
  * research gallery filters
  */
  if (($form['#id'] == 'views-exposed-form-research-project-gallery-block-1') || ($form['#id'] == 'views-exposed-form-research-project-gallery-page-1')) {

    if (count((array)$form['field_research_gallery_tags_target_id']['#options']) == 1) {
      $form['field_research_gallery_tags_target_id']['#access'] = FALSE;
    }

    if (count((array)$form['field_research_gallery_tags_2_target_id']['#options']) == 1) {
      $form['field_research_gallery_tags_2_target_id']['#access'] = FALSE;
    }

    if (count((array)$form['field_research_gallery_tags_3_target_id']['#options']) == 1) {
      $form['field_research_gallery_tags_3_target_id']['#access'] = FALSE;
    }

    if (isset($form['field_research_gallery_tags_target_id']['#options']['All'])) {

      $tarr = array_slice(array_keys($form['field_research_gallery_tags_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_research_gallery_tags_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }

    if (isset($form['field_research_gallery_tags_2_target_id']['#options']['All'])) {

      $tarr = array_slice(array_keys($form['field_research_gallery_tags_2_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_research_gallery_tags_2_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }


    if (isset($form['field_research_gallery_tags_3_target_id']['#options']['All'])) {

      $tarr = array_slice(array_keys($form['field_research_gallery_tags_3_target_id']['#options']), 1, 5, true);
      $tid = array_shift($tarr);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_research_gallery_tags_3_target_id']['#options']['All'] = $vocab_label;
        }
      }
    }
  }



  /*
  * people filters
  */
  if ($form['#id'] == 'views-exposed-form-people-directory-page-1') {
    /*
    * add new HTML content to filters (headers and spacer)
    * and setting weights for where they should appear in the form hierarchy
    */
    $form['filter_heading'] = array(
      '#markup' => '<h2 class="filter-heading">Filter</h2>',
      '#weight' => -10,
    );
    $form['filter_search_heading'] = array(
      '#markup' => '<div class="filter-linebreak"></div>',
      '#weight' => 4,
    );
    $form['spacer'] = array(
      '#markup' => '<div class="filter-spacer"></div>',
      '#weight' => 1.5,
    );
    /*
    * setting up weights for form elements
    */
    $form['field_appointment_target_id']['#weight'] = 1;
    $form['field_research_interests_target_id']['#weight'] = 2;
    $form['field_other_target_id']['#weight'] = 3;
    $form['combine']['#weight'] = 5;
    $form['keys']['#attributes']['placeholder'] = t('Search Keywords');
    /*
    * checking if taxonomy has vocabulary - if not, don't display field
    */
    if (count($form['field_appointment_target_id']['#options']) == 0) {
      $form['field_appointment_target_id_collapsible']['#access'] = FALSE;
    } else {
      $tmp = array_keys($form['field_appointment_target_id']['#options']);
      $tid = array_shift($tmp);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_appointment_target_id_collapsible']['#title'] = $vocab_label;
        }
      }
    }

    if (count($form['field_research_interests_target_id']['#options']) == 0) {
      $form['field_research_interests_target_id_collapsible']['#access'] = FALSE;
    } else {
      $tmp = array_keys($form['field_research_interests_target_id']['#options']);
      $tid = array_shift($tmp);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_research_interests_target_id_collapsible']['#title'] = $vocab_label;
        }
      }
    }


    if (count($form['field_other_target_id']['#options']) == 0) {
      $form['field_other_target_id_collapsible']['#access'] = FALSE;
    } else {
      $tmp = array_keys($form['field_other_target_id']['#options']);
      $tid = array_shift($tmp);
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_other_target_id_collapsible']['#title'] = $vocab_label;
        }
      }
    }
  }

  /*
  * mobile people filters
  */
  if ($form['#id'] == 'views-exposed-form-people-directory-page-2') {
    /*
    * add new HTML content to filters (headers and spacer)
    * and setting weights for where they should appear in the form hierarchy
    */
    $form['filter_heading'] = array(
      '#markup' => '<h2 class="ppl-filter-heading ppl-taxon-heading">Filter</h2>',
      '#weight' => 2,
    );
    $form['filter_search_heading'] = array(
      '#markup' => '<div class="filter-linebreak"></div>',
      '#weight' => 0,
    );
    /*
    * setting up weights for form elements
    */
    $form['combine']['#weight'] = 1;
    $form['field_appointment_target_id_collapsible']['#weight'] = 3;
    $form['field_research_interests_target_id_collapsible']['#weight'] = 4;
    $form['field_other_target_id_collapsible']['#weight'] = 5;

    if (count($form['field_appointment_target_id']['#options']) == 0) {
      $form['field_appointment_target_id_collapsible']['#access'] = FALSE;
    } else {
      $tid = array_shift(array_keys($form['field_appointment_target_id']['#options']));
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_appointment_target_id_collapsible']['#title'] = $vocab_label;
        }
      }
    }

    if (count($form['field_research_interests_target_id']['#options']) == 0) {
      $form['field_research_interests_target_id_collapsible']['#access'] = FALSE;
    } else {
      $tid = array_shift(array_keys($form['field_research_interests_target_id']['#options']));
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_research_interests_target_id_collapsible']['#title'] = $vocab_label;
        }
      }
    }

    if (count($form['field_other_target_id']['#options']) == 0) {
      $form['field_other_target_id_collapsible']['#access'] = FALSE;
    } else {
      $tid = array_shift(array_keys($form['field_other_target_id']['#options']));
      if (isset($tid)) {
        $term = Term::load($tid);
      }
      if (!empty($term)) {
        $tmp = $term->vid->getValue();
        $vid = array_shift($tmp)['target_id'];
        if (!empty($vid)) {
          $vocab = Vocabulary::load($vid);
          $vocab_label = $vocab->get('name');
          $form['field_other_target_id_collapsible']['#title'] = $vocab_label;
        }
      }
    }
  }
}


function asc_bootstrap_bux_views_pre_render(\Drupal\views\ViewExecutable $view)
{
  // print " - view_current_display: " . $view->current_display;

  //Research Project Directory
  if ($view->id() == 'research_project_directory' || $view->id() == 'research_project_directory_mobile') {
    $view->element['#cache']['max-age'] = 0;
    // get vocabulary name and set label to it
    $vocab_dep = Vocabulary::load('departments');
    $vocab_dep_label = $vocab_dep->get('name');
    $vocab_year = Vocabulary::load('year');
    $vocab_year_label = $vocab_year->get('name');
    $vocab_other = Vocabulary::load('other');
    $vocab_other_label = $vocab_other->get('name');

    $view->field['field_research_departments']->options['label'] = $vocab_dep_label;
    $view->field['field_research_year']->options['label'] = $vocab_year_label;
    $view->field['field_research_other']->options['label'] = $vocab_other_label;
  }

  //Discover Your Path
  if ($view->id() == 'discover_your_path') {
    //gets filter values from url
    $values = $view->getExposedInput();
    //counts number of filters selected
    if (isset($values['field_ap_interest_target_id'])) {
      $count = count((array)$values['field_ap_interest_target_id']);
    }
    ///////////////////////////////////////////////////////////////
    //if no URL parameter is present
    ///////////////////////////////////////////////////////////////
    if (empty(\Drupal::request()->query->all()['field_ap_interest_target_id']) || !\Drupal::request()->query->all()['field_ap_interest_target_id']) {
      //clear results
      $view->result = [];
      //changes text in header results if url parameter does not exist
      $view->header['result']->options['content'] = '@total Programs<br/><br/>';
    } else {
    ///////////////////////////////////////////////////////////////
    //if URL parameters are present
    ///////////////////////////////////////////////////////////////
      //displays number of matched interests vs number of interests selected
      $view->field['nothing_2']->options['alter']['text'] = '<span class="dyp-interests">{{ field_ap_interest }}/' . $count . ' Interests</span>';

      //get parameters from URL
      $currentInterests = array_values(\Drupal::request()->query->all()['field_ap_interest_target_id']);

      //count length of currentInterests array
      $currentInterestsLength = count($currentInterests);

      $i = 0;
      //set up variable to hold parameter values
      $interestParams = '&';
      //loop through currentInterests and add to interestParam variable
      while ($i < $currentInterestsLength) {
        //check if last time through loop - if so remove & from param
        if ($i + 1 == $currentInterestsLength) {
          $interestParams .= 'field_ap_interest_target_id[' . $currentInterests[$i] . ']=' . $currentInterests[$i];
        } else {
          $interestParams .= 'field_ap_interest_target_id[' . $currentInterests[$i] . ']=' . $currentInterests[$i] . '&';
        }
        $i++;
      }

      ///////////////////////////////////////////////////////////////
      //get all view results into an array to easily search through
      ///////////////////////////////////////////////////////////////
      $view_result = $view->result;
      if (!empty($view_result)) {
        foreach ($view->result as $rid => $row) {
          foreach ($view->field as $fid => $field) {
            $viewValues[$rid][$fid] = $field->getValue($row);
          }
        }
      }

      ///////////////////////////////////////////////////////////////
      //creates matching interests
      ///////////////////////////////////////////////////////////////
      $i = 0;
      while ($i < count($viewValues)) {
        //compares interest field values in url parameters with viewValues variable and
        //removes values that don't match and save to variable
        $interestResults = array_intersect($values['field_ap_interest_target_id'], $viewValues[$i]['field_ap_interest_1']);
        //sets result for interest field to matched results saved in interestResults variable
        $view_result[$i]->_entity->set('field_ap_interest', $interestResults);
        $i++;
      }

      //setup counting variables
      $majorsCount = 0;
      $minorsCount = 0;
      $certCount = 0;
      $gradCount = 0;
      $i = 0;
      $majorsClasses = '';
      $minorsClasses = '';
      $certsClasses = '';
      $gradClasses = '';
      $allClasses = '';
      ///////////////////////////////////////////////////////////////
      //loop through our viewValues array and count how many of each program exists
      ///////////////////////////////////////////////////////////////
      while ($i < count($viewValues)) {
        switch ($viewValues[$i]['field_ap_program']) {
          case 51:
            $majorsCount++;
            break;
          case 52:
            $minorsCount++;
            break;
          case 53:
            $certCount++;
            break;
          case 54:
            $gradCount++;
            break;
        }
        $i++;
      }

      ///////////////////////////////////////////////////////////////
      //get count from url if user is coming from a filtered search to a specific program
      ///////////////////////////////////////////////////////////////
      //this insures that the counts on other programs aren't set to 0 when user
      //selects a specific program
      if (\Drupal::request()->query->get('major_count') != '') {
        $majorsCount = \Drupal::request()->query->get('major_count');
      }
      if (\Drupal::request()->query->get('minor_count') != '') {
        $minorsCount = \Drupal::request()->query->get('minor_count');
      }
      if (\Drupal::request()->query->get('cert_count') != '') {
        $certCount = \Drupal::request()->query->get('cert_count');
      }
      if (\Drupal::request()->query->get('grad_count') != '') {
        $gradCount = \Drupal::request()->query->get('grad_count');
      }
      ///////////////////////////////////////////////////////////////
      //check if program is active
      ///////////////////////////////////////////////////////////////
      if (\Drupal::request()->query->get('active') != '') {
        $active = \Drupal::request()->query->get('active');

        //setting up active classes for buttons
        if ($active == 'majors') {
          $majorsClasses = 'active';
        }

        if ($active == 'minors') {
          $minorsClasses = 'active';
        }

        if ($active == 'certs') {
          $certsClasses = 'active';
        }

        if ($active == 'grad') {
          $gradClasses = 'active';
        }

        if ($active == 'all') {
          $allClasses = 'active';
        }
      } else {
        $allClasses = 'active';
      }

      ///////////////////////////////////////////////////////////////
      //create program buttons and replace header textarea with them
      ///////////////////////////////////////////////////////////////
      //in button link we're adding the program counts to the end of the URL parameters
      //this insures that we will retain the program counts if the user selects one of
      //the program buttons to filter to a specific program
      $view->header['area_text_custom']->options['content'] = '
      <a href="/discover-your-path?field_ap_program_target_id=51' . $interestParams . '&minor_count=' . $minorsCount . '&cert_count=' . $certCount . '&grad_count=' . $gradCount . '&active=majors" title="Majors" class="interest-majors ' . $majorsClasses . '">Majors (' . $majorsCount . ')</a>
      <a href="/discover-your-path?field_ap_program_target_id=52' . $interestParams . '&major_count=' . $majorsCount . '&cert_count=' . $certCount . '&grad_count=' . $gradCount . '&active=minors" title="Minors" class="interest-minors ' . $minorsClasses . '">Minors (' . $minorsCount . ')</a>
      <a href="/discover-your-path?field_ap_program_target_id=53' . $interestParams . '&major_count=' . $majorsCount . '&minor_count=' . $minorsCount . '&grad_count=' . $gradCount . '&active=certs" title="Certificates" class="interest-certs ' . $certsClasses . '">Certificates (' . $certCount . ')</a>
      <a href="/discover-your-path?field_ap_program_target_id=54' . $interestParams . '&major_count=' . $majorsCount . '&minor_count=' . $minorsCount . '&cert_count=' . $certCount . '&active=grad" title="Graduate" class="interest-grad ' . $gradClasses . '">Graduate (' . $gradCount . ')</a>
      <a href="/discover-your-path?field_ap_program_target_id=All' . $interestParams . '&active=all" title="All" class="interest-all ' . $allClasses . '">All</a>';
    }
  }
}

/*
* views
*/
function asc_bootstrap_bux_preprocess_views_view(&$vars)
{
  //adds view name variable which is used to identify which view directory user is on (based off view id)
  $vars['view_name'] = $vars['id'];
}