As follow-up to #2116341: Apply defaults to definition objects, there are two @todo comments that links to a closed issue. Since they are referring to changes already done, they should be removed, and the BaseFieldDefinition::getFieldItemClass() method deprecated. Instead, the class should simply use $this->getItemDefinition()->getClass().

No other change as needed, since the other @todo comment is describing what the current code is already doing.

Comments

kiamlaluno created an issue. See original summary.

avpaderno’s picture

Issue summary: View changes
avpaderno’s picture

Status: Active » Needs review
StatusFileSize
new2.01 KB

Status: Needs review » Needs work

The last submitted patch, 3: remove-getFieldItemClass-2933964-3.patch, failed testing. View results

avpaderno’s picture

Status: Needs work » Needs review
StatusFileSize
new2.43 KB

Status: Needs review » Needs work

The last submitted patch, 5: remove-getFieldItemClass-2933964-5.patch, failed testing. View results

avpaderno’s picture

Title: Remove BaseFiedDefinition::getFieldItemClass() » Remove the left-over @todo comment
Status: Needs work » Needs review
StatusFileSize
new593 bytes

After having done some tests, and read the code, I am convinced the comment is just a left-over. There isn't any @todo to achieve.

oakulm’s picture

Assigned: Unassigned » oakulm
StatusFileSize
new874 bytes

Hi,

if I have understood this correctly the unnecessary code has to be removed as well as the @todo comment.

Patch https://www.drupal.org/files/issues/d8_definition_defaults_3.patch in https://www.drupal.org/node/2116341 adds in the functionality that is currently in the function we are talking about. See:

  protected function getFieldItemClass() {
    if ($class = $this->getItemDefinition()->getClass()) {
      return $class;
    }
    else {
      $type_definition = \Drupal::typedDataManager()
        ->getDefinition($this->getItemDefinition()->getDataType());
      return $type_definition['class'];
    }
  }

vs.

  public function getClass() {
    if (isset($this->definition['class'])) {
      return $this->definition['class'];
    }
    else {
      $type_definition = \Drupal::typedDataManager()->getDefinition($this->getDataType());
      return $type_definition['class'];
    }
  }

I have attached patch removing the duplicate code.

avpaderno’s picture

StatusFileSize
new2.07 KB
avpaderno’s picture

StatusFileSize
new2.67 KB

There is another @todo comment, but it seems describing exactly what the code after it is doing.

    // Create a definition for the items, and initialize it with the default
    // settings for the field type.
    // @todo Cleanup in https://www.drupal.org/node/2116341.
    $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
    $default_settings = $field_type_manager->getDefaultStorageSettings($type) + $field_type_manager->getDefaultFieldSettings($type);
    $field_definition->itemDefinition->setSettings($default_settings);
    return $field_definition;

Status: Needs review » Needs work

The last submitted patch, 10: remove-left-over-2933964-10.patch, failed testing. View results

avpaderno’s picture

Status: Needs work » Needs review
oakulm’s picture

Status: Needs review » Reviewed & tested by the community

Yes this was my thinking also with this. Better to remove the whole function.

xjm’s picture

Title: Remove the left-over @todo comment » Deprecate BaseFieldDefinition::getFieldItemClass()
Status: Reviewed & tested by the community » Needs work

BaseFieldDefinition is public API, so I don't think we can get away with removing the protected method anymore following the release of 8.0.0. What we can do instead though is remove the usages of it and instead deprecate it. Reference: https://www.drupal.org/core/deprecation

Thanks!

avpaderno’s picture

@xjm BaseFieldDefinition::getFieldItemClass() is a protected method, not exposed from any interface, and in Drupal 8 backwards compatibility and internal API policy, I read:

In general, only interfaces can be relied on as the public API.

Protected methods of a class should be assumed @internal and subject to change unless either the class or method itself are marked with @api. Drupal leaves most internal methods protected rather than private to allow for one-off customized subclasses when needed, but in most cases that "escape hatch" should not be relied upon indefinitely. If no alternative presents itself consider filing a feature request for a more directly supported approach.

The patch can eventually keep the method. The purpose of the patch is removing those @todo comments and change the code where needed.

xjm’s picture

@kiamlaluno, see: https://www.drupal.org/core/deprecation#internal

So, again, let's remove the usages and deprecate the method instead of removing it. Thanks!

avpaderno’s picture

Title: Deprecate BaseFieldDefinition::getFieldItemClass() » Remove the left-over @todo comments in the BaseFieldDefinition class
Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new2.54 KB
oakulm’s picture

Status: Needs review » Reviewed & tested by the community

I was thinking the same thing should we remove the function or no. But this final patch seems to do the job and applies as it should.

xjm’s picture

Status: Reviewed & tested by the community » Needs work

Thanks, #17 looks good! We need just one more thing, a @trigger_error() inside the function so that it gets removed. Example here: https://www.drupal.org/core/deprecation#how-method

+++ b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php
@@ -641,15 +640,15 @@ public function getPropertyNames() {
+   * Use \Drupal\Core\TypedData\ListDataDefinition::getClass() instead.

Small thing, this docs line should be indented by two spaces.

oakulm’s picture

Status: Needs work » Needs review
StatusFileSize
new2.78 KB

Good catch. Also this trigger_error is new to me. Added trigger_error & added two spaces.

avpaderno’s picture

StatusFileSize
new2.84 KB

I would also add a link to this very issue, as other @trigger_error() calls in that class do.

oakulm’s picture

Status: Needs review » Reviewed & tested by the community
xjm’s picture

Status: Reviewed & tested by the community » Fixed

Okay, that looks good! We don't need a change record for this one since it was only ever an internal helper; the deprecation notice by itself is sufficient in this case.

  • xjm committed d039f07 on 8.5.x
    Issue #2933964 by kiamlaluno, oakulm: Remove the left-over @todo...

Status: Fixed » Closed (fixed)

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