You are here

function party_find_fields_of_types in Party 7

Same name and namespace in other branches
  1. 8.2 party.module \party_find_fields_of_types()

Find all columns of particular types on parties and their data sets.

Parameters

array|string $types: Either a single or an array of possible schema types.

bool $reset: Whether to rebuild the information.

Return value

array Nested arrays of possible columns suitable for #options. The top level key is the data set label and the child arrays are of the format:

  • keys: data_set_name:field_name:column where field_name is empty for properties of an entity.
  • values: depending on whether a property or field
    • property_label (column)
    • field_label (field_name:column)

See also

http://api.drupal.org/api/drupal/includes!database!schema.inc/group/sche...

1 call to party_find_fields_of_types()
party_user_settings_form in modules/party_user/party_user.admin.inc
User integration settings form.

File

./party.module, line 1547
Provides a generic CRM party entity.

Code

function party_find_fields_of_types($types, $reset = FALSE) {

  // Get hold of our cached data.
  $types = drupal_map_assoc((array) $types);
  $hash_key = md5(serialize($types));
  $cache =& drupal_static(__FUNCTION__, array());

  // Check whether we need to build the information.
  if ($reset || !isset($cache[$hash_key])) {

    // Make sure we have an empty array to add to.
    $cache[$hash_key] = array();
    $options =& $cache[$hash_key];

    // Iterate over our data sets finding all the fields that are relevant.
    foreach (party_get_data_set_info() as $data_set_name => $set_info) {

      // Get our set key which is the label for the option group.
      $set_key = format_string('@label (@name)', array(
        '@label' => $set_info['label'],
        '@name' => $data_set_name,
      ));
      $options[$set_key] = array();

      // Add all of the properties of this entity.
      $entity_info = entity_get_info($set_info['entity type']);
      $schema = drupal_get_schema($entity_info['base table']);
      $property_info = entity_get_all_property_info($set_info['entity type']);
      foreach ($schema['fields'] as $column => $definition) {

        // Check whether this matches our allowed types.
        if (in_array($definition['type'], $types)) {

          // Build our key - data_set_name:field_name:column.
          $property_key = $set_info['set_name'] . '::' . $column;
          $options[$set_key][$property_key] = format_string('@label (@column)', array(
            '@label' => isset($property_info[$column]['label']) ? $property_info[$column]['label'] : $column,
            '@column' => $column,
          ));
        }
      }

      // Get hold of our fields and iterate over them adding them to our options.
      $fields = field_info_instances($set_info['entity type'], $set_info['entity bundle']);
      foreach ($fields as $field) {

        // Get hold of field info so we can check out the columns.
        $field_info = field_info_field($field['field_name']);
        foreach ($field_info['columns'] as $column => $definition) {

          // Check whether this matches our allowed types.
          if (in_array($definition['type'], $types)) {

            // Build our key - data_set_name:field_name:column.
            $field_key = $set_info['set_name'] . ':' . $field['field_name'] . ':' . $column;
            $options[$set_key][$field_key] = format_string('@label (@name:@column)', array(
              '@label' => $field['label'],
              '@name' => $field['field_name'],
              '@column' => $column,
            ));
          }
        }
      }
    }
  }
  return $cache[$hash_key];
}