2

View with relationship to custom entity via computed property

I have nodes connected to a custom entity (called here "myentity") via a computed property, like this:

  $info['node']['properties']['myentity'] = array(
    'label' => t("Myentity"),
    'type' => 'myentity',
    'computed' => TRUE,
    'queryable' => TRUE,  // setting this to FALSE doesn't change anything
    'entity views field' => TRUE,
    'getter callback' => 'mymodule_node_property_myentity_get',
  );

The callback function extracts some values from the node and calls another function, which fills these values into an entity field query, returning the wanted myentity instance.

This entity connection is working, so I can get the property "myprop" of myentity via an entity API wrapper $node_wrapper->myentity->myprop->value().

Now I want to add myprop in a simple view for nodes. After creating a relationship to myentity, "Content: Myentity" shows up in the "Add fields" list, as well as all of myentity's properties.

Selecting "Content: Myentity" works - myentity's default label is displayed in the view output. (With some magic - the displayed SQL query is SELECT node.nid AS nid FROM {node} node).

However, selecting "Myentity: Myprop" does not work. Even before executing the view, Drupal shows a SQL error:

SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM-clause entry for table "myentity" LINE 1: SELECT node.nid AS nid, myentity.myprop AS myentity_myprop, no... ^

The displayed SQL query here is SELECT node.nid AS nid, myentity.myprop AS myentity_myprop FROM {node} node. myentity is referenced in the select clause, but not in the from clause, nor is there any join statement.

Has somebody managed to add properties of a custom entity, connected via a computed node property, to views?

1 Answer 1

1

Finally figured out a workaround: Pull every needed entity property as a "virtual field" into the node itself.

// define additional property information for node
function mymodule_entity_property_info_alter(&$info) {
  $properties = & $info['node']['properties'];
  foreach (array('myprop') as $property) {
    $properties[$property] = array(
      'label' => $info['mymodule']['properties'][$property]['label'],
      'description' => $info['mymodule']['properties'][$property]['description'],
      'type' => $info['mymodule']['properties'][$property]['type'],
      'computed' => TRUE,
      'queryable' => TRUE,
      'entity views field' => TRUE,
      'getter callback' => "mymodule_node_property_verbatim_get",
    );
  }
}

// getter callback
function mymodule_node_property_verbatim_get($data, array $options, $name, $type, $info) {
  $myentity = mymodule_get_from_node($data);
  return !empty($publication->{$name}) ? $publication->{$name} : array();
}

// obtain the related entity from the node
function mymodule_get_from_node($node) {

  // some custom code to get myentity

  return $myentity;
}

Thus, the properties can be adressed as views fields as well as for any other purposes. It's possible to extend this to access properties of nested entities, too.

One drawback, however: The fields cannot be used as views sort criteria.

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.