0

I would like to display the group of a member in their user profile but I don't know the PHP syntax for getting the group. I'm using the render function, but I have an error. When I put print_r($user_profile);, I get the following array structure :

Array
(
    [#printed] => 1
    [user_picture] => Array
        (
            [#markup] => 
            [#weight] => -10
            [#type] => markup
            [#pre_render] => Array
                (
                    [0] => drupal_pre_render_markup
                    [1] => ctools_dependent_pre_render
                )
        [#children] => 
        [#printed] => 1
    )

[og_user_group_ref] => Array
    (
        [#theme] => field
        [#weight] => 0
        [#title] => Group membership
        [#access] => 1
        [#label_display] => above
        [#view_mode] => full
        [#language] => und
        [#field_name] => og_user_group_ref
        [#field_type] => entityreference
        [#field_translatable] => 0
        [#entity_type] => user
        [#bundle] => user
        [#object] => stdClass Object
            (
                [uid] => 11
                [name] => tmeddeb
                [pass] => $S$DRJpiD/FKXsAIoBKMv9YFcGMfhu/4jU0TfOaoeolVcS4ZhxP8KIA
                [mail] => tmeddeb@ccomptes.net
                [theme] => 
                [signature] => 
                [signature_format] => filtered_html
                [created] => 1348668493
                [access] => 1349771256
                [login] => 1349771256
                [status] => 1
                [timezone] => Europe/Berlin
                [language] => ar
                [picture] => 
                [init] => tmeddeb@ccomptes.net
                [data] => 
                [roles] => Array
                    (
                        [2] => authenticated user
                        [8] => الناشر
                        [10] => _user_role_11
                    )

                [og_user_group_ref] => Array
                    (
                        [und] => Array
                            (
                                [0] => Array
                                    (
                                        [target_id] => 17
                                    )

                            )

                    )

                [og_other_user_group_ref] => Array
                    (
                    )

                [field_grade] => Array
                    (
                        [und] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => مستشار لدائرة المحاسبات
                                        [format] => 
                                        [safe_value] => مستشار لدائرة المحاسبات
                                    )

                            )

                    )

                [field_function] => Array
                    (
                        [und] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => مدير عام
                                        [format] => 
                                        [safe_value] => مدير عام
                                    )

                            )

                    )

                [field_firstname] => Array
                    (
                        [und] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => الطاهر
                                        [format] => 
                                        [safe_value] => الطاهر
                                    )

                            )

                    )

                [field_lastname] => Array
                    (
                        [und] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => المؤدّب
                                        [format] => 
                                        [safe_value] => المؤدّب
                                    )

                            )

                    )

                [rdf_mapping] => Array
                    (
                        [rdftype] => Array
                            (
                                [0] => sioc:UserAccount
                            )

                        [name] => Array
                            (
                                [predicates] => Array
                                    (
                                        [0] => foaf:name
                                    )

                            )

                        [homepage] => Array
                            (
                                [predicates] => Array
                                    (
                                        [0] => foaf:page
                                    )

                                [type] => rel
                            )

                    )

                [realname] => tmeddeb
                [entity_view_prepared] => 1
                [privatemsg_disabled] => 
            )

        [#items] => Array
            (
                [0] => Array
                    (
                        [target_id] => 17
                    )

            )

        ***[#formatter] => og_list_default
        [0] => Array
            (
                [#type] => link
                [#title] => غرفة الموارد البشرية
                [#href] => http://localhost/drupal/?q=node/17
            )***

        [#markup] => 
        [#children] => <div class="field field-name-og-user-group-ref field-type-entityreference field-label-above"><div class="field-label">Group membership:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="http://localhost/drupal/?q=node/17">غرفة الموارد البشرية</a></div></div></div>
        [#printed] => 1
    )

But when I try to get the title value of [formatter] I get the following fatal error:

Fatal error: Cannot use string offset as an array in C:\wamp\www\drupal\sites\all\themes\courdescomptes\user-profile.tpl.php on line 17

The function for getting a title value for formatter is as follows:

print render($user_profile['og_user_group_ref']['#formatter'][0]['title'])
1

From the looks of your output it should be #title not title:

print render($user_profile['og_user_group_ref']['#formatter'][0]['#title']);

Since #title is a string though you'll want to just print it directly and save yourself the tiny bit of overhead:

print $user_profile['og_user_group_ref']['#formatter'][0]['#title'];
  • whene I put your code print $user_profile['og_user_group_ref']['#formatter'][0]['#title']; I have alse the same problem Fatal error: Cannot use string offset as an array in C:\wamp\www\drupal\sites\all\themes\courdescomptes\user-profile.tpl.php on line 17 – karimhmadi Oct 9 '12 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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