You are here

protected static function PartyPrimaryFields::buildFields in Party 7

Build the primary field information.

Parameters

bool $reset: Whether the cache should be reset.

1 call to PartyPrimaryFields::buildFields()
PartyPrimaryFields::getFields in includes/party.primary_fields.inc
Get hold of primary field information.

File

includes/party.primary_fields.inc, line 608
Primary field related functions and callbacks.

Class

PartyPrimaryFields
Helper class for primary fields.

Code

protected static function buildFields($reset = FALSE) {

  // If we are resetting, clear our static cache.
  if ($reset) {
    self::$fields = NULL;
  }
  else {
    if ($cache = cache_get('party:primary_fields:fields')) {
      self::$fields = $cache->data;
    }
  }

  // If we have no information, build it.
  if (!isset(self::$fields)) {

    // Label and email are provided by custom UIs and stored in a variable which
    // other modules could potentially add to.
    self::$fields = variable_get('party_primary_fields', array());

    // Retrieve from primary field widgets.
    foreach (field_info_instances('party', 'party') as $instance) {
      if ($instance['widget']['type'] == 'party_primary_field') {
        self::$fields[$instance['field_name']] = $instance['widget']['settings']['sources'];
      }
    }

    // Allow other modules to alter it.
    drupal_alter('party_primary_fields_fields', self::$fields);

    // Remove any invalid targets.
    $party_property_info = self::getPropertyInfo('party', 'party');
    foreach (array_keys(self::$fields) as $target) {
      if (!isset($party_property_info[$target])) {

        // Remove and log a watchdog warning.
        watchdog('party', 'The target for the primary field %target does not exist.', array(
          '%target' => $target,
        ), WATCHDOG_WARNING);
        unset(self::$fields[$target]);
      }
    }

    // Make sure the sources are valid and sorted by weight.
    $valid_sources = self::getSources();
    foreach (self::$fields as $target => &$sources) {

      // If we don't have any sources, skip it all.
      if (!is_array($sources)) {
        unset(self::$fields[$target]);
        continue;
      }

      // Remove any invalid sources.
      foreach ($sources as $key => &$source) {

        // Make sure our sources have all the keys.
        $source += array(
          'data_set' => NULL,
          'property' => NULL,
          'value' => NULL,
          'callback' => NULL,
          'weight' => 0,
        );

        // Check that the data set exists.
        if (!isset($valid_sources[$source['data_set']])) {
          watchdog('party', 'The source data set %data_set for the primary field %target does not exist.', array(
            '%data_set' => $source['data_set'],
            '%target' => $target,
          ), WATCHDOG_WARNING);
          unset($sources[$key]);
          continue;
        }

        // Check that the property exists.
        $source_key = self::getSourceKey($source, TRUE);
        if (!isset($valid_sources[$source['data_set']]['sources'][$source_key])) {
          watchdog('party', 'The source property %key for the primary field %target does not exist.', array(
            '%key' => $key,
            '%target' => $target,
          ), WATCHDOG_WARNING);
          unset($sources[$key]);
          continue;
        }
        $source_key = self::getSourceKey($source);
        if (!isset($valid_sources[$source['data_set']]['sources'][$source_key])) {
          watchdog('party', 'The source property %key for the primary field %target does not exist. Falling back to %property.', array(
            '%key' => $key,
            '%target' => $target,
            '%property' => $source['property'],
          ), WATCHDOG_WARNING);
          $sources[$key]['value'] = NULL;
          continue;
        }
      }

      // Sort the sources.
      uasort($sources, 'drupal_sort_weight');
    }
  }

  // Store our information to the cache.
  cache_set('party:primary_fields:fields', self::$fields);
}