Support from Acquia helps fund testing for Drupal Acquia logo

Comments

piggito created an issue. See original summary.

piggito’s picture

The options field is defined as MapDataDefinition but the Map doesn't have any PropertyDefinition. Then, the ComplexDataNormalizer fails to loop over the properties in the Map and returns an empty array.

I'm uploading a patch which changes the options field from MapDataDefinition to DataDefinition::create('any') to overcome the issue.

piggito’s picture

Status: Active » Needs review
andypost’s picture

Component: link.module » entity system

Btw maybe kind of fix for normalization of MapDataDefinition required?

andypost’s picture

Issue tags: +Needs tests

It needs unit test to make sure ComplexDataNormalizer works fine

Status: Needs review » Needs work

The last submitted patch, 2: link_options_serialization-2876686-2.patch, failed testing.

tacituseu’s picture

Status: Needs work » Needs review

Unrelated failure, sorry, gave testbot a bit of a headache, might take a while to recover.

Status: Needs review » Needs work

The last submitted patch, 2: link_options_serialization-2876686-2.patch, failed testing.

piggito’s picture

My exact scenario was with menu_link_content so I'm adding a Kernel test for serialization of menu_link_content.

piggito’s picture

Component: entity system » menu_link_content.module
piggito’s picture

Status: Needs work » Needs review
piggito’s picture

Status: Needs review » Needs work

The last submitted patch, 9: menu_link_content_serialization-test.patch, failed testing.

andypost’s picture

Version: 8.3.x-dev » 8.4.x-dev
Issue tags: -Needs tests +serialization

Now it needs aggregated patch #2+#9

piggito’s picture

Component: menu_link_content.module » link.module
FileSize
3.89 KB

The menu link content test was just 1 scenario of LinkItem serialization issue so I created a new Kernel test to check serialization of LinkItems. I also include a new LinkItemNormalizer to solve the issue.

piggito’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 15: serialization_of_link-2876686-15.patch, failed testing.

piggito’s picture

Status: Needs work » Needs review
FileSize
3.91 KB
444 bytes

Adding priority 20 to LinkItemNormalizer to make sure it is executed instead of ComplexDataNormalizer

Status: Needs review » Needs work

The last submitted patch, 18: serialization_of_link-2876686-18.patch, failed testing.

Wim Leers’s picture

Title: Serialization of link options » Normalization of LinkItem's 'options' property
Issue tags: -serialization +API-First Initiative
+++ b/core/modules/link/src/Normalizer/LinkItemNormalizer.php
@@ -0,0 +1,39 @@
+class LinkItemNormalizer extends NormalizerBase {
...
+  protected $supportedInterfaceOrClass = 'Drupal\link\Plugin\Field\FieldType\LinkItem';

Hm… but:

    $properties['options'] = MapDataDefinition::create()
      ->setLabel(t('Options'));

So isn't the problem that maps aren't normalized correctly?

Wim Leers’s picture

piggito’s picture

So isn't the problem that maps aren't normalized correctly?

I don't think it is a Map issue but the implementation of Map in LinkItem. The options attribute in LinkItem's definition is a Map but doesn't have its properties defined there and the ComplexDataNormalizer uses these properties to loop during normalization process.

piggito’s picture

Status: Needs work » Needs review
FileSize
3.37 KB

I changed the approach and added options attributes in MapDefinition according to Url::fromUri
This way we can still use ComplexDataNormalizer

Status: Needs review » Needs work

The last submitted patch, 23: normalization_of-2876686-23.patch, failed testing.

piggito’s picture

Status: Needs work » Needs review
FileSize
4.1 KB

It seems like the approach in #23 broke a lot of stuff so I came back to creating a LinkItem Normalizer that includes options property.

Status: Needs review » Needs work

The last submitted patch, 25: normalization_of-2876686-25.patch, failed testing.

piggito’s picture

Status: Needs work » Needs review
FileSize
6.02 KB
4.67 KB

It required a default normalizer and another one for hal module so I'm adding the patch.

larowlan’s picture

Wim Leers’s picture

Title: Normalization of LinkItem's 'options' property » [PP-1] Normalization of LinkItem's 'options' property
Status: Needs review » Postponed
Related issues: +#2871591: Allow ComplexData in TypedData to specify computed properties that should be exposed in normalization and other contexts

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Berdir’s picture

@WimLeers: That issue is not the solution for every problem :)

options is *not* a computed property, so I'm not sure that issue will help. It is possible a more generic solution is possible, but not through computed properties but generic support for exporting map properties.

damiankloip’s picture

Status: Postponed » Active

If we do need a new normalizer here, it should be one on the data property level instead, i.e. a MapNormalizer? Another up side of that is that it would only need one normalizer in serialization module, not a field level implementation in both hal and serialization.

+++ b/core/modules/link/src/Normalizer/LinkItemNormalizer.php
@@ -0,0 +1,39 @@
+      if ($name == 'options') {
+        $options_attributes = [];
+        foreach ($field->getValue() as $key => $val) {
+          $options_attributes[$key] = $this->normalize($val, $format, $context);
+        }
+        $attributes['options'] = $options_attributes;
+      }

What you're doing here is basically what a MapNormalizer could/should/would do.

Tentatively moving back to open.

Wim, let me know if you still don't agree :)

lawxen’s picture

Issue summary: View changes
Status: Active » Closed (duplicate)

This issue has been fixed on other issueMapData cannot be serialized by using a generic solution
which solved all data's normalization's problem that was defined as MapDataDefinition
1. core/LinkItem
2. commerce
3. BlockField
4. ...

andypost’s picture

Issue summary: View changes
Status: Closed (duplicate) » Active
Related issues: +#2895532: @DataType=map cannot be normalized, affects @FieldType=link, @FieldType=map

@liuxin the issue is not solved and related #2895532: @DataType=map cannot be normalized, affects @FieldType=link, @FieldType=map is not a half way in, I'd better marked related as duplicate

lawxen’s picture

Hi @andypost,
I don't understand

is not a half way in

It means "it's haven't been completely solved?" Or "it's not a shortcut“
Could you explain further?thanks

lawxen’s picture

@andypost,Sorry for closing the issue hastily before,
The related #2895532: MapData cannot be serialized issue's resolution has divergence now
So, we should rethinking in the issue

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Berdir’s picture

Status: Active » Closed (duplicate)

I agree with the duplicate. The other issue is close and among other things, has specific test coverage for the link field.