Hi,
I'm using DATA to connect some drupal node to a row of a table created with data. Using drupal 6 I have a ckk field called "Data Record Reference" and using thid field I can use an autocomplete widget to select the row by a view. Everything is working correctly. I have a "user select field" to chose wich colum use for the selection into the widget and the "referencies Field to select the field used as identifier.
Now I'm try to repeated the same operation usinf drupal 7 but there isn't the "Data Record Reference", I have try using Entity Reference but in this case the widget to select the row is empty and I don't have the possibility to chose "selection" and "identifier". Someone have an idea to replace the same condition into drupal 7?

Comments

joachim’s picture

Title: D7 and Data Record Reference » allow entity ref fields to point to data items
Category: support » feature

Selecting a data item from somewhere else with entity reference probably won't work yet. The main thing is probably the absence of a label callback...

imclean’s picture

See #1551430: implement hook_entity_property_info for Feeds integration

I've added a label to hook_entity_property_info() which may help.

joachim’s picture

I suspect it's rather the label callback in hook_entity_info() that we need, but that would need checking.

joachim’s picture

Yup, it uses entity_label(): http://api.drupal.org/api/drupal/includes!common.inc/function/entity_lab...

Hence we need a label key or callback.

imclean’s picture

Thank joachim. Feeds integration is something I'm keen to get working and I've noticed data_entity lacks a few required methods and properties. The label shouldn't be too hard to add.

imclean’s picture

Looking at entity_data.module's hook_entity_info(), the label is currently set to the table's title. ['entity keys']['label'] is not set.

joachim’s picture

Those are two different things.

$info[label] is the label of the entities as a whole, like 'Node'.
$info['entity keys']['label'] is the key on the entity that gives you the label for that entity, so like 'About us' for a page node.

imclean’s picture

In the case of a data entity, I imagine $info['entity keys']['label'] would the primary key(s). Except the primary key field is used as the entity ID, which needs to be an integer, so that wouldn't be very friendly. This could be combined with the table name or label.

This may be a rare case: my primary key field is an auto-increment integer field which isn't included when saving the entity.

Ideally, the primary key(s) could be independent of the entity ID, with a separate config option to specify the entity ID field.

imclean’s picture

Alternatively, data_entity could set and increment the ID specific to each entity type.

imclean’s picture

Ok, to the topic at hand: could the label be 1 or more fields selected on the "Configure Entity Form"? Does it need to be unique?

imclean’s picture

StatusFileSize
new986 bytes

Added a label callback which uses index fields as the label. Multiple indexes are separated by a space in the label.

imclean’s picture

Status: Active » Needs review

Updated status.

joachim’s picture

Status: Needs review » Needs work
+++ b/data_entity/data_entity.module
@@ -192,6 +193,22 @@ function data_entity_permission() {
+  if (isset($schema['indexes'])) {

This will return NULL if there aren't any indexes, which may cause problems with other modules expecting a string.

Should we return something like '' or 'No label'?

Alternatively, we could add checkboxes to the data table management UI to pick which columns to make up labels.

imclean’s picture

Status: Needs work » Needs review
StatusFileSize
new2.21 KB

"No label" sounds good.

This adds the option to the Entity Configuration form.

Some notes:

1. The schema is used to get the fields in their correct order. This may be important for label fields but if it isn't we could just loop through the meta information.

2. The callback looks a bit clumsy getting all the information. There may be a cleaner way.

3. Unrelated to this issue: theme_data_entity_admin_entity_form() sticks an extra blank cell at the end of each row. Is this necessary?

joachim’s picture

+++ b/data_entity/data_entity.module
@@ -173,6 +175,22 @@ function data_entity_permission() {
+function data_entity_label($entity, $entity_type) {
+  $table = DataTable::instance($entity->data_table);
+  $meta = $table->get('meta');
+  $schema = drupal_get_schema($entity->data_table);
+  $labels = array();
+  foreach ($schema['fields'] as $field_name => $field) {
+    if ($meta['fields'][$field_name]['label_field']) {
+      $labels[] = $entity->$field_name;
+    }
+  }

I don't think you need to get the schema here. Just iterate over $meta['fields'] and see if there are labels set.

Alternatively, perhaps in the submit handler for data_entity_admin_entity_form() we could save a nice piece of metadata that specifically gives us a list of the label fields, so we don't need to muck about with looping every time.

Also, does DataTable::instance() cache anything? If not, we should possibly investigate performance of this, say in an entity reference field.

Finally, I've just thought of something -- how does the autocomplete widget for entref work? Is it compatible with a label callback that builds the label programmatically, or does it only work with a particular entity property being the label? If that's the case, we should probably limit just one column to being the label.

imclean’s picture

The schema reflects the order of the fields in the admin screens, $meta['fields'] doesn't. That said, a list of label fields as separate metadata sounds like a nicer way to go.

I'm not sure about autocomplete, I'll have to check.

What alternatives are there to DataTable::instance()? All we really need is the metadata, can Data module provide this alone?

joachim’s picture

> What alternatives are there to DataTable::instance()? All we really need is the metadata, can Data module provide this alone?

Oh I'm fairly sure we need this, as it's the way into the Data API. My concern is that if you're looking at a dropdown of entities, creating that class and having it load the same thing from the database once for each entity in the dropdown is going to be quite expensive. I'd like that to be statically cached somewhere.

imclean’s picture

Multiple label fields may be overkill. If we only had one we could potentially avoid a label callback, although we'd need a helper function for hook_entity_info(). Does this cache its information?

joachim’s picture

Yup, hook_entity_info() is cached. Doing expensive things in that is fine, though beware of circularity there.

imclean’s picture

Here's a much simpler approach which allows one column to be selected as the label. If one isn't specified a callback is used so it doesn't break anything.

joachim’s picture

Looks good. Would be great if someone could try it out!

imclean’s picture

Title: allow entity ref fields to point to data items » Add Entity Label field (Was: allow entity ref fields to point to data items)

Updated title. Hopefully we can get some action here.

cayenne’s picture

Been struggling with this all day. Hoped that the patch would cure it. Still not able to display anything at all for the referenced data. Is anybody able to make this work, or is it just me?

imclean’s picture

Hard to know. This patch does add the entity label as described, but there may be other issues.

What modules and what patches are you using?

joachim’s picture

Issue summary: View changes
StatusFileSize
new2.53 KB

The patch at #20 was nearly there -- just wasn't saving the setting in the UI.

Here's an update patch.

joachim’s picture

Status: Needs review » Fixed

Committed. New alpha release shortly!

Thanks to everyone who's worked on this.

SHA: 9865f0c0ca4c70701fe8a9d977f535beedfcb53c
Author: imclean
Subject: Issue #1538588 by imclean, joachim: Added setting for field to use for entity label, thus allowing use of data entities as targets for entityreference fields.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.