You are here

function mailchimp_lists_fieldmap_options in Mailchimp 7.4

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_fieldmap_options()
  2. 7.3 modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_fieldmap_options()

Get an array with all possible Drupal properties for a given entity type.

Parameters

string $entity_type: Name of entity whose properties to list.

string $entity_bundle: Optional bundle to limit available properties.

bool $required: Set to TRUE if properties are required.

string $prefix: Optional prefix for option IDs in the options list.

string $tree: Optional name of the parent element if this options list is part of a tree.

Return value

array List of properties that can be used as an #options list.

1 call to mailchimp_lists_fieldmap_options()
_mailchimp_lists_field_instance_settings_form_process in modules/mailchimp_lists/includes/mailchimp_lists.field.inc
Element processor. Expand the mergefields mapping form.

File

modules/mailchimp_lists/mailchimp_lists.module, line 642

Code

function mailchimp_lists_fieldmap_options($entity_type, $entity_bundle = NULL, $required = FALSE, $prefix = NULL, $tree = NULL) {
  $options = array();
  if (!$prefix) {
    $options[''] = t('-- Select --');
  }
  $properties = entity_get_all_property_info($entity_type);
  if (isset($entity_bundle)) {
    $info = entity_get_property_info($entity_type);
    $properties = $info['properties'];
    if (isset($info['bundles'][$entity_bundle])) {
      $properties += $info['bundles'][$entity_bundle]['properties'];
    }
  }
  foreach ($properties as $key => $property) {

    // The entity_token module uses - instead of _ for all property and field
    // names so we need to ensure that we match that pattern.
    $key = str_replace('_', '-', $key);

    // Include the entity type at the beginning of token names since tokens use
    // the format [entity_type:entity_property].
    $keypath = $prefix ? $prefix . ':' . $key : $entity_type . ':' . $key;
    $type = isset($property['type']) ? entity_property_extract_innermost_type($property['type']) : 'text';
    $is_entity = $type == 'entity' || (bool) entity_get_info($type);
    $required_property = isset($property['required']) ? $property['required'] : FALSE;
    $computed_property = isset($property['computed']) ? $property['computed'] : FALSE;
    if ($is_entity) {

      // We offer fields on related entities (useful for field collections).
      // But we only offer 1 level of depth to avoid loops.
      if (!$prefix) {
        $bundle = isset($property['bundle']) ? $property['bundle'] : NULL;
        $options[$property['label']] = mailchimp_lists_fieldmap_options($type, $bundle, $required, $keypath, $property['label']);
      }
    }
    elseif (!$required || $required_property || $computed_property) {
      if (isset($property['field']) && $property['field'] && !empty($property['property info'])) {
        foreach ($property['property info'] as $sub_key => $sub_prop) {
          $label = isset($tree) ? $tree . ' - ' . $property['label'] : $property['label'];

          // Convert paths to full token syntax for parsing later.
          $token = '[' . $keypath . ']';
          $options[$label][$token] = $sub_prop['label'];
        }
      }
      else {

        // Convert paths to full token syntax for parsing later.
        $token = '[' . $keypath . ']';
        $options[$token] = $property['label'];
      }
    }
  }
  return $options;
}