Skip to content

Instantly share code, notes, and snippets.

@timonweb
Last active December 20, 2015 12:39
Show Gist options
  • Select an option

  • Save timonweb/6133237 to your computer and use it in GitHub Desktop.

Select an option

Save timonweb/6133237 to your computer and use it in GitHub Desktop.
Truncate string on a word boundary
Views Exposed Forms Ajax Support
================================
Adds #ajax support to Views Exposed Forms.
Since Views Exposed Forms are $_GET forms only, #ajax callbacks won't work there by default.
This module adds this ability. Just enable it and create your #ajax callbacks for any exposed form
element.
This module is the adopted version of the patch here: https://drupal.org/node/1183418, hopefully it will
be submitted to Views soon and you won't need this module anymore, but for now it will save you.
def smart_truncate(content, length=100, suffix='...'):
if len(content) <= length:
return content
else:
return ' '.join(content[:length+1].split(' ')[0:-1]) + suffixes
name = Views Exposed Forms Ajax Support
description = Adds Ajax Support to Views Exposed Forms
dependencies[] = views
package = views
core = 7.x
<?php
/**
* @file vef_ajax.module
* TODO: Enter file description here.
*/
/**
* Implement hook_form_alter for the exposed form.
*
* Since the exposed form is a GET form, we don't want it to send a wide
* variety of information.
*/
function vef_ajax_form_views_exposed_form_alter(&$form, &$form_state) {
$form['form_build_id']['#access'] = FALSE;
$form['form_token']['#access'] = FALSE;
$form['form_id']['#access'] = FALSE;
// AJAX behaviors need these data, so we add it back in #after_build.
$form['#after_build'][] = 'vef_ajax_exposed_form_ajax_enable';
}
/**
* Checks whether the exposed form will use ajax and passes required
* form information removed in views_form_views_exposed_form_alter().
*/
function vef_ajax_exposed_form_ajax_enable(&$form, &$form_state) {
// In order for Ajax to work, we need the form build info. Here we
// check if #ajax has been added to any form elements, and if so,
// pass this info as settings via Javascript, which get attached to
// the submitted form on Ajax form submissions.
// #ajax property can be set not only for the first level of the form,
// so we look for it in the whole form.
$ajax_info = array();
$ajax_elements = vef_ajax_exposed_form_ajax_lookup_recursive($form);
// Determine if form is being processed.
$triggering_element_name = '';
if (!empty($form_state['input']['_triggering_element_name'])) {
$triggering_element_name = $form_state['input']['_triggering_element_name'];
}
// When we have multiple elements with #ajax set on exposed form
// we need to pass only triggering element name to Javascript.
// Otherwise #ajax will work only for the first element.
if (!empty($triggering_element_name) && !empty($ajax_elements)) {
// Check if element has #ajax property set.
if (in_array($triggering_element_name, $ajax_elements)) {
$ajax_elements = array(
$triggering_element_name => $triggering_element_name,
);
}
else {
$ajax_elements = array();
}
}
if (!empty($ajax_elements)) {
$form_info = array(
'form_id' => $form['#form_id'],
'form_build_id' => $form['#build_id'],
);
// Anonymous users don't get a token.
if (!empty($form['#token'])) {
$form_info['form_token'] = $form['#token'];
}
foreach ($ajax_elements as $element_name) {
$ajax_info[$element_name] = $form_info;
}
$form['#attached']['js'][] = array(
'type' => 'setting',
'data' => array(
'ViewsExposedFormInfo' => $ajax_info,
),
);
// Add the javascript behavior that will handle this data.
$form['#attached']['js'][] = array(
'weight' => 100,
'data' => drupal_get_path('module', 'vef_ajax') . '/js/exposed-form-ajax.js',
);
}
return $form;
}
/**
* Recursively looks for the #ajax property for every form elemet.
* @param $elements
* The element array to look for #ajax property.
*
* @return
* Array of the elements names where #ajax was found.
*/
function vef_ajax_exposed_form_ajax_lookup_recursive($elements) {
$ajax_elements = array();
foreach (element_children($elements) as $key) {
if (!empty($elements[$key]['#name']) && !empty($elements[$key]['#ajax'])) {
$ajax_elements[$elements[$key]['#name']] = $elements[$key]['#name'];
}
// Recursive call to look for #ajax in element's childrens.
$ajax_elements += vef_ajax_exposed_form_ajax_lookup_recursive($elements[$key]);
}
return $ajax_elements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment