1

I'm trying to replicate entity->export() using Entity Metadata Wrappers. When I call value() on entity_metadata_wrapped entities (in this case a field collection), I get everything entity->export() give me, plus a bunch of entity_key:protected fields. I'd like to strip these out, as everything not protected gives me identical values to that provided in export.

Example wrapper->value() result:

Object
(
    [fieldInfo:protected] => 
    [hostEntity:protected] => 
    [hostEntityId:protected] => 
    [hostEntityRevisionId:protected] => 
    [hostEntityType:protected] => 
    [langcode:protected] => und
    [item_id] => 9
    [revision_id] => 9
    [field_name] => field_treatment_categories
    [default_revision] => 1
    [archived] => 0
    [entityType:protected] => field_collection_item
    [entityInfo:protected] => Array
        (
            [label] => Field collection item
            [label callback] => entity_class_label
            [uri callback] => entity_class_uri
            [entity class] => FieldCollectionItemEntity
            [controller class] => EntityAPIController
            [base table] => field_collection_item
            [revision table] => field_collection_item_revision
            [fieldable] => 1
            [redirect] => 
            [entity keys] => Array
                (
                    [id] => item_id
                    [revision] => revision_id
                    [bundle] => field_name
                    [uuid] => uuid
                )

            [module] => field_collection
            [view modes] => Array
                (
                    [full] => Array
                        (
                            [label] => Full content
                            [custom settings] => 
                        )

                    [token] => Array
                        (
                            [label] => Tokens
                            [custom settings] => 
                        )

                )

            [access callback] => field_collection_item_access
            [metadata controller class] => FieldCollectionItemMetadataController
            [bundles] => Array
                (
                    [field_treatment_categories] => Array
                        (
                            [label] => Field collection field_treatment_categories
                            [admin] => Array
                                (
                                    [path] => admin/structure/field-collections/%field_collection_field_name
                                    [real path] => admin/structure/field-collections/field-treatment-categories
                                    [bundle argument] => 3
                                    [access arguments] => Array
                                        (
                                            [0] => administer field collections
                                        )

                                )

                            [rdf_mapping] => Array
                                (
                                )

                        )

                    [field_treatments] => Array
                        (
                            [label] => Field collection field_treatments
                            [admin] => Array
                                (
                                    [path] => admin/structure/field-collections/%field_collection_field_name
                                    [real path] => admin/structure/field-collections/field-treatments
                                    [bundle argument] => 3
                                    [access arguments] => Array
                                        (
                                            [0] => administer field collections
                                        )

                                )

                            [rdf_mapping] => Array
                                (
                                )

                        )

                )

            [static cache] => 1
            [field cache] => 1
            [load hook] => field_collection_item_load
            [translation] => Array
                (
                )

            [schema_fields_sql] => Array
                (
                    [base table] => Array
                        (
                            [0] => item_id
                            [1] => revision_id
                            [2] => field_name
                            [3] => archived
                            [4] => uuid
                        )

                    [revision table] => Array
                        (
                            [0] => revision_id
                            [1] => item_id
                        )

                )

            [token type] => field_collection_item
            [apachesolr] => Array
                (
                    [indexable] => 
                    [status callback] => 
                    [document callback] => 
                    [reindex callback] => 
                    [bundles changed callback] => 
                )

            [configuration] => 
            [uuid] => 1
        )

    [idKey:protected] => item_id
    [nameKey:protected] => item_id
    [statusKey:protected] => status
    [defaultLabel:protected] => 
    [uuid] => c1d3982f-31c8-4640-89b4-2f54334e4803
    [field_tc_short_description] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => short desc 2
                            [format] => 
                            [safe_value] => short desc 2
                        )

                )

        )

    [field_tc_short_title] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => Short title 2
                            [format] => 
                            [safe_value] => Short title 2
                        )

                )

        )

    [field_tc_title] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => Treatment Category 2
                            [format] => 
                            [safe_value] => Treatment Category 2
                        )

                )

        )

    [field_treatments] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => 10
                            [revision_id] => 10
                        )

                    [1] => Array
                        (
                            [value] => 11
                            [revision_id] => 11
                        )

                )

        )

    [rdf_mapping] => Array
        (
        )

    [entity_view_prepared] => 1
)

The [fieldInfo:protected] fields I'd like to remove, but I haven't found an options/methods to do so. Any thoughts?

0

use array_walk and scan the entity array to see if the key has the substring :protected in it. If yes, then unset() the array key.

| improve this answer | |
  • While I am hoping for existing functionality, that's a good alternative. Cheers. – Atomox Jun 11 '14 at 13:58
  • Because these were object properties, looping would not work to strip them out. You could use this method and construct a second array containing only the values foreach or array_walk can touch, providing a similar result. – Atomox Jun 11 '14 at 14:48
0

For anyone looking to do this, the answer is pretty simple.

entity_metadata_wrapper->value() returns an object, complete with protected properties. To get only the values from the result, you can use php's get_object_vars() function.

This returns data equivalent to a normal entity->export() call in terms of data, although I believe export returns JSON instead of the array you get here.

So:

// Wrap an existing entity.
$wrapper = entity_metadata_wrapper('entity_type', $entity);

// Get the object representation of that entity.
$entity_data = $wrapper->value();

// Strip all properties (including :protected), 
// and get an array representation of that data.
// This is the same data you get from entity->export(), 
// but in array form.
$final_data = get_object_vars($entity_data);

Hope this helps someone.

| improve this answer | |
0

You can't, because what you're seeing it's not an array, but an object, so you can't simply unset protected properties, because they're protected for the reason.

Members declared protected can be accessed only within the class itself and by inherited classes.

This is how PHP OOP visibility works. So you should find appropriate getter/setter method to change that member of class or extend that class and define one.

| improve this answer | |

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.