Skip to content
Snippets Groups Projects
asc_bootstrap.theme 14.1 KiB
Newer Older
bcweaver's avatar
bcweaver committed
<?php
use \Drupal\taxonomy\Entity\Term;
use \Drupal\taxonomy\Entity\Vocabulary;

bcweaver's avatar
bcweaver committed
/**
 * @file
 * Bootstrap sub-theme.
 *
 * Place your custom PHP code in this file.
 */

function asc_bootstrap_preprocess_page(&$vars, $hook) {
 // Render the library as laid out in herchel.libraries.yml
 $libraries['#attached']['library'][] = 'asc_bootstrap/asc_bootstrap-corescripts';
 drupal_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');
bcweaver's avatar
bcweaver committed
}

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

bcweaver's avatar
bcweaver committed
//create another suggestion based on the block type

/* function asc_bootstrap_theme_suggestions_block_alter(array &$suggestions, array $variables) {

function asc_bootstrap_theme_suggestions_block_alter(array &$suggestions, array $variables) {
>>>>>>> a03c8d80522d837a9acc6a1213d7ad7e61dc9e51
  // Block suggestions for custom block bundles.
  if (isset($variables['elements']['content']['#block_content'])) {
    array_splice($suggestions, 1, 0, 'block__bundle__' . $variables['elements']['content']['#block_content']->bundle());
  }
} */

/*
* adds content type to theme suggestions so that landing page doesn't default to page.html.twig
*/
function asc_bootstrap_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    $content_type = $node->bundle();
    $suggestions[] = 'page__'.$content_type;
  }
}
* adds content type to variable to be used in paragraph twig files.
*/
function asc_bootstrap_preprocess_paragraph(&$variables) {
  $node = \Drupal::request()->attributes->get('node');
  if ($node != null) {
    $variables['page_content_type'] = $node->getType();
  }


function asc_bootstrap_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_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  /*
  * events and news filters
  */
Brian Canini's avatar
Brian Canini committed
  if (($form['#id'] == 'views-exposed-form-events-page-1')||($form['#id'] == 'views-exposed-form-news-page-1')||($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-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' => '<h2 class="filter-heading search-heading">Search</h2>',
      '#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['keys']['#weight'] = 5;
    $form['keys']['#attributes']['placeholder'] = t('Search Keywords');
Brian Canini's avatar
Brian Canini committed
  }
    /*
    * checking if taxonomy has vocabulary - if not, don't display field
    */
Brian Canini's avatar
Brian Canini committed
  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;
    }
Brian Canini's avatar
Brian Canini committed
  }
Brian Canini's avatar
Brian Canini committed
  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;
Brian Canini's avatar
Brian Canini committed
  }
Brian Canini's avatar
Brian Canini committed
  if (($form['#id'] == 'views-exposed-form-research-project-directory-block-1')||($form['#id'] == 'views-exposed-form-research-project-directory-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;
    }
Brian Canini's avatar
Brian Canini committed
  }
Brian Canini's avatar
Brian Canini committed
  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;
    }
Brian Canini's avatar
Brian Canini committed
  }

  /*
  * research filters - setting default value on select box to taxonomy vocabulary
  */
  if (($form['#id'] == 'views-exposed-form-research-project-directory-block-1')||($form['#id'] == 'views-exposed-form-research-project-directory-page-1')) {

Brian Canini's avatar
Brian Canini committed
    if(isset($form['field_research_departments_target_id']['#options']['All'])) {
Brian Canini's avatar
Brian Canini committed
      // $form['field_research_departments_target_id']['#options']['All'] = $form['#info']['filter-field_research_departments_target_id']['label'];
      $tarr = array_slice(array_keys($form['field_research_departments_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_research_departments_target_id']['#options']['All'] = $vocab_label;
        }
      }
Brian Canini's avatar
Brian Canini committed
    if(isset($form['field_research_other_target_id']['#options']['All'])) {
Brian Canini's avatar
Brian Canini committed
      //$form['field_research_other_target_id']['#options']['All'] = $form['#info']['filter-field_research_other_target_id']['label'];
      $tarr = array_slice(array_keys($form['field_research_other_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_research_other_target_id']['#options']['All'] = $vocab_label;
        }
      }
Brian Canini's avatar
Brian Canini committed
    // dpm($form['field_research_year_target_id']);

    if(isset($form['field_research_year_target_id']['#options']['All'])) {
Brian Canini's avatar
Brian Canini committed
      //$form['field_research_year_target_id']['#options']['All'] = $form['#info']['filter-field_research_year_target_id']['label'];
      $tarr = array_slice(array_keys($form['field_research_year_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_research_year_target_id']['#options']['All'] = $vocab_label;
        }
      }
Brian Canini's avatar
Brian Canini committed


  /*
  * Research Mobile filters
  */
  if (($form['#id'] == 'views-exposed-form-research-project-directory-mobile-block-1')||($form['#id'] == 'views-exposed-form-research-project-directory-mobile-page-1')) {
      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);
          $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_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);
          $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_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);
          $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_research_year_target_id']['#theme_wrappers']['details']['#title'] = $vocab_label;
            }
          }
      }
  }

  if($form['#id'] == 'views-exposed-form-people-directory-page-1') {
    /*
    * 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']['#access'] = FALSE;
    }
Brian Canini's avatar
Brian Canini committed
      // $form['field_appointment_target_id']['#validated'] = TRUE;
      $tid = array_shift(array_keys($form['field_appointment_target_id']['#options']));
      $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['#info']['filter-field_appointment_target_id']['label'] = $vocab_label;
        }
      }
    }

    if(count($form['field_research_interests_target_id']['#options']) == 0) {
      $form['field_research_interests_target_id']['#access'] = FALSE;
    }
Brian Canini's avatar
Brian Canini committed
      // $form['field_research_interests_target_id']['#validated'] = TRUE;
      $tid = array_shift(array_keys($form['field_research_interests_target_id']['#options']));
      $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['#info']['filter-field_research_interests_target_id']['label'] = $vocab_label;
        }
      }
    }


    if(count($form['field_other_target_id']['#options']) == 0) {
      $form['field_other_target_id']['#access'] = FALSE;
    }
    else {
Brian Canini's avatar
Brian Canini committed
      // $form['field_other_target_id']['#validated'] = TRUE;
      $tid = array_shift(array_keys($form['field_other_target_id']['#options']));
      $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['#info']['filter-field_other_target_id']['label'] = $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' => '<h2 class="ppl-filter-heading ppl-search-heading">Search</h2>',
      '#weight' => 0,
    );
    /*
    * setting up weights for form elements
    */
    $form['combine']['#weight'] = 1;
    $form['field_appointment_target_id']['#weight'] = 3;
    $form['field_research_interests_target_id']['#weight'] = 4;
  }
Brian Canini's avatar
Brian Canini committed


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

  if($view->id() == 'research_project_directory' || $view->id() == 'research_project_directory_mobile') {
      // 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;
  }
}