Updated: Comment #N

Problem/Motivation

MapItem::propertyDefinitions() currently claims that it has a single property named value. That doesn't sense, it can have any number of properties and value is very likely not one of them.

This came up in #597236: Add entity caching to core, which tries to use toArray() to get the values, which doesn't return anything useful for MapItem's right now.

Proposed resolution

The new method doesn't support to define this dynamically, so when called statically, we should return an empty list.

However, we can override getPropertyDefinitions() and when called dynamically, return the actual properties that we have right now, with type any, as we used to.

Remaining tasks

User interface changes

API changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir’s picture

Title: MapItem::propertyDefinitions() is wrong » MapItem::propertyDefinitions() claims it has a static value propertiy, which is wrong
Wim Leers’s picture

Title: MapItem::propertyDefinitions() claims it has a static value propertiy, which is wrong » MapItem::propertyDefinitions() claims it has a static value property, which is wrong
Berdir’s picture

Status: Active » Needs review
FileSize
1.02 KB

Something like this.

fago’s picture

+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/MapItem.php
@@ -27,10 +27,20 @@ class MapItem extends FieldItemBase {
+    // Return the definitions for the currently set values.
+    $definitions = array();
+    foreach ($this->values as $name => $value) {

I'm not sure we should have that auto-generated values even? Once we'd have some static definitions they would be overwritten by what ever you set on the map, what seems wrong.

We could support defining some metadata for contained properties in the field settings + return them in static propertyDefinitions(). That would allow per instance definitions of contained properties while properly returning that from the field definition also.

Berdir’s picture

Good point but that does mean that we need to toArray() override from the other issue.

pwolanin’s picture

Status: Needs review » Reviewed & tested by the community

I just got a form error due to cross- post. I was about to say it seems to prevent getting a stray "value" element serialized into the database when using this field type in a new content entity, so that's good, but an alternative to using the any type is to override toArray().

Obviously, you agree. This new patch looks good and also resolves the problem I see.

pwolanin’s picture

To clarify my comment, the kind of thing I was seeing without this patch when creating a content entity with a MapItem field was:

   'routeParameters' => 
  array (
    'node' => '3',
    'value' => NULL,
  ),

where the 'value' key should not be present.

pwolanin’s picture

Status: Reviewed & tested by the community » Needs work

oops, actually the code comments need some grammer and punctuations fixes.

pwolanin’s picture

Status: Needs work » Needs review
FileSize
1.37 KB
1.35 KB

I think this text is clearer.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

simple docs fix

fago’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
1.35 KB
425 bytes

Changes look good, removed a superfluous empty line though.

pwolanin’s picture

Status: Needs review » Reviewed & tested by the community

thanks - just a blank line back to rtbc

pwolanin’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
834 bytes
1.2 KB

per discussion with alexpott & berdir in IRC, just go back to the simple version since property_definitions is not used in core and should be documented and tested in a follow-up.

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Yeah, agreed, we shouldn't introduce a badly documented and untested feature because in case someone will actually need it, it would be broken somehow anyway ;)

pwolanin’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 78594ab and pushed to 8.x. Thanks!

  • Commit 78594ab on 8.x by alexpott:
    Issue #2229181 by pwolanin, Berdir, fago: MapItem::propertyDefinitions...

Status: Fixed » Closed (fixed)

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

tstoeckler’s picture