You are here

function noderelationships_get_relationships in Node Relationships 6

Build the list of node relationships on the system.

This function is basically the heart of this module. We build the list of all relationed content types by parsing all nodereference fields.

Non-standard requirement: The 'referenceable_types' attribute of nodereference fields must be populated explicitly by the user, regardless of the use of views for nodereference widgets.

We cannot use content_types() because we could be invoked while the data content_types() generates is being computed. Note that noderelationships_content_extra_fields() is invoked by content_types() to process hook_content_extra_fields() and noderelationships_content_extra_fields() needs the information we generate here. So we read field settings directly from database using the same method as content_field_instance_read(), just focussing on data we're interested in.

4 calls to noderelationships_get_relationships()
noderelationships_erd_diagram in ./noderelationships.admin.inc
Build the Entity Relationship Diagram (ERD).
noderelationships_get_referred_types in ./noderelationships.inc
Get the list of referred types for the given referrer type.
noderelationships_get_referrer_types in ./noderelationships.inc
Get the list of referrer types for the given referred type.
noderelationships_get_relationed_types in ./noderelationships.inc
Get the list of all content types that have some kind of relation.

File

./noderelationships.inc, line 363
Common functions for the noderelationships module.

Code

function noderelationships_get_relationships($reset = FALSE) {
  static $relationships;

  // Provide the data from static or cached storage when possible.
  if (!$reset) {

    // Do we have static data?
    if (isset($relationships)) {
      return $relationships;
    }

    // Do we have cached data?
    if ($cached = cache_get('noderelationships_relationships', content_cache_tablename())) {
      $relationships = $cached->data;
      return $relationships;
    }
  }

  // Build the relationships structure.
  $relationships = array(
    'referrer' => array(),
    'referred' => array(),
    'nodereferences' => noderelationships_get_nodereferences(TRUE),
  );
  foreach ($relationships['nodereferences'] as $field_name => $field_instances) {
    foreach ($field_instances as $referrer_type => $referenceable_types) {

      // Build the list of referrers by child type and field name.
      if (!isset($relationships['referrer'][$referrer_type])) {
        $relationships['referrer'][$referrer_type] = array(
          'referred_types' => array(),
          'fields' => array(
            $field_name => array(),
          ),
        );
      }
      foreach ($referenceable_types as $referred_type) {
        if (!isset($relationships['referrer'][$referrer_type]['referred_types'][$referred_type])) {
          $relationships['referrer'][$referrer_type]['referred_types'][$referred_type] = array();
        }
        $relationships['referrer'][$referrer_type]['referred_types'][$referred_type][] = $field_name;
        if (!isset($relationships['referrer'][$referrer_type]['fields'][$field_name])) {
          $relationships['referrer'][$referrer_type]['fields'][$field_name] = array();
        }
        $relationships['referrer'][$referrer_type]['fields'][$field_name][] = $referred_type;

        // Build the list of referred types by parent type and field name.
        if (!isset($relationships['referred'][$referred_type])) {
          $relationships['referred'][$referred_type] = array(
            'referrer_types' => array(),
            'fields' => array(),
          );
        }
        if (!isset($relationships['referred'][$referred_type]['referrer_types'][$referrer_type])) {
          $relationships['referred'][$referred_type]['referrer_types'][$referrer_type] = array();
        }
        $relationships['referred'][$referred_type]['referrer_types'][$referrer_type][] = $field_name;
        if (!isset($relationships['referred'][$referred_type]['fields'][$field_name])) {
          $relationships['referred'][$referred_type]['fields'][$field_name] = array();
        }
        $relationships['referred'][$referred_type]['fields'][$field_name][] = $referrer_type;
      }
    }
  }

  // Sort phase.
  foreach (array_keys($relationships['referrer']) as $referrer_type) {
    ksort($relationships['referrer'][$referrer_type]['fields']);
    ksort($relationships['referrer'][$referrer_type]['referred_types']);
  }
  ksort($relationships['referrer']);
  foreach (array_keys($relationships['referred']) as $referred_type) {
    ksort($relationships['referred'][$referred_type]['fields']);
    ksort($relationships['referred'][$referred_type]['referrer_types']);
  }
  ksort($relationships['referred']);

  // Cache a copy of the relationships structure.
  cache_set('noderelationships_relationships', $relationships, content_cache_tablename());
  return $relationships;
}