1

I am totally new to entities in Drupal. I have used the Profile2 module to add a few profile types. This is displayed in the add/user screen as Customer.

After adding the profile type I manage the fields and add a new entry called cust with a field_id of field_cid. This is setup as a list(text) and checkbox widget.

When I edit a user I have a checkbox list for Customer that can be selected by each user. I have a custom module that I am trying to read the selected value by $user->uid from the profile2 field.

The idea is to retrieve the selected item in my module and format the return values to the user from a custom DB table. Here is what I have tried and got results but cannot get all the values.

In my module using:

$profile = profile2_load_by_user($user->uid);$list = field_view_field('profile2', $profile['customer'], 'field_cid');print_r($list)

I get this result:

Array ( [#theme] => field [#weight] => 0 [#title] => cust [#access] => 1 [#label_display] => above [#view_mode] => _custom_display [#language] => und [#field_name] => field_cid [#field_type] => list_text [#field_translatable] => 0 [#entity_type] => profile2 [#bundle] => customer [#object] => Profile Object ( [pid] => 9 [type] => customer [label] => Customer [uid] => 1 [created] => 1409542969 [changed] => 1409545821 [entityType:protected] => profile2 [entityInfo:protected] => Array ( [label] => Profile [plural label] => Profiles [description] => Profile2 user profiles. [entity class] => Profile [controller class] => EntityAPIController [base table] => profile [fieldable] => 1 [view modes] => Array ( [account] => Array ( [label] => User account [custom settings] => ) ) [entity keys] => Array ( [id] => pid [bundle] => type [label] => label [revision] => ) [bundles] => Array ( [customer] => Array ( [label] => Customer [admin] => Array ( [path] => admin/structure/profiles/manage/%profile2_type [real path] => admin/structure/profiles/manage/customer [bundle argument] => 4 [access arguments] => Array ( [0] => administer profiles ) ) [rdf_mapping] => Array ( ) ) ) [bundle keys] => Array ( [bundle] => type ) [label callback] => entity_class_label [uri callback] => entity_class_uri [access callback] => profile2_access [module] => profile2 [metadata controller class] => Profile2MetadataController [static cache] => 1 [field cache] => 1 [load hook] => profile2_load [translation] => Array ( ) [schema_fields_sql] => Array ( [base table] => Array ( [0] => pid [1] => type [2] => uid [3] => label [4] => created [5] => changed ) ) [configuration] => ) [idKey:protected] => pid [nameKey:protected] => pid [statusKey:protected] => status [defaultLabel:protected] => [field_cid] => Array ( [und] => Array ( [0] => Array ( [value] => 1 ) [1] => Array ( [value] => 2 ) ) ) [field_test] => Array ( [und] => Array ( [0] => Array ( [value] => 3 ) ) ) [rdf_mapping] => Array ( ) ) [#items] => Array ( [0] => Array ( [value] => 1 ) [1] => Array ( [value] => 2 ) ) [#formatter] => list_default [0] => Array ( [#markup] => Disney ) [1] => Array ( [#markup] => Burger King ) )

I want the name in the last line [#markup] only returned to my function. In the above output the returned values should be Disney and Burger King.

How can I retrieve these values only?

1 Answer 1

0

Finally this is giving me the results I am after:

$profile = profile2_load_by_user($user->uid);
$customer = field_get_items('profile2', $profile['customer'], 'field_cid');
$key = array();
foreach($customer as $item=>$value){
  $output = field_view_value('profile2', $profile['customer'], 'field_cid', $customer[$item]);
  $key[] = strip_tags(render($output));
}
foreach($key as $word) {
  echo $word . "</br>";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.