You are here

function responsive_tables_filter_preprocess_page in Responsive Tables Filter 7

Implements hook_preprocess_page().

Adds tablesaw JS when tables present and "every_page" is off.

File

./responsive_tables_filter.module, line 149
Make tables responsive, when filter is enabled for the field.

Code

function responsive_tables_filter_preprocess_page(&$variables) {

  // Only execute this preprocess logic if every page loading is off and the
  // page request is for the default theme.
  if (_responsive_tables_filter_is_theme_default() && isset($variables['node']) && !variable_get(RESPONSIVE_TABLES_FILTER_EVERY_PAGE, FALSE)) {
    $formats_with_tablesaw = array();

    // Get text formats that have "tablesaw filter" enabled.
    $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'];

    // Switch to workbench moderation element if present.
    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;

      // Add the required JS only if the following 3 conditions are met:
      // (1) The field must have a text format value
      // (2) The field format must have tablesaw filter enabled
      // (3) The field value must contain '<table'.
      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);

        // Quit searching once a field matching the criteria is found.
        break;
      }
    }
  }
}