Created
February 2, 2017 07:11
-
-
Save druman/88edba2d93c4a4296525fc318a785b4d to your computer and use it in GitHub Desktop.
Exposed sort combined custom. Write module and rewrite views-exposed-form.tpl.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_form_alter(). | |
* | |
* Alter exposed filter form in views | |
*/ | |
function MODULE_form_views_exposed_form_alter(&$form, &$form_state, $form_id) { | |
if (isset($form['sort_by'])) { | |
// Combine sort drop-downs into one. | |
$form['sorting'] = array( | |
'#type' => 'select', | |
'#id' => 'sort', | |
'#title' => $form['sort_by']['#title'], | |
); | |
foreach ($form['sort_by']['#options'] as $sort_by_key => $sort_by_title) { | |
foreach ($form['sort_order']['#options'] as $sort_order_key => $sort_order_title) { | |
$form['sorting']['#options'][$sort_by_key . '|' . $sort_order_key] = $sort_by_title . ' ' . $sort_order_title; | |
} | |
} | |
// Get default value for combined sort. | |
$sort_by_keys = array_keys($form['sort_by']['#options']); | |
$form['sorting']['#default_value'] = $sort_by_keys[0] . '|' . $form['sort_order']['#default_value']; | |
} | |
// Explode combined sort field into two values that are appropriate for views. | |
if (isset($form_state['input']['sorting'])) { | |
$sorting = explode('|', $form_state['input']['sorting']); | |
$form_state['input']['sort_by'] = $sorting[0]; | |
$form_state['input']['sort_order'] = $sorting[1]; | |
} | |
} | |
/** | |
* Default preprocess function for all filter forms. | |
*/ | |
function MODULE_preprocess_views_exposed_form(&$vars) { | |
$form = &$vars['form']; | |
// Render new created sort field. | |
if (isset($form['sorting'])) { | |
$form['sorting']['#printed'] = FALSE; | |
$vars['sorting'] = drupal_render($form['sorting']); | |
// Need to rebuild the submit button. | |
$form['submit']['#printed'] = FALSE; | |
$vars['button'] = drupal_render_children($form); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* This template handles the layout of the views exposed filter form. | |
* | |
* Variables available: | |
* - $widgets: An array of exposed form widgets. Each widget contains: | |
* - $widget->label: The visible label to print. May be optional. | |
* - $widget->operator: The operator for the widget. May be optional. | |
* - $widget->widget: The widget itself. | |
* - $sort_by: The select box to sort the view using an exposed form. | |
* - $sort_order: The select box with the ASC, DESC options to define order. May be optional. | |
* - $items_per_page: The select box with the available items per page. May be optional. | |
* - $offset: A textfield to define the offset of the view. May be optional. | |
* - $reset_button: A button to reset the exposed filter applied. May be optional. | |
* - $button: The submit button for the form. | |
* | |
* @ingroup views_templates | |
*/ | |
?> | |
<?php if (!empty($q)): ?> | |
<?php | |
// This ensures that, if clean URLs are off, the 'q' is added first so that | |
// it shows up first in the URL. | |
print $q; | |
?> | |
<?php endif; ?> | |
<div class="views-exposed-form"> | |
<div class="views-exposed-widgets clearfix"> | |
<?php foreach ($widgets as $id => $widget): ?> | |
<div id="<?php print $widget->id; ?>-wrapper" class="views-exposed-widget views-widget-<?php print $id; ?>"> | |
<?php if (!empty($widget->label)): ?> | |
<div class="filter-block--wrapper"> | |
<label for="<?php print $widget->id; ?>"> | |
<span class="arrows filter-label"> | |
<?php print $widget->label; ?> | |
</span> | |
<?php if (!empty($widget->description)): ?> | |
<span class="arrows filter-tip guarantee-block__item__text__link" data-toggle="tooltip" data-placement="top" title="<p class='filter-tip__descr'><?php print $widget->description; ?></p>"></span> | |
<?php endif; ?> | |
</label> | |
<?php if (!empty($widget->operator)): ?> | |
<div class="views-operator"> | |
<?php print $widget->operator; ?> | |
</div> | |
<?php endif; ?> | |
<div class="views-widget"> | |
<?php print $widget->widget; ?> | |
</div> | |
</div> | |
<?php endif; ?> | |
</div> | |
<?php endforeach; ?> | |
<?php if (!empty($sorting)): ?> | |
<div class="views-exposed-widget views-widget-sort-by views-widget-sort-sort_bef_combine"> | |
<?php print $sorting; ?> | |
</div> | |
<?php endif; ?> | |
<?php if (!empty($items_per_page)): ?> | |
<div class="views-exposed-widgets views-widget-per-page"> | |
<?php print $items_per_page; ?> | |
</div> | |
<?php endif; ?> | |
<!-- <?php if (!empty($sort_by)): ?> | |
<div class="views-exposed-widget views-widget-sort-by"> | |
<?php print $sort_by; ?> | |
</div> | |
<div class="views-exposed-widgets views-widget-sort-order"> | |
<?php print $sort_order; ?> | |
</div> | |
<?php endif; ?> --> | |
<?php if (!empty($offset)): ?> | |
<div class="views-exposed-widget views-widget-offset"> | |
<?php print $offset; ?> | |
</div> | |
<?php endif; ?> | |
<div class="views-exposed-widgets exp-but views-submit-button"> | |
<?php print $button; ?> | |
</div> | |
<?php if (!empty($reset_button)): ?> | |
<div class="views-exposed-widgets exp-but views-reset-button"> | |
<?php print $reset_button; ?> | |
</div> | |
<?php endif; ?> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment