You are here

function _ds_search_group_by_type in Display Suite 7.2

Helper function to group by type.

1 call to _ds_search_group_by_type()
ds_search_search_page in modules/ds_search/ds_search.module
Implements hook_search_page().

File

modules/ds_search/ds_search.module, line 321
Display Suite search.

Code

function _ds_search_group_by_type(&$build) {
  $settings = variable_get('ds_search_group_by_type_settings');
  foreach ($build['search_results'] as $id => $result) {
    if ($settings[$result['#bundle']]['status']) {

      // Type group.
      if (!isset($build['search_results'][$result['#bundle']])) {
        $type = $settings[$result['#bundle']]['wrapper'];
        $title = check_plain(t($settings[$result['#bundle']]['label']));
        $class = 'group-result group-result-' . strtr($result['#bundle'], '_', '-');
        $parity[$result['#bundle']] = 'odd';
        $build['search_results'][$result['#bundle']] = array(
          '#type' => $type,
          '#title' => $title,
          '#weight' => $settings[$result['#bundle']]['weight'],
          '#attributes' => array(
            'class' => array(
              $class,
            ),
          ),
        );
        if ($type == 'markup') {
          $build['search_results'][$result['#bundle']]['#prefix'] = '<div class="' . $class . '">' . (!empty($title) ? ' <h2>' . $title . '</h2>' : '');
          $build['search_results'][$result['#bundle']]['#suffix'] = '</div>';
        }
      }

      // Move result into the wrapper of its type and unset previous.
      $build['search_results'][$result['#bundle']][$id] = $result;
      unset($build['search_results'][$id]);

      // Add the parity to the result to enable correct zebra striping.
      $build['search_results'][$result['#bundle']][$id]['#node']->ds_search_zebra = $parity[$result['#bundle']];
      $parity[$result['#bundle']] = $parity[$result['#bundle']] == 'odd' ? 'even' : 'odd';
    }
    else {

      // Other group.
      if (!isset($build['search_results']['ds-other'])) {
        $title = check_plain(t(variable_get('ds_search_group_by_type_other', 'Other')));
        $type = variable_get('ds_search_group_by_type_other_wrapper', 'fieldset');
        $class = 'group-result group-result-other';
        $parity['ds-other'] = 'odd';
        $build['search_results']['ds-other'] = array(
          '#type' => $type,
          '#title' => $title,
          '#weight' => 100,
          '#attributes' => array(
            'class' => array(
              $class,
            ),
          ),
        );
        if ($type == 'markup') {
          $build['search_results']['ds-other']['#prefix'] = '<div class="' . $class . '">' . (!empty($title) ? '<h2>' . $title . '</h2>' : '');
          $build['search_results']['ds-other']['#suffix'] = '</div>';
        }
      }

      // Move result into other wrapper and unset previous.
      $build['search_results']['ds-other'][$id] = $result;
      unset($build['search_results'][$id]);

      // Add the parity to the result to enable correct zebra striping.
      $build['search_results']['ds-other'][$id]['#node']->ds_search_parity = $parity['ds-other'];
      $parity['ds-other'] = $parity['ds-other'] == 'odd' ? 'even' : 'odd';
    }
  }
}