You are here

domain_integration.module in Domain Integration (Drupal 7) 7

Same filename and directory in other branches
  1. 8 domain_integration.module

Domain Integration.

File

domain_integration.module
View source
<?php

/**
 * @file
 * Domain Integration.
 */

/**
 * Implements hook_entity_property_info_alter().
 *
 * Adds domain access entity properties for node, user and site.
 *
 * Available domain-properties are (@see domain_load($domain_id)):
 *  - domain_id (returns a list of integers)
 *  - subdomain (returns a list of text)
 *  - sitename  (returns a list of text)
 *  - scheme (* not implemented *)
 *  - valid (* not implemented *)
 *  - weight (* not implemented *)
 *  - is_default (* not implemented *)
 *  - machine_name (returns a list of text)
 *  - path (returns a list of text)
 *  - site_grant (* not implemented *)
 */
function domain_integration_entity_property_info_alter(&$info) {

  // Domain properties.
  $domain_properties = array();
  $domain_properties['domain_domain_id'] = array(
    'label' => t('Domain ID'),
    'description' => t("A list of Domain ID's."),
    'type' => 'list<integer>',
    'getter callback' => 'domain_integration_get_domain_info',
    'entity views field' => TRUE,
  );
  $domain_properties['domain_subdomain'] = array(
    'label' => t('Domain Subdomain'),
    'description' => t("A list of Domain Subdomains (full url)."),
    'type' => 'list<text>',
    'getter callback' => 'domain_integration_get_domain_info',
    'entity views field' => TRUE,
  );
  $domain_properties['domain_sitename'] = array(
    'label' => t('Domain Sitename'),
    'description' => t("A list of Domain Sitenames (readable, perfect for search filters)."),
    'type' => 'list<text>',
    'getter callback' => 'domain_integration_get_domain_info',
    'entity views field' => TRUE,
  );
  $domain_properties['domain_machine_name'] = array(
    'label' => t('Domain Machine name'),
    'description' => t("A list of Domain Machine names (perfect for string comparisons)."),
    'type' => 'list<text>',
    'getter callback' => 'domain_integration_get_domain_info',
    'entity views field' => TRUE,
  );
  $domain_properties['domain_path'] = array(
    'label' => t('Domain Path'),
    'description' => t("A list of Domain Paths (full url including http://)."),
    'type' => 'list<text>',
    'getter callback' => 'domain_integration_get_domain_info',
    'entity views field' => TRUE,
  );

  // Node Domain properties.
  $info['node']['properties'] = $info['node']['properties'] + $domain_properties;

  // Add 'send to all' property for nodes.
  $info['node']['properties']['domain_domain_site'] = array(
    'label' => t('Domain Send to All'),
    'description' => t('A boolean that is TRUE if Send to All is set'),
    'type' => 'boolean',
    'getter callback' => 'domain_integration_get_domain_info',
    'entity views field' => TRUE,
  );

  // User Domain properties.
  $info['user']['properties'] = $info['user']['properties'] + $domain_properties;

  // Site Domain properties.
  $info['site']['properties'] = $info['site']['properties'] + $domain_properties;
}

/**
 * Returns the domain property for an entity_type.
 *
 * @return array
 *   Array with domain properties (integer or string).
 *
 * @param string $name
 *   Parameter to return
 * @param string $entity_type
 *   Entity_type (implemented are 'site', 'node', 'user').
 *
 * @see domain_integration_entity_property_info_alter
 */
function domain_integration_get_domain_info($data, array $options, $name, $entity_type, $info) {

  // Property mapper (remove "domain_" from $name).
  $domain_target_property = substr_replace($name, "", 0, 7);

  // Fast forward if $entity_type is 'site'.
  if ($entity_type == 'site') {
    $domain = domain_get_domain();
    return array(
      $domain[$domain_target_property],
    );
  }

  // Property that contains domain information for 'real' entity_types.
  if ($entity_type == 'node') {
    $domain_property = 'domains';
  }
  elseif ($entity_type == 'user') {
    $domain_property = 'domain_user';
  }
  else {
    $domain_property = NULL;
  }

  // Extract and return data.
  $domains = array();

  // Check SendToAll
  if ($domain_target_property == 'domain_site') {
    return $data->domain_site;
  }

  // Domain properties.
  if (!empty($data->{$domain_property})) {
    foreach ($data->{$domain_property} as $domain_id) {
      $domain = domain_load($domain_id);
      if (isset($domain[$domain_target_property])) {
        $domains[] = $domain[$domain_target_property];
      }
    }
  }
  return $domains;
}

Functions

Namesort descending Description
domain_integration_entity_property_info_alter Implements hook_entity_property_info_alter().
domain_integration_get_domain_info Returns the domain property for an entity_type.