You are here

domain_views.module in Domain Access 5

Same filename and directory in other branches
  1. 6.2 domain_views/domain_views.module

Provides a Views filter for the Domain Access module.

Original code by mrichar1 see http://drupal.org/node/200714

@in

Implement hook_views_tables(). Provide a filter that restricts a view to only show nodes from a specific domain (by domain_id). Relevant for authenticated users who have superuser permissions and can therefore see all nodes.

File

domain_views/domain_views.module
View source
<?php

/**
 * @defgroup domain_views Domain Views: provides a views filter
 *
 * Creates unique subdomains for registered users.
 */

/**
* @file
* Provides a Views filter for the Domain Access module.
*
* Original code by mrichar1 see http://drupal.org/node/200714
*
* @in

/**
* Implement hook_views_tables().
* Provide a filter that restricts a view to only show nodes from a specific domain (by domain_id).
* Relevant for authenticated users who have superuser permissions and can therefore see all nodes.
*/
function domain_views_tables() {
  $domains = domain_domains();
  $domain_options = array(
    '-1' => t('Any domain -- use with "Is not one of"'),
    '***CURRENT_DOMAIN***' => t('Current Domain'),
  );
  foreach ($domains as $domain) {
    $domain_options[$domain['domain_id']] = $domain['sitename'];
  }
  $domain_form = array(
    '#type' => 'select',
    '#options' => $domain_options,
    '#multiple' => TRUE,
    '#size' => 'size="' . min(8, count($domain_options)) . '"',
  );
  $tables['domain_access'] = array(
    'name' => 'node_access',
    'join' => array(
      'left' => array(
        'table' => 'node',
        'field' => 'nid',
      ),
      'right' => array(
        'field' => 'nid',
      ),
      'extra' => array(
        'realm' => 'domain_id',
      ),
    ),
    'filters' => array(
      'gid' => array(
        'name' => t('Domain Access: Domain'),
        'value' => $domain_form,
        'value-type' => 'array',
        'operator' => 'views_handler_operator_or',
        'help' => t('This will filter a view to only show nodes from the specified domain.'),
      ),
    ),
  );
  $tables['domain'] = array(
    'name' => 'domain',
    'join' => array(
      'left' => array(
        'table' => 'domain_access',
        'field' => 'gid',
      ),
      'right' => array(
        'field' => 'domain_id',
      ),
      'extra' => array(
        'valid' => '1',
      ),
    ),
  );
  return $tables;
}

/**
 * Implement hook_views_arguments().
 * Provide a filter that restricts a view to only show nodes from a specific domain (by domain_id).
 * If you pass "current" as the argument, it filters according to the domain the page is being viewed from.
 */
function domain_views_arguments() {
  $arguments = array(
    'domain_access' => array(
      'name' => t('Domain Access: Domain'),
      'handler' => 'domain_views_handler_arg_domain_id',
      // This is the function that handles all the work
      'help' => t('The argument will filter a view to only show nodes from the specified domain.'),
    ),
  );
  return $arguments;
}

/**
 * This is the function that handles all the work for dealing with view arguments
 * There is some *very rough* documentation on argument handlers for views here http://drupal.org/node/99566.
 */
function domain_views_handler_arg_domain_id($op, &$query, $argtype, $arg = '') {
  switch ($op) {
    case 'summary':
      $query
        ->add_table('domain_access');

      // Re-uses the node_access table info from the domain_views_tables() function
      $query
        ->add_table('domain');

      // Re-uses the domain table info from the domain_views_tables() function
      $query
        ->add_field('sitename', 'domain');
      $query
        ->add_field('domain_id', 'domain');
      $query
        ->add_where("domain.valid = '%s'", 1);

      // don't show summary item for inactive domains
      $fieldinfo['field'] = "domain.domain_id";
      return $fieldinfo;
    case 'sort':
      $query
        ->add_orderby('domain', 'sitename', $argtype);
      break;
    case 'filter':
      $domain_id = $arg == 'current' ? (int) $GLOBALS['_domain']['domain_id'] : (int) $arg;
      $query
        ->add_table('domain_access');
      $query
        ->add_where("domain_access.gid = '%s'", $domain_id);

      // domain_access is an alias for the node_access table
      $query
        ->set_distinct();
      break;
    case 'link':
      return l($query->sitename, "{$arg}/" . intval($query->domain_id));
    case 'title':
      return '';
  }
}

/**
 * Substitute current domain; this works with cached queries.
 */
function domain_views_query_substitutions($view) {
  return array(
    '***CURRENT_DOMAIN***' => $GLOBALS['_domain']['domain_id'],
  );
}

Functions

Namesort descending Description
domain_views_arguments Implement hook_views_arguments(). Provide a filter that restricts a view to only show nodes from a specific domain (by domain_id). If you pass "current" as the argument, it filters according to the domain the page is being viewed from.
domain_views_handler_arg_domain_id This is the function that handles all the work for dealing with view arguments There is some *very rough* documentation on argument handlers for views here http://drupal.org/node/99566.
domain_views_query_substitutions Substitute current domain; this works with cached queries.
domain_views_tables @file Provides a Views filter for the Domain Access module.