I'm working on properly setting the property metadata for the new Entity Registrations module. Registrations are a custom entity and is associated with a generic entity type, E.g., node, user, term, product, etc. It has entity_id and entity_type schema fields. For Views and Rules integration, I want to return the actual Entity object in hook_entity_property_info(). I'm trying something like this,

  $properties['entity'] = array(
    'label' => t("Associated Entity"),
    'description' => t("The entity this registration is associated with."),
    'type' => 'entity',
    'schema field' => 'entity_id',
  );

The documentation states that when the type is entity,

The entity has to be represented using an EntityMetadataWrapper.

This is where I get lost, though. What doest that mean exactly and what are the steps to implement? Thanks!

Comments

levelos’s picture

Changing the title here to reflect the underlying problem I'm trying to solve. In Entity Registraiton, we have a custom registration entity that includes properties for host entity type and host entity id. Combined, these can easily give us the host entity, but I'm unclear how to expose that relationship in Rules and Views. Also wondering if it makes sense to load the host entity objet and set it as a property in entity class __construct() method, E.g.,

  public function __construct(array $values = array(), $entityType = NULL) {
    parent::__construct($values, $entityType);
    $this->host_entity = entity_load_single($this->entity_type, $this->entity_id);
  }

Any guidance is appreciated, this is a real blocker right now for getting a stable release of Entity Registrations. Thanks!

levelos’s picture

Title: clarification on 'entity' type in hook_entity_property_info() » enable views/rules relations on custom entity property

Changing title to reflect underlying question.

jpontani’s picture

It means the property you're adding must be an EntityMetadataWrapper object. The following code sets the entity property to a new EntityMetadataWrapper based on the attached entity type and ID.

public function __construct(array $values = array(), $entityType = NULL) {
  parent::__construct($values, $entityType);
  $this->host_entity = entity_load_single($this->entity_type, $this->entity_id);
  $this->entity = entity_metadata_wrapper($this->entity_type, $this->host_entity);
}
levelos’s picture

Thanks for that great clarification @jpontani, that gets the host entity loaded correctly when I load the registration. Does that help with Views or Rules relationships? They weren't available with that change alone. Any tips there?

alexweber’s picture

Rules/Views integration is automatically provided for all custom properties.

You just need to declare them, preferrably specifying a metadata controller class extending EntityDefaultMetadataController.