View source
<?php
define('RESPONSIVE_TABLES_FILTER_VIEWS', 'responsive_tables_filter_views');
define('RESPONSIVE_TABLES_FILTER_ADMIN', 'responsive_tables_filter_admin');
define('RESPONSIVE_TABLES_FILTER_FILES', 'responsive_tables_filter_files');
define('RESPONSIVE_TABLES_FILTER_EVERY_PAGE', 'responsive_tables_filter_every_page');
define('RESPONSIVE_TABLES_FILTER_CUSTOM_CSS', 'responsive_tables_filter_custom_css');
define('RESPONSIVE_TABLES_FILTER_ADMIN_PATH', 'admin/config/content/responsive_tables_filter');
function responsive_tables_filter_menu() {
return array(
RESPONSIVE_TABLES_FILTER_ADMIN_PATH => array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'responsive_tables_filter_admin_form',
),
'access arguments' => array(
'administer views',
),
'title' => 'Responsive Tables',
'description' => 'Configure which tables should be responsive',
'file' => 'responsive_tables_filter.admin.inc',
),
);
}
function responsive_tables_filter_init() {
if (_responsive_tables_filter_is_theme_default() && variable_get(RESPONSIVE_TABLES_FILTER_EVERY_PAGE, FALSE)) {
_responsive_tables_filter_add_js_css();
}
}
function responsive_tables_filter_filter_info() {
$filters = array();
$filters['tablesaw'] = array(
'title' => t('Make tables responsive'),
'process callback' => '_tablesaw_filter',
);
return $filters;
}
function _tablesaw_filter($text, $filter, $format, $langcode, $cache, $cache_id) {
static $new_libxml;
if (!isset($new_libxml)) {
$new_libxml = defined(LIBXML_VERSION) && LIBXML_VERSION >= 20708;
}
if ($text != '') {
$tables = array();
libxml_use_internal_errors(TRUE);
$rooted_text = '<root>' . $text . '</root>';
$dom = new DOMDocument();
if ($new_libxml) {
$dom
->loadHTML(mb_convert_encoding($rooted_text, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
}
else {
$dom
->loadHTML(mb_convert_encoding($rooted_text, 'HTML-ENTITIES', 'UTF-8'));
}
$query = variable_get('responsive_tables_filter_table_xpath', FALSE);
if ($query) {
$xpath = new DOMXPath($dom);
$tables = $xpath
->query($query);
}
else {
$tables = $dom
->getElementsByTagName('table');
}
if ($tables->length !== 0) {
foreach ($tables as $table) {
$existing_classes = $table
->getAttribute('class');
if (strpos($existing_classes, 'no-tablesaw') === FALSE) {
$new_classes = !empty($existing_classes) ? $existing_classes . ' tablesaw tablesaw-stack' : 'tablesaw tablesaw-stack';
$table
->setAttribute('class', $new_classes);
$table
->setAttribute('data-tablesaw-mode', 'stack');
}
}
$html = "";
foreach ($dom
->getElementsByTagName('root')
->item(0)->childNodes as $child) {
$html .= $dom
->saveHTML($child);
}
if (!$new_libxml && strpos($html, '<html><body>') != FALSE) {
$html_start = strpos($html, '<html><body>') + 12;
$html_length = strpos($html, '</body></html>') - $html_start;
$html = substr($html, $html_start, $html_length);
}
return $html;
}
}
return $text;
}
function responsive_tables_filter_preprocess_page(&$variables) {
if (_responsive_tables_filter_is_theme_default() && isset($variables['node']) && !variable_get(RESPONSIVE_TABLES_FILTER_EVERY_PAGE, FALSE)) {
$formats_with_tablesaw = array();
$result = db_query('SELECT n.format FROM {filter} n WHERE n.module = :module AND n.status = 1', array(
':module' => 'responsive_tables_filter',
));
foreach ($result as $record) {
$formats_with_tablesaw[] = $record->format;
}
array_unique($formats_with_tablesaw);
$node = $variables['node'];
if (isset($node->workbench_moderation['current']) && !isset($node->entity_view_prepared)) {
$node = workbench_moderation_node_current_load($node);
}
$field_info = field_info_instances('node', $node->type);
$fields = array_keys($field_info);
foreach ($fields as $field) {
$lang = field_language('node', $node, $field);
$f = $field;
if (isset($node->{$f}[$lang][0]['format']) && in_array($node->{$f}[$lang][0]['format'], $formats_with_tablesaw) && strpos($node->{$f}[$lang][0]['value'], '<table') !== FALSE) {
_responsive_tables_filter_add_js_css(FALSE);
break;
}
}
}
}
function responsive_tables_filter_preprocess_field(&$variables) {
$field_type = $variables['element']['#field_type'];
switch ($field_type) {
case 'tablefield':
if (isset($variables['items'])) {
foreach ($variables['items'] as &$item) {
$item['#attributes']['class'][] = 'tablesaw';
$item['#attributes']['class'][] = 'tablesaw-stack';
$item['#attributes']['data-tablesaw-mode'] = 'stack';
}
}
break;
}
}
function responsive_tables_filter_field_formatter_info_alter(&$info) {
if (variable_get(RESPONSIVE_TABLES_FILTER_FILES, TRUE)) {
$info['file_table']['module'] = 'responsive_tables_filter';
}
}
function responsive_tables_filter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = file_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display);
$element[0]['#theme'] = 'file_formatter_responsive_table';
return $element;
}
function responsive_tables_filter_theme() {
return array(
'file_formatter_responsive_table' => array(
'variables' => array(
'items' => NULL,
),
),
);
}
function theme_file_formatter_responsive_table(array $variables) {
if (!variable_get(RESPONSIVE_TABLES_FILTER_EVERY_PAGE, FALSE)) {
_responsive_tables_filter_add_js_css();
}
$header = array(
t('Attachment'),
t('Size'),
);
$rows = array();
foreach ($variables['items'] as $delta => $item) {
$rows[] = array(
theme('file_link', array(
'file' => (object) $item,
)),
format_size($item['filesize']),
);
}
$attributes = array(
'class' => array(
'tablesaw',
'tablesaw-stack',
),
'data-tablesaw-mode' => 'stack',
);
return empty($rows) ? '' : theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => $attributes,
));
}
function responsive_tables_filter_preprocess_views_view_table(&$variables) {
if ((_responsive_tables_filter_is_theme_default() || variable_get(RESPONSIVE_TABLES_FILTER_ADMIN, TRUE)) && variable_get(RESPONSIVE_TABLES_FILTER_VIEWS, TRUE)) {
$variables['classes_array'][] = 'tablesaw';
$variables['classes_array'][] = 'tablesaw-stack';
$variables['attributes_array']['data-tablesaw-mode'] = 'stack';
if (!variable_get(RESPONSIVE_TABLES_FILTER_EVERY_PAGE, FALSE)) {
_responsive_tables_filter_add_js_css();
}
}
}
function _responsive_tables_filter_is_theme_default() {
global $theme;
$theme_default = variable_get('theme_default', FALSE);
return $theme == $theme_default;
}
function _responsive_tables_filter_add_js_css() {
$every_page = variable_get(RESPONSIVE_TABLES_FILTER_EVERY_PAGE, FALSE);
$module_path = drupal_get_path('module', 'responsive_tables_filter');
$custom = variable_get(RESPONSIVE_TABLES_FILTER_CUSTOM_CSS, '');
if (!empty($custom)) {
drupal_add_css($custom, array(
'every_page' => $every_page,
));
}
else {
drupal_add_css($module_path . '/tablesaw/css/tablesaw.stackonly-base.css', [
'every_page' => $every_page,
'weight' => 31,
]);
drupal_add_css($module_path . '/tablesaw/css/tablesaw.stackonly-responsive.css', [
'every_page' => $every_page,
'media' => 'screen',
'weight' => 32,
]);
}
drupal_add_js($module_path . '/tablesaw/js/tablesaw.stackonly.jquery.js', array(
'every_page' => $every_page,
'scope' => 'footer',
'weight' => 30,
));
drupal_add_js($module_path . '/tablesaw/js/tablesaw-init.js', array(
'every_page' => $every_page,
'scope' => 'footer',
'weight' => 31,
));
}