You are here

function name_field_parse_additional_components in Name Field 7

Helper function to discover alternative name component sources.

2 calls to name_field_parse_additional_components()
name_field_formatter_view in ./name.module
Implements hook_field_formatter_view().
name_user_load in ./name.module
Implements hook_user_load().

File

./name.module, line 1479
Defines an API for displaying and inputing names.

Code

function name_field_parse_additional_components($entity_type, $entity, $instance) {
  $settings = $instance['settings'];
  $extra = array();
  $map = array(
    'preferred' => 'preferred_field_reference',
  );
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  foreach ($map as $component => $key) {
    if (!empty($settings[$key])) {
      if ($settings[$key] == '_self') {
        if ($label = entity_label($entity_type, $entity)) {
          $extra[$component] = $label;
        }
      }
      elseif (strpos($settings[$key], '_self_property') === 0) {
        $property = str_replace('_self_property_', '', $settings[$key]);
        if (!empty($entity->{$property}) && is_scalar($entity->{$property})) {
          $extra[$component] = $entity->{$property};
        }
      }
      elseif ($_instance = field_info_instance($entity_type, $settings[$key], $bundle)) {
        $_field = field_info_field($settings[$key]);
        $_values = array();
        if (field_access('view', $_field, $entity_type) || field_access('edit', $_field, $entity_type)) {
          $clone = clone $entity;
          $_langcode = field_language($entity_type, $entity, $settings[$key]);

          // @todo
          $_view_mode = 'default';
          $_display = field_get_display($_instance, $_view_mode, $clone);
          if ($_display['type'] == 'hidden') {
            $cache = _field_info_field_cache();
            $_display = array();
            $_display = $cache
              ->prepareInstanceDisplay($_display, $_field["type"]);
          }
          $_display['label'] = 'hidden';
          $_element = field_view_field($entity_type, $clone, $settings[$key], $_display, $_langcode);
          if ($output = trim(strip_tags(drupal_render($_element)))) {
            $extra[$component] = decode_entities($output);
          }
        }
      }
    }
  }
  return $extra;
}