Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Status: Needs review » Needs work

The last submitted patch failed testing.

Status: Needs work » Needs review

Amitaibu requested that failed test be re-tested.

Status: Needs review » Needs work

The last submitted patch failed testing.

amitaibu’s picture

hmm, is that a test bot issue? "Failed to install Drupal on MySQL 5.0 ISAM"

Status: Needs work » Needs review

Amitaibu requested that failed test be re-tested.

Status: Needs review » Needs work

The last submitted patch failed testing.

amitaibu’s picture

Status: Needs work » Needs review
FileSize
3.04 KB

For some reason *many* core tests on my local fail, so re-rolled and let bot try.

Status: Needs review » Needs work

The last submitted patch failed testing.

Status: Needs work » Needs review

Amitaibu requested that failed test be re-tested.

Status: Needs review » Needs work

The last submitted patch failed testing.

amitaibu’s picture

Status: Needs work » Needs review

I think there's a problem with the bot here - can someone please check this?

Amitaibu requested that failed test be re-tested.

Status: Needs review » Needs work

The last submitted patch failed testing.

deekayen’s picture

I forced both bots to do a new self test, which will test core without your patch. If they fail, they'll shut themselves off.

deekayen’s picture

Seems to be your patch that's preventing core from installing. Both bots passed the self test and have been checking other patches with passing results.

amitaibu’s picture

Status: Needs work » Needs review
FileSize
4.35 KB

/* me not gonna blame bot again for stupid things I did ;) 10x deekayen...

amitaibu’s picture

Ok, let's see how #571654: Revert node titles as fields turns out before doing anything with this issue.

amitaibu’s picture

#571654: Revert node titles as fields has been rolled back, so back to needs review.

Re-test of 629484-entity-label-16.patch from comment #16 was requested by aspilicious.

Status: Needs review » Needs work

The last submitted patch, 629484-entity-label-16.patch, failed testing.

amitaibu’s picture

Status: Needs work » Needs review
FileSize
2.72 KB

re-rolled.

amitaibu’s picture

#21: 629484-entity-label-21.patch queued for re-testing.

chx’s picture

Status: Needs review » Needs work

There is no use case, example, documentation, test, anything. Not to mention that you might need more than a key (might -- I am not sure because I cant be sure what do you want here) it might need an optional callback.

amitaibu’s picture

Status: Needs work » Needs review
FileSize
6.26 KB

Patch adds 'labal callback' property and docs.
I have seen no tests for uri callback, so I'm not sure where to put it -- each entity should test it's own callback?

amitaibu’s picture

Fix typo.

amitaibu’s picture

Here's a code snippet of a use case in OG7:

      $params = array(
        '@user' => $account->name,
        '@group' => entity_label($entity_type, $entity),
      );

      drupal_set_message(t('@user is already a member of the group @group.', $params));
catch’s picture

I can see the use case but can we please not call it label_field, that's going to create massive confusion with field API. This should just be added to the 'object_keys' array.

Also I have no idea what will happen with this if we try to move label-ish things to fields in D8, but since that didn't happen and title as field showed how complex it is, that shouldn't necessarily stop this going in but worth bearing in mind.

amitaibu’s picture

10x catch -- Moved label under object_keys. Let's see what bot thinks of it...

amitaibu’s picture

violets are blue and tests are green :)

fago’s picture

Status: Needs review » Needs work

That one makes a lot of sense.

@object-keys-label:
It's documented to be a "field in the base column", but that's not what the "object-keys" are for. It should just work like the other object keys - thus it should specify the key the entity object is using for the label property.

Then entity_label() could just return $entity->$label_key - that's it. Don't know whether we need the possibility to specify a 'label callback' instead, probably just having the key is fine. Also having the key in place would be useful for the RDF stuff to map those key to rdfs:label.

amitaibu’s picture

@fago,
> It should just work like the other object keys - thus it should specify the key the entity object is using for the label property.

One of the reasons for the patch, is that we don't need to have the entity loaded to get the label. We should be able to query it directly, for example when we want to show a selectlist with 100 node titles. This is the reason we specify the DB column, and not the entity key.

Aslo, Maybe we should use the same idea of entity_uri #699440: Add bundle support to entity_uri callback to remove performance overhead of forum_url_outbound_alter() (regression from D6) -- and unify those functions to entity_property($property, $entity_type, $entity).

amitaibu’s picture

Status: Needs work » Needs review
FileSize
5.48 KB

- removed label callback from each entity, and moves logic into entity_label -- if there's a callback it will be fired, otherwise use the label key.
- A bit better docs.

fago’s picture

I'm not sure about this:
>+ * This property can be also used when a module wants to query the
+ * database directly in order to get the entity label, without loading the
+ * entire entity object. Note that it is possible that an entity label is
+ * a result of more complex logic, thus can't be queried. In such a case
+ * the "label callback" should be used. @see entity_label().

Well, it also won't work if the entity doesn't use a db backend. But still specifying the label key + no callback might make sense?
Also, to make use of that but still falling back to loading the full entity, quite some special casing is needed anyway. Next entity_uri() needs the full entity to generate the uri anyway, so the typical use-case to generate a link needs the entity object. So shouldn't we just use entity_load_multiple() anyway and remove that db-specific special thing?

amitaibu’s picture

> Well, it also won't work if the entity doesn't use a db backend

Right, didn't think of it.

> But still specifying the label key + no callback might make sense?

Yes, actually in most cases you won't specify a callback function, and can just use the label key.

> So shouldn't we just use entity_load_multiple() anyway and remove that db-specific special thing?

My concern is that if we want to show a selectlist with 100 node titles do we really want to node_load() all 100 nodes.

fago’s picture

>My concern is that if we want to show a selectlist with 100 node titles do we really want to node_load() all 100 nodes.

I see. I just wonder, if one has to fall back on loading entities + using entity_label() on the entity anyway, whether the performance impact of using entity_load() to load all entities at once vs a direct query is worth the troubles.

amitaibu’s picture

Status: Needs review » Needs work

Ok, also after talking with fago and DamZ, I'll change the patch to only work will $entity->label, and entity_callback - and avoid advocating direct query of the DB.

DamZ also suggested for my use case to use an autocomplete field when there are more then X items in the list.

amitaibu’s picture

Status: Needs work » Needs review
FileSize
5.75 KB

Patch changes API docs, and no longer refers to query of DB. Also the file entity label key was added.

aspilicious’s picture

Status: Needs review » Needs work
+++ includes/common.inc

+ *       @see entity_label().
  *   - bundle keys: An array describing how the Field API can extract the

We don't put an @see statement in the middle of a docblock.
Replace this with a normal "See"

+ * @param $entity
+ *   The entity for which to generate a path.
+ * @return

Put a newline between the @param and @return block

Powered by Dreditor.

amitaibu’s picture

Status: Needs work » Needs review
FileSize
5.76 KB

Fixed aspilicious' comments.

fago’s picture

Status: Needs review » Needs work

Great!

+
+ // The label property marks the label location (e.g. title on a node object)
+ // we don't need to treat it as a reserved field name.
+ unset($info['entity keys']['label']);

Why not? A field of this name would conflict with the existing label key, not?

I think you forgot to add the label key for vocabularies.

amitaibu’s picture

[Edit: GURU meditation error?! :)]

amitaibu’s picture

Status: Needs work » Needs review
FileSize
6.06 KB

> + unset($info['entity keys']['label']);
>
> Why not? A field of this name would conflict with the existing label key, not?

That's the issue that caused the tests to fail at first. Upon installation the "label" field couldn't be created -I've added a bit to docs, to make it clearer.

> I think you forgot to add the label key for vocabularies.

thanks, added.

fago’s picture

+ // installation the original label field will not be able to be created.

Which original label field? *confused* We have no label as field, no? Even it won't be supported by a simple entity key, as the field API doesn't use a simple string property anyway. So I re-rolled the patch without that hunk, I'm curious what breaks.

fago’s picture

Issue tags: +Needs tests

Fine, no need for that hunk! ;)

ok, we still need tests. Unfortunately there is no "entity" testing file/module yet. I'd just put the test into the system module's system.test and add a small entity_test.module to the simpletest/tests modules. Testing two new entity types, one using the key and one using the callback should be fine.
Ideally we'd convert the existing "entity_cache_test_dependency.module" to the entity_test.module, such that this module can server for any kind of entity API related tests now.

amitaibu’s picture

> Fine, no need for that hunk! ;)

Cool, something must have changed, since the first time I've rolled the patch -- thanks for checking it!

amitaibu’s picture

Patch is untested -- my home laptop doesn't seem to run simpletets properly.

In this patch:
- Pass to the label callback the entity type as-well.
- Add test. I've added them in field.test as there are already helper functions there that deal with entity creation.

+    $entity = field_test_create_stub_entity();
+    field_test_entity_save($entity);

Status: Needs review » Needs work

The last submitted patch, 629484-entity-label-46.patch, failed testing.

amitaibu’s picture

Status: Needs work » Needs review
FileSize
10.29 KB

Let's see if bots likes this one better -- if it won't work, I'll continue on Sunday on my office computer.

Status: Needs review » Needs work

The last submitted patch, 629484-entity-label-48.patch, failed testing.

amitaibu’s picture

Status: Needs work » Needs review
FileSize
11.68 KB

Fixed failing tests.

amitaibu’s picture

Discussed with catch on IRC that in order to keep this patch simple, the tests should remain in field_entity. A follow up patch can move the tests to an entity_test module.

amitaibu’s picture

FileSize
10.83 KB

Re-roll to apply to head. No code change.

Status: Needs review » Needs work

The last submitted patch, 629484-entity-label-52.patch, failed testing.

amitaibu’s picture

FileSize
11.68 KB

*sigh*, wrong patch...

amitaibu’s picture

Status: Needs work » Needs review
FileSize
11.68 KB

correct status.

Damien Tournoud’s picture

Status: Needs review » Reviewed & tested by the community

This feature is needed by groups, and is a nice addition to the entity API.

The patch looks sound on visual inspection (except the fixes the FieldAttachStorageTestCase, but this set of tests is bound to go away with the Listing API), adds a few tests and doesn't introduce regressions.

Me want.

amitaibu’s picture

FileSize
10.33 KB

> except the fixes the FieldAttachStorageTestCase, but this set of tests is bound to go away with the Listing API)

Listing API is in -- re-rolled

amitaibu’s picture

Issue tags: -Needs tests

#57: 629484-entity-label-56.patch queued for re-testing.

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

The last submitted patch, 629484-entity-label-56.patch, failed testing.

amitaibu’s picture

Status: Needs work » Needs review
FileSize
10.89 KB

re-roll

amitaibu’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

It was already RTBC and tests are green, so I think it's safe to re-mark as RTBC.

fago’s picture

Status: Reviewed & tested by the community » Needs work

This patch looks great to me!

 }
 
+/*
+ * Returns the label of an entity.
+ *
+ * @param $entity_type

It should be /**. Also the added docs to hook_entity_info() 'label callback' don't tell me that there is the label key I can use instead. Thus if I don't coincidently stumble over that, I don't know whether I should implement it.

amitaibu’s picture

Status: Needs work » Needs review
FileSize
11.08 KB

Re-rolled to address #62 comments. Thanks fago.

fago’s picture

hm, imho the new comment reads a bit clumsy. What about that instead:

+ *   - label callback: Optional; A function taking an entity as argument and
+ *     returning the label of the entity, e.g. the title of a node, or the
+ *     subject of a comment. This property can be used when the label is a
+ *     result of complex logic, else just specify the entity key "label" (see
+ *     below).

?

amitaibu’s picture

@fago,
I explicitly specify that label is under "entity keys" , so it won't be confused with the entity label property.

fago’s picture

Status: Needs review » Reviewed & tested by the community

I see, makes sense. Then back to RTBC!

Having a label for an entity is rather critical for doing anything on top of entities. Thus I guess this is rather important for some field types too. Then the patch looks fine and comes with tests.

amitaibu’s picture

@Dries,
Webchick asked me to give a summary of why this issue is important, I'll use OG's case:

OG is no longer node-centric. This means that a node can be a group, but also a user or a taxonomy term. In fact every entity that is fieldable can be group.

OG can't be responsible, nor does it know how to extract the label of any entity -- it's up to the entity to "explain" Drupal how it's label is formed - similar to entity_uri().
The simple case is just returning the key of the label (e.g. "title" on node entity). If complex logic is needed an optional label callback can be set.

Dave Reid’s picture

Should the user entity label be using a label callback with format_username() so we're not promoting direct use of $account->name?

amitaibu’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
11.61 KB

Good idea Dave, re-rolled and added user_label() callback.

Dave Reid’s picture

Yes, that's a definite improvement. Is there a reason we're having to add $entity_type as a parameter for the label callback? It doesn't fit with the pattern currently used by entity_uri which just takes the entity object as the sole parameter. Maybe entity_uri is the messed-up function and should have two parameters. Just looking for which one is right.

amitaibu’s picture

FileSize
11.45 KB

Makes sense, the callback function is already on specific entity type.

moshe weitzman’s picture

So, does this mostly deprecate Automatic Nodetitles module (http://drupal.org/project/auto_nodetitle)? If one wants to custom node title for a given content type, would that be an entity_alter now?

Dave Reid’s picture

Well, auto_nodetitle would still have to do some logic, but yes, the hard stuff is done with this improvement.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

looks ready to me

arhak’s picture

subscribe

chx’s picture

Priority: Normal » Major

I am in strong support of this patch because you can't create a URL out of an entity without having a title / name / label for the URL.

Edit: the path already comes from entity_uri.

rcross’s picture

is this going to be committed? or pushed to D8?

amitaibu’s picture

Yikes, @rcross, don't give the committers bad ideas ;) OG is now relying on it, but I seriously doubt it will be the only one that needs to get the label of an entity...

Damien Tournoud’s picture

I'm strongly in favor of this too.

One use case I have in mind is building custom translatable terms for internationalization. Currently $term->name is hardcoded all over the place, so you cannot do that properly.

fago’s picture

>I'm strongly in favor of this too.

Me too, having a possibility to get the label of an entity is critical for any module building upon entities.

drunken monkey’s picture

I'm very much in favor of this, too, working with entities generically is a lot more difficult if you can't find out their names. At the moment I've hardcoded a test for $name, $title or $label as an entity field, but that's hardly a solution.

drunken monkey’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
12.46 KB

Re-roll.

sun’s picture

FileSize
12.12 KB

"label" and "label callback" slightly clash with the existing entity type "label". I wonder why we cannot simply use "title" instead of "label" - why do we always re-invent the wheel? Just because it's "subject" for comments, but "title" for 99.9999% of all other entities? Sorry, but I don't get that.

Re-rolled with various documentation fixes, proper test group, etc.

Also removed needless user_label() wrapper, which was plain function call overhead.

fago’s picture

As you know I'm no native speaker, but afaik "title" fits well for documents (nodes). But with not all entities being documents, label makes more sense to me.

Also I can't think of another entity type beside nodes that uses 'title'? I'd say most of them use 'name', but personally I prefer 'label' as it makes clear that we are talking about a human readable description and not a machine readable name as e.g. vocabs have.

drunken monkey’s picture

Status: Needs review » Reviewed & tested by the community

I'm with fago, e.g. for users a "title" would be something like "PhD" rather than the name.
That an entity info now can contain "label", "label callback" and "entity keys[label]", with the first having got nothing to do with the others is a bit unfortunate, but I think the documentation is clear enough on this.

mossy2100’s picture

For the record, I agree that "label" is misleading. Since "name" applies to users and terms at least, this would be semantically clearer.

webchick’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: +API change

I talked with Amitaibu about this some in CPH, along with DamZ. While I'm (as always) heavily resistant to any sort of API change at this stage in the release cycle, it does definitely seem like this is something that's a missing link in the entity API. Now that a few contributed modules are starting to make use of entities (OG, Drupal Commerce, etc.) it's becoming more clear that such functionality is needed. Apparently Dries has already given verbal sign-off on this as well.

On the naming, I'm a bit unsure. "Title" has a very specific meaning within the context of nodes. "Name" has a very specific meaning within the context of users. "Label", while not perfect, is at least a new word (in terms of core) that doesn't have any pre-existing meaning that people need to "un-learn", and applies fine across all entities I can think of (except, I guess, an entity with an identifying field called "Label" :P) without ambiguity on what's being talked about. So I think I'm fine with "label", at least for D7.

Committed to HEAD. Thanks!!

Randy, the API change here is that modules that declare entities must now declare a new 'label' index in the 'entity keys' array, which points to their primary identifying field (subject for comments, title for nodes, etc.)

For example, in taxonomy_entity_info():

function taxonomy_entity_info() {
       'entity keys' => array(
         'id' => 'tid',
         'bundle' => 'vocabulary_machine_name',
+        'label' => 'name',
       ),

Since Entity API is a new API in D7, I'm unsure whether this needs to go out over the devel list. I leave that to your call.

effulgentsia’s picture

Priority: Major » Normal
Status: Fixed » Needs work
Issue tags: +Needs documentation

I just saw this issue and the new entity_label() function and 'label callback' seems cool, but as in #823428: Impossible to alter node URLs efficiently, I'm confused whether this is intended for altering in hook_entity_info_alter(). Should node_admin_nodes() be changed to call entity_label() instead of hard-coding that it's the 'title' column? I'm guessing fago would say no, just like node.module builds many queries that hard-code 'node' as the table instead of assuming that 'base table' is alterable. Can we have some more docs that clarify how this is intended to be used?

This is potentially very helpful in a media.module use-case (#910396: Implement the new entity label setting), where we use filename as the label by default, but want contrib modules to be able to add a title field to media entities, so I'm assuming such a contrib module could hook_entity_info_alter() to a 'label callback' that can return the value of a field, but then such a contrib module could only expect this to work in places that actually call entity_label().

fago’s picture

I'm guessing fago would say no, just like node.module builds many queries that hard-code 'node' as the table instead of assuming that 'base table' is alterable.

Exactly ;) The existence of an info hook does not mean altering it magically works. It is information about the entity type, which the providing module defines. However if the providing module wants to allow altering, it could just stick to the information put in the hook and document that somewhere. E.g. implement a helper function/method media_label() that just makes use of entity_label() and document that the label is alterable via hook_entity_info in the function docs.

I don't think we need to document each info property whether its alterable or not, usually they are not. Documenting *generically* alterable properties + document entity type specific supported alterations like the media label should be fine.

Does that make sense?

flokli’s picture

subscribing

tbeauchemin’s picture

Suscribing

baduong’s picture

Suscribing

kenianbei’s picture

subscribing

kim.pepper’s picture

subscribing

squ1rr3l’s picture

subscribe

flokli’s picture

What about this?
Is the only thing left here documentation work or is there still a possible API change? (when looking at the nearing release of D7 RC1...)

When looking into CVS, I see that there was code commited, but is this code enough? (Bug has still the "API change" tag...)

effulgentsia’s picture

Issue tags: -API change

I think all that's left here is documentation. Therefore, removing the "API change" tag.

fago’s picture

I'm unsure whether we should document this at all? Else, we'd have to go and document for each key of drupal's info array keys whether it's alterable or not.
If it's unclear though whether something is alterable - and it is, then I think this needs to be mentioned in the docs.

For that issue that would mean, if no one documented it to be alterable for a specific entity type, assume it's not.

effulgentsia’s picture

Priority: Normal » Major
Status: Needs work » Fixed
Issue tags: -Needs documentation +API change

OK. Back to #87 status, priority, and tags.

Status: Fixed » Closed (fixed)
Issue tags: -API change

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