Problem/Motivation

Core has settled on using "field items" for variables that contain a FieldItemListInterface object.

Proposed resolution

Also do this for normalizers.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yched’s picture

Status: Active » Needs review
FileSize
5.58 KB

Found a couple more in other normalizer classes.

As a side note, the code in FieldItemNormalizer::createTranslatedInstance() looks really weird ? (left untouched)

Status: Needs review » Needs work

The last submitted patch, 1: normalizer_var_names-2143089-1.patch, failed testing.

yched’s picture

Status: Needs work » Needs review
FileSize
5.6 KB

Silly me. $item->getName() is the delta, not the field name.

yched’s picture

Title: Drupal\hal\Normalizer\FieldNormalizer param should be called $items, not $field » Cleanup Field normalizers
Component: hal.module » serialization.module
Issue tags: -Novice
FileSize
6.92 KB
2.02 KB

Extending the scope a bit, simplifies some really weird / needlessly convoluted code in FieldItemNormalizer::createTranslatedInstance() (used for denormalize())

The logic of what's done here escapes me, and it looks like it would use some double-checking given the current state of the entity/field translation API. The patch leaves the logic untouched, just simplifies it.

damiankloip’s picture

Looks like the code in FieldItemNormalizer::createTranslatedInstance() already got changed?

Status: Needs review » Needs work

The last submitted patch, 4: normalizer_var_names-2143089-4.patch, failed testing.

Wim Leers’s picture

Title: Cleanup Field normalizers » Clean up Field normalizers
Component: serialization.module » hal.module
Wim Leers’s picture

Assigned: Unassigned » yched
Priority: Normal » Minor
Status: Needs work » Needs review
Issue tags: +Quickfix
FileSize
5.22 KB

Looks like the code in FieldItemNormalizer::createTranslatedInstance() already got changed?

Indeed.

Rerolled #4.

Status: Needs review » Needs work

The last submitted patch, 9: normalizer_var_names-2143089-9.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Title: Clean up Field normalizers » Clean up \Drupal\hal\Normalizer\FieldNormalizer parameter names
Assigned: yched » Unassigned
Issue tags: -Quickfix +clean-up, +Novice, +php-novice
mradcliffe’s picture

Version: 8.3.x-dev » 8.4.x-dev
Issue tags: +Needs issue summary update, +Baltimore2017

This patch is most likely needing to be re-rolled, and we probably should go ahead and add @internal to the hal field item and field normalizer classes because of method signature changes.

damiankloip’s picture

No, I don't think these classes should be internal, most classes implementing some custom behaviour for fields would likely extend these. I'm not sure PHP cares about actual parameter names, more the types? So I don't think existing code would actually break? https://3v4l.org/5tCCK

Wim Leers’s picture

Yep. But at DrupalCon, there's been talk to make https://www.drupal.org/core/d8-bc-policy explicit, which apparently might also mean marking every single protected method as @internal (which I personally think is going too far). @mradcliffe was considering doing that as part of this issue. We talked at DrupalCon, and I explained to him that's polluting the issue scope; that that is something that should happen in bulk in another issue. He agreed. So we're good :)

damiankloip’s picture

ok, awesome!

cjgratacos’s picture

I was thinking of picking this issue, and do the reroll and cleanup, but I am a bit confused with the description of the issue with the changes inside the patch. The issue is stating that we need to change "$fields" with "$items" on the function signature uses a FieldItemList object, yet what was done is append the "_item" keyword to the "$field" variable. Was this the intend of the issue, or to just completely replace "fields" or "$field" with "$items" or "$item". Thanks in advance.

imadalin’s picture

I made a new patch to cover on the most recent 8.4.x

pritish.kumar’s picture

Status: Needs work » Needs review

Adding Needs Review Status for Queuing

Status: Needs review » Needs work

The last submitted patch, 20: normalizer_var_names-2143089-10.patch, failed testing.

c.nish2k3’s picture

Status: Needs work » Needs review
FileSize
3.91 KB

Corrected the patch for 8.4.x

Status: Needs review » Needs work

The last submitted patch, 23: normalizer_var_names-2143089-23.patch, failed testing.

mradcliffe’s picture

Thank you for getting a re-rolled patch, @imadalin, @c.nish2k3. It would help if in #23 you could create and upload an interdiff between patches so that it is easier to review the changes between patches.

It looks like there are some test failures.

Fatal error: Call to undefined method Drupal\Core\Field\BaseFieldDefinition::getFieldName() in /var/www/html/core/modules/hal/src/Normalizer/FieldItemNormalizer.php on line 41

It looks like the method is getName and not getFieldName.

imadalin’s picture

Redo patch.

PS. Anybody would have an idea why a patch made from PHPStorm would fail? I use that on other projects and never had an issue.

imadalin’s picture

imadalin’s picture

FileSize
571 bytes

Sorry, wrong interdiff file.

imadalin’s picture

Status: Needs work » Needs review

The last submitted patch, 26: clean_up-2143089-26.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 27: clean_up-2143089-27.patch, failed testing.

imadalin’s picture

Status: Needs work » Needs review
John Cook’s picture

Status: Needs review » Needs work

The code look good, but I'm a little confused.

The issue summary says that we should be using the variable name "$items" but the patch appears to using "$field_items". Am I understanding the issue correctly?

vegantriathlete’s picture

Issue tags: +dcco2017
bradjones1’s picture

Status: Needs work » Needs review
FileSize
3.16 KB

Re-rolled for 8.4.x; changes in #2768651: Let TimestampItem (de)normalize to/from RFC3339 timestamps, not UNIX timestamps, for better DX mean, I think, that the changes to FieldItemNormalizer.php do not need to be applied here, akin to the change observed in #9 above.

dawehner’s picture

Small changes like this sum up over time!

+++ b/core/modules/hal/src/Normalizer/FieldNormalizer.php
@@ -62,14 +61,14 @@ public function normalize($field, $format = NULL, array $context = []) {
    */
-  protected function normalizeFieldItems($field, $format, $context) {
-    $normalized_field_items = [];
-    if (!$field->isEmpty()) {
-      foreach ($field as $field_item) {
-        $normalized_field_items[] = $this->serializer->normalize($field_item, $format, $context);
+  protected function normalizeFieldItems($field_items, $format, $context) {
+    $normalized_items = [];
+    if (!$field_items->isEmpty()) {
+      foreach ($field_items as $field_item) {
+        $normalized_items[] = $this->serializer->normalize($field_item, $format, $context);
       }
     }
-    return $normalized_field_items;
+    return $normalized_items;
   }
 
 }

I don't get this rename to be honest. The field name afterwards seems worse than before ... At least for me $normalized_field_items was really descriptive.

vegantriathlete’s picture

Issue tags: -dcco2017

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.

Wim Leers’s picture

Status: Needs review » Needs work

I agree with #36. The last hunk of changes seems unnecessary. The rest looks good!

harsha012’s picture

Status: Needs work » Needs review
FileSize
2.97 KB
815 bytes

added patch

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Thank you @harsha012!

Wim Leers’s picture

Thanks!

damiankloip’s picture

Agreed, +1!

The last submitted patch, 27: clean_up-2143089-27.patch, failed testing. View results

xjm’s picture

Title: Clean up \Drupal\hal\Normalizer\FieldNormalizer parameter names » Clean up \Drupal\hal\Normalizer\FieldNormalizer parameter and variable names to refer to field items
Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/hal/src/Normalizer/FieldNormalizer.php
@@ -38,15 +38,14 @@ public function normalize($field, $format = NULL, array $context = []) {
-    $normalized = NestedArray::mergeDeepArray($normalized_field_items);
-    return $normalized;
+    return NestedArray::mergeDeepArray($normalized_field_items);

Technically this is out of scope. Not too concerned about that. However...

... I am pretty sure at least ContentEntityNormalizer needs the same change. In this bit:

    // If the fields to use were specified, only output those field values.     
    if (isset($context['included_fields'])) {
      $fields = [];
      foreach ($context['included_fields'] as $field_name) {
        $fields[] = $entity->get($field_name);
      }
    }
    else {
      $fields = $entity->getFields();
    }
    foreach ($fields as $field) {
      // Continue if the current user does not have access to view this field.  
      if (!$field->access('view', $context['account'])) {
        continue;
      }

      $normalized_property = $this->serializer->normalize($field, $format, $context);

(I could be wrong; please confirm.)

The parent class (ListNormalizer) has it named as $fieldItems rather than $field_items. Both are allowable in our coding standards currently.

dawehner’s picture

@xjm
Nice spot!

I went through all normalizers and found an additional instance where we named something field, even its a typed data property.

Personally meh about field_items vs. fieldItems

Wim Leers’s picture

Title: Clean up \Drupal\hal\Normalizer\FieldNormalizer parameter and variable names to refer to field items » Clean up normalizer parameter and variable names to use "field_items" consistently etc

#45: nice spot, although that's technically expanding the issue scope ;) Updating issue title to reflect #45+#46.

+++ b/core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php
@@ -21,9 +21,9 @@ public function normalize($object, $format = NULL, array $context = []) {
-    foreach ($object as $name => $field) {
...
+    foreach ($object as $name => $field_items) {

If we're making this change, then we should probably also rename $object to $entity.

(Which is also what \Drupal\hal\Normalizer\ContentEntityNormalizer::normalize() already does.)

dawehner’s picture

Good point!

Wim Leers’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs issue summary update
dawehner’s picture

Thank you @Wim Leers!

  • catch committed fb3c8d4 on 8.5.x
    Issue #2143089 by imadalin, yched, dawehner, harsha012, Wim Leers, c....

  • catch committed b80f368 on 8.4.x
    Issue #2143089 by imadalin, yched, dawehner, harsha012, Wim Leers, c....

catch credited catch.

catch’s picture

Version: 8.5.x-dev » 8.4.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.5.x and cherry-picked to 8.4.x. Thanks!

Status: Fixed » Closed (fixed)

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