Comments

fago’s picture

Title: Implement the property API for all field types » Implement the new entity field API for all field types
Project: D8 Entity API improvements » Drupal core
Version: » 8.x-dev
Component: Entity Property API » entity system
Issue tags: +Entity Field API

Moving to the core queue.

chx’s picture

Issue summary: View changes

Updated issue summary.

yched’s picture

A couple hints on what exactly needs to be done would probably help ;-)

amitaibu’s picture

Little help for others:

amitaibu’s picture

@fago,
I'm working on adding property info for ER, some questions:

  1. Should I move the current EntityReferenceItem under ER module?
  2. The current implementation assumes $this->definition['settings']['entity type']; but in ER the value is in $this->definition['settings']['target_type']; -- should I rename the keys?
  3. ER comes with revision ID support. If the key is NULL we use entity_load, otherwise, we load by revision. How should I treat 'settings' => array('id source' => 'value'), as the source ID is infact chaned on the fly per delta (i.e. not even on the field level, but on the field values)
fago’s picture

Ok, so I try to give a summary of what needs to be done right now:

  • Add a field item class which extends from FieldItemBase. For an example I'd suggest looking at #1839070: Implement the new entity field API for the taxonomy reference field type. The actual implementation of the class is probably similar to one of the existing ones, thus check the classes below \Drupal\core\Entity\Field\Type for examples also.
  • The added class should live below the field type's module \Type namespace. Then the class has to specified using hook_field_info, see the term reference patch for an example.
  • As mentioned: Don't try it on nodes, as it won't work. currently only entity_test implements the new entity field API -- so all your tests have to be against that entity type.
  • A test case should check the proper interfaces are implement (see term example) and make sure reading and writing values works as intended.
  • Existing field type tests mostly use the test_entity type, which we cannot use. So either write a new test case using entity_test or migrate the existing one to use entity_test and extend it. Also see #1822000: Remove Drupal\field_test\Plugin\Entity\Type\TestEntity in favor of EntityTest.
fago’s picture

Should I move the current EntityReferenceItem under ER module

Yep, let's do so. This creates more dependencies on field type modules but as long as we've that modules I think we should have the implementations bundled there.

The current implementation assumes $this->definition['settings']['entity type']; but in ER the value is in $this->definition['settings']['target_type']; -- should I rename the keys?

I'd prefer 'entity type' but I'm not keen on bikesheding this ;)

ER comes with revision ID support. If the key is NULL we use entity_load, otherwise, we load by revision. How should I treat 'settings' => array('id source' => 'value'), as the source ID is infact chaned on the fly per delta (i.e. not even on the field level, but on the field values)

That's fine, the ER field keeps a referene on the 'id source' property so it will reflect any changes. However, what about having a separate type for revision references? API wise I'd prefer to have something like type => entity_revision - as it's quite different to a regular reference.

amitaibu’s picture

> However, what about having a separate type for revision references?

The thing is that the same field on the same entity, can reference one time an entity by ID, and one time reference by revision ID. for example:

field_reference['und'][0] = array('target_id' => 1, 'revision_id' => NULL); // Use entity_load()
field_reference['und'][1] = array('target_id' => 5, 'revision_id' => 10); // Use entity_revision_load()
fago’s picture

Is that valuable to have in the same field?

But anyway, couldn't we still do 'entity_revision' as type and have

$field_item->entity
$field_item->revision

So even if you reference a revision ->entity would give you the active revision of the entity?

amitaibu’s picture

> Is that valuable to have in the same field?

I guess this is something we need to figure out. The revision ID support doesn't exist yet in D7, so I don't have experience with it. I wonder if we should add a checkbox to the UI, saying "Use revision ID" -- in that case I don't even know how/ if we should show a widget.

I could start with a simpler implementation to deal only with the entity ID, and not with the revision ID, until we figure out how it should work.

yched’s picture

Re: 'entity type' :
Please keep in mind that with 'field types as plugins', all of these will be in annotations, so we should settle on underscore separated properties.

fago’s picture

true. I wasn't aware of that restriction when writing the typed data api initially. I guess we'll have to migrate it to underscores, yeah.

yched’s picture

Right. Yaml keys in CMI files are another reason, actually.

Also, 'field types as plugins' means field type implementations will consist in providing one 'field type handler' class - which probably won't be the same as the 'field item (aka value)' class mentioned here. That would mean providing two classes for a field type.

Or is there a base 'field item' class that could be the same for most field types, and reference fields being the exception that needs to provide a dedicated item class ?

Berdir’s picture

#1839070: Implement the new entity field API for the taxonomy reference field type now contains a working example of what needs to be done.

Note that this issue currently only covers the first step of this conversion. The whole thing is tracked in #1346214: [meta] Unified Entity Field API but doesn't list this issue yet. Once this is done and all entity types are converted to NG, we can start converting how we access these fields and also convert Field API itself. Then we can finally remove the BC layer :)

fago’s picture

Also, 'field types as plugins' means field type implementations will consist in providing one 'field type handler' class - which probably won't be the same as the 'field item (aka value)' class mentioned here. That would mean providing two classes for a field type.

Or is there a base 'field item' class that could be the same for most field types, and reference fields being the exception that needs to provide a dedicated item class ?

I think we'll always need a custom field item class, as that's the one you are implementing for providing the defintition of contained properties/columns.

But as we discussed in Munich, I think we could base field types solely on the field-item classes. We could move everything more needed by field API into it for now (add another interface for the extensions), and then re-factor so that any possibly remaining duplicating functionality is unified.
Most problematic are probably the field-level crud hooks, but in the long term I think we could just replace them by computed properties and by moving the few modules that are using it to use entity-crud hooks instead.

yched’s picture

I think we'll always need a custom field item class, as that's the one you are implementing for providing the defintition of contained properties/columns.

Well, precisely, providing the definition of contained properties/columns was not something I would have considered for the 'item' class, because you want that list in cases where you don't have an actual field value from an actual entity - like, to create the sql table on creation of the field.
There are also the methods that operate on multiple entities (like 'load', 'prepare_view'), and therefore on multiple items.

Well, I won't be focusing on "field types as plugins" before Dec 1st anyway, we'll have opportunities to discuss this :-)

yched’s picture

Issue summary: View changes

Updated issue summary.

fago’s picture

Well, I won't be focusing on "field types as plugins" before Dec 1st anyway, we'll have opportunities to discuss this :-)

Yeah, let's discuss this more afterwards.

But note that it's perfectly fine to instantate field items without a value, so one can use the typedData API to derive information without nested properties without values. So that's not a problem. However yes, multiple-callbacks would not necessarily fit into that so well, so that's something we'd have to look at.

amitaibu’s picture

@fago,
How do I get the field name in getPropertyDefinitions()?
I expected that implementing ContextAwareInterface would be enough, however when I $this->getParent() I don't see the field name.

fago’s picture

There is getName() which gets you the name of a property, i.e. $entity->field->getName() gets you the field name.

amitaibu’s picture

No, I mean something else-- in EntityReferenceItem::getPropertyDefinitions() we currently have :
$entity_type = $this->definitions['settings']['entity type']

But this is of course not yet populated. The entity-type should be extracted from the field info, so I expected to do something like:

$field = field_info_field($this->getParent());
$target_type = $field['settings']['target_type'];

But even though I changed class EntityReferenceItem to extend ContextAwareInterface $this->getParent() doesn't work.

fago’s picture

hm, the target-type is static information so I think it should be part of the definition always also. So what about adding it in via field_entity_field_info_alter() and add a todo to unify this keys? (If not the whole field-info)

amitaibu’s picture

catch’s picture

Priority: Normal » Critical

Bumping status.

plach’s picture

Title: Implement the new entity field API for all field types » [meta] Implement the new entity field API for all field types
plach’s picture

Issue summary: View changes

Updated issue summary.

Berdir’s picture

Issue summary: View changes

add telephone issue

arlinsandbulte’s picture

An ignorant question:
Does this need to be done for the new Date field type too?

Berdir’s picture

the date field type already provides a DateItem class, the only thing that is missing are are unit tests for it I think like all other field types now have.

plach’s picture

Status: Active » Fixed

The last issue in the OP was committed so I guess we can mark this fixed. Yay!

xjm’s picture

Yay!

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

Anonymous’s picture

Issue summary: View changes

Updated issue summary.