The Field code is littered with a term, "entity", that apparently is not defined anywhere in the code or documentation. It's not in the Field API glossary, for example.

This is very confusing to new developers like myself. I propose that:

  • "Entity" should be replaced everywhere by its new-fangled synonym, "fieldable object type".
  • Because $fieldable_object_type is a typist's nightmare, "$entity" should be replaced by "$obj_type". I wish I could claim credit for this idea, but in fact half the code is already doing this.

Of course, it's entirely possible that I am missing some crucial semantic distinction between "entities" and "fieldable object types". And that's my point: If that distinction exists, I'll never figure it out. The terminology situation in the code is much more puzzling than it needs to be.

Comments

bjaspan’s picture

There is no difference. Patches welcome. :-)

yched’s picture

Due to the lack of a preexistent unified Data API, Field API had to invent its terminology.

'entity' (resp 'entity type') is rather an equivalent of 'object' (resp 'object type') with the following nuances :
- 'object' is a both heavily generic and well defined term in PHP (or in CS more generally), our notion doesn't quite overlap.
- 'entity' is a candidate name to designate more specifically 'one of the data items handled by the various drupal APIs', fieldable or not.
#460320: Standarized, pluggable entity loading (nodes, users, taxonomy, files, comments) attempts to build more stuff on the notion.

The current uses of 'entity', 'object type', 'object' in the API, variable names and PHPdocs might not be optimal. As Barry said, patches are welcome to propose enhancements, improve consistency or make concepts clearer, but I'm not sure removing 'entity' in favor of 'object' is a good move.

bjaspan’s picture

Well, apparently the last paragraph in Mike's post was quite prophetic:

Of course, it's entirely possible that I am missing some crucial semantic distinction between "entities" and "fieldable object types". And that's my point: If that distinction exists, I'll never figure it out.

I wasn't aware that we were thinking of 'entity' as a distinct term from 'object', I thought they were just synonyms.

Based on #2, perhaps we should replace 'obj' and 'entity' everywhere in Field API?

mike booth’s picture

I have no problem with defining "entity" as "one of the data items handled by the Drupal API". Although I want a stricter definition (I need to be able to answer the question "is $foo an entity or not" unambiguously) and I want that definition documented somewhere.

But here's my question for this issue: Are all fieldable objects entities? If they are, then it seems to me that the word "entity" is such an empty term that it has no reason to exist. And if they aren't, the word 'entity' needs to be replaced in many places, because the use of that word is misleading: Fields aren't restricted to entities.

It's clear that, by definition, nodes, users, taxonomy, and files are all entities. But suppose I want to attach some fields to an arbitrary Flickr photo. Let's assume that Flickr photos have a unique numerical ID that's embedded in the URL, so whenever a user hands me a Flickr photo URL I can just create an object and pass it to field_attach_load():

  $photo = new StdClass();
  $photo->flickr_id = 3159030384;
  $photo->bundle = 'flickr_photo';
  field_attach_load('flickr_photo', $photo);

This will work fine provided that $photo is a fieldable object. But, so long as my custom module defines a hook_fieldable_info() that returns something like this:

return array(
  'flickr_photo' => array(
     'name' => t('Flickr photo'),
     'id key' => 'flickr_id',
     'revision key' => NULL,
     'bundle key' => 'bundle',
     'cacheable' => TRUE,
     'bundles' => array('flickr'),
  ),
);

$photo is a fieldable object, yes?

But is $photo an entity? Surely not, right? There is no Drupal API that handles such objects. They don't have an API or even a class. I made them up myself in fourteen lines of PHP, some of which I believe are redundant. ;) If we declare that objects like $photo are entities, then I declare that the word "entity" is useless, certainly not worth the hassle of teaching it to all new Drupal programmers. Let's just call these things by the name they already have: "objects".

yched’s picture

IMO the answer lies in #460320: Standarized, pluggable entity loading (nodes, users, taxonomy, files, comments) (whether it gets commented or not, BTW), which makes 'file' as an entity type, but not fieldable ('not' possibly meaning 'not yet', but that's not relevant)

So:
- Field API is the only thing *right now* that uses the notion of 'entity'. It needed this generic notion to be able practically to operate on 'things that can be nodes or other stuff'. It's not the job of the Field API to define theoretically what an entity is aside from its 'fieldable' aspect.
- If #460320: Standarized, pluggable entity loading (nodes, users, taxonomy, files, comments) gets in, the you have your answer : an entity type is something that implements hook_entity_info(). Being fieldable implies you are an entity. Being an entity doesn't imply you are fieldable.
- Being fieldable means that a) you do the job of calling the right field_attach_*() functions at the right time in your workflow b) you declare yourself 'fieldable' in your hook_entity_info()

bjaspan’s picture

What's funny here is that I thought I knew the answer to Mike's original question and now I wonder if I am confused.

mike booth’s picture

Okay, I obviously need to read through #460320 in more detail. I missed the key phrase, where hook_entity_info() replaced hook_fieldable_info() and it suddenly became a requirement that every fieldable item also be an entity.

At first glance, I'm very dubious of that. I thought the ability to add fields to a simple StdClass(), without having to deal with or even know about the semantics of "entities", was a nice feature, and I hope there's a big payoff to getting rid of that feature. But, again, I haven't read the other patch.

Sounds like the right thing to do might be to postpone this issue until #460320 is hashed out.

yched’s picture

I thought the ability to add fields to a simple StdClass(), without having to deal with or even know about the semantics of "entities" was a nice feature, and I hope there's a big payoff to getting rid of that feature

I think you're getting this wrong. #460320: Standarized, pluggable entity loading (nodes, users, taxonomy, files, comments) doesn't put more burden in any way on you. There's *no* cost additional in implementing hook_entity_info(). It doesn't force you to do anything, write any additional code, add any specific feature. It *allows* you to say 'I'm fieldable' (and then you have some work to do), or 'I want to benefit from the multiple load handlers' (and then you have some work to do).

also, I forgot to followup on that part of #4:

But is $photo an entity? Surely not, right? There is no Drupal API that handles such objects

Of course there is : the one, even minimal, that your module defines in order to be able to 'talk about' filcker photos. So, sure, the mere fact that you have code, be it core or contrib, that handles Flicker Photos, make flicker photos a de-facto 'entity'.

mike booth’s picture

Title: Remove obsolete term "entity" from the code » Document the term "entity" and use it consistently

Okay, I have gone back and forth several times on this, but I think I might be ready to concede that "entity" is a useful term. Perhaps useful enough that it's worth creating yet another Drupal jargon word.

Here's the one-line take-home version of my proposed definition:

  • An "entity" is an object which is identified by a unique ID that is stored inside it.

Here's some fleshing out of the definition:

  • Each entity has a type. Entity types must be declared to Drupal using hook_entity_info().
  • Entities have a single "id" field, and may also have a "revision" field. The (id, revision) combination uniquely identifies the entity. The field names do not actually need to be "id" and "revision"; they are defined for each entity type by hook_entity_info(). For example, a node entity has its ID in $node->nid and its revision in $node->revision, while user entities have $user->uid as their ID field.
  • Entities are the only objects that can be fieldable, though not all entity types are necessarily fieldable.
  • Entities may be loadable using Drupal's loading API, but not all entity types are necessarily loadable.
  • (...insert future optional properties of entities here...)

I was not convinced that the word "entity" was useful until I realized that the one common factor between the four subtypes of entity which we have mentioned so far (fieldable vs non-fieldable, loadable vs non-loadable) is that they all require PHP objects with a unique ID. And I'll accept that that concept deserves its own word. Indeed, now that I think about it, I've been looking for that word for a while.

So I'm altering the suggested agenda of this issue: If we want to go with the term "entity", we need to use it everywhere, we need to put it in the glossary, we need to define it in the Doxygen docs. And we need to do these things as soon as we can, perhaps even without waiting for #460320 to land. Right now all the word is doing is confusing people like me who try to read the Field code.

yched’s picture

Title: Document the term "entity" and use it consistently » Use the term 'entity' instead of 'object'

#460320: Standarized, pluggable entity loading (nodes, users, taxonomy, files, comments) just further pushed 'entity' as an official drupal term.
So I guess a followup task would be to consistently replace our mentions of 'object' by 'entity'... Obviously post code freeze, if ever...

matt2000’s picture

subscribe

yched’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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