Skip to content
semanticviews.module 4.99 KiB
Newer Older
<?php
// $Id$
/**
 * @file semanticviews.module
 * TODO: Enter file description here.
 */

/**
 *  Implementation of hook_views_api().
 */
function semanticviews_views_api() {
  return array(
    'api' => 2.0,
  );
}

/**
 * Preprocess theme function to print a single record from a row, with fields
 */
function template_preprocess_semanticviews_view_fields(&$variables) {
  $view = $variables['view'];
  // Loop through the fields for this view.
  $variables['fields'] = array(); // ensure it's at least an empty array.
  foreach ($view->field as $id => $field) {
    // render this even if set to exclude so it can be used elsewhere.
    $field_output = $view->field[$id]->theme($variables['row']);
    if (empty($field->options['exclude'])) {
      $object = new stdClass();

      $object->content = $field_output;
      if (isset($view->field[$id]->field_alias) && isset($variables['row']->{$view->field[$id]->field_alias})) {
        $object->raw = $variables['row']->{$view->field[$id]->field_alias};
      }
      else {
        $object->raw = NULL; // make sure it exists to reduce NOTICE
      }

      $object->handler = &$view->field[$id];

      $semantic_html = $variables['options']['semantic_html'][$id];
      $object->element_type = check_plain($semantic_html['element_type']);
      $object->attributes = array();
      if ($semantic_html['class']) {
        $object->attributes['class'] = $semantic_html['class'];
      }

      $object->label = check_plain($view->field[$id]->label());
      $variables['fields'][$id] = $object;
    }
  }
}

/**
 * Display the simple view of rows one after another
 */
function template_preprocess_semanticviews_view_unformatted(&$variables) {
  $variables['group_element'] = check_plain($variables['options']['group']['element_type']);
  $variables['group_attributes'] = array();
  if ($variables['options']['group']['class']) {
    $variables['group_attributes']['class'] = $variables['options']['group']['class'];
  }

  // TODO: set a default or handle empty value.
  $variables['row_element'] = check_plain($variables['options']['row']['element_type']);
  $last_every_nth = $variables['options']['last_every_nth'];

  $variables['row_attributes'] = array();
  foreach ($variables['rows'] as $id => $row) {
    $variables['row_attributes'][$id] = array();
    $classes = array();
    if ($variables['options']['row']['class']) {
      $classes[] = str_replace('#', $id, $variables['options']['row']['class']);
    }
    if ($id == 0 && $variables['options']['row']['first_class']) {
      $classes[] = $variables['options']['row']['first_class'];
    }
    if (($last_every_nth && ($id + 1) % $last_every_nth == 0) || 
       (!$last_every_nth && $id == count($variables['rows']))) {
      $classes[] = $variables['options']['row']['last_class'];
    }
    // TODO: implement configurable row striping
    //$classes[] = $id % 2 ? 'even' : 'odd';

    if (!empty($classes)) {
      $variables['row_attributes'][$id]['class'] = implode(' ', $classes);
    }
  }
}

function semanticviews_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['views_view'])) {
    
    // Add the module's path to the theme paths.
    if (isset($theme_registry['views_view']['theme paths'])) {
      $module_path = drupal_get_path('module', 'semanticviews');
      array_push($theme_registry['views_view']['theme paths'], $module_path);
    }

    // Add an additional preprocess function to the theme info.
    if (isset($theme_registry['views_view']['preprocess functions'])) {
      $module_path = drupal_get_path('module', 'semanticviews');
      array_push($theme_registry['views_view']['preprocess functions'], 'template_preprocess_semanticviews_view');
    }
  }
}

/**
 * The class attributes of the outer div of a View cannot be changed when the
 * view uses AJAX. The Views ajax scripts need these classes to attach
 * behaviors.
 *   - view-dom-id-%
 *   OR
 *   - view-id-% and view-display-id-%
 * 
 * @see template_preprocess_views_view
 * @see ajax_view.js
 *
 * @param <type> $variables
 */
function template_preprocess_semanticviews_view(&$variables) {
  if ($variables['view']->plugin_name == 'semanticviews_default') {
    $variables['template_file'] = 'semanticviews-view';
    $variables['attributes'] = array();

    // Container class attributes are stacked in an array.
    $classes = array();

    // TODO: Bring class attribute into UI for configuration.
    //$classes[] = 'view';
    if ($variables['view']->use_ajax) {

      // The old way Ajax Views were selected for behaviors.
      //$classes[] = 'view-'. $variables['css_name'];
      //$classes[] = 'view-id-'. $variables['name'];
      //$classes[] = 'view-display-id-'. $variables['display_id'];

      // The new way.
      $classes[] = 'view-dom-id-'. $variables['dom_id'];
    }
    if (!empty($classes)) {
      $variables['attributes']['class'] = implode(' ', $classes);
    }
    if ($variables['view']->style_options['id']) {
      // TODO: Prevent ID collision. Or not?
      $variables['attributes']['id'] = $variables['view']->style_options['id'];
    }
  }
}