Problem/Motivation

The \Drupal\Core\TypedData\DataDefinition implements ArrayAccess with a 'this is for bc support only' comment thus:

/**
   * {@inheritdoc}
   *
   * This is for BC support only.
   * @todo: Remove in https://www.drupal.org/node/1928868.
   */
  public function offsetExists($offset) {
    // PHP's array access does not work correctly with isset(), so we have to
    // bake isset() in here. See https://bugs.php.net/bug.php?id=41727.
    return array_key_exists($offset, $this->definition) && isset($this->definition[$offset]);
  }

  /**
   * {@inheritdoc}
   *
   * This is for BC support only.
   * @todo: Remove in https://www.drupal.org/node/1928868.
   */
  public function &offsetGet($offset) {
    if (!isset($this->definition[$offset])) {
      $this->definition[$offset] = NULL;
    }
    return $this->definition[$offset];
  }

  /**
   * {@inheritdoc}
   *
   * This is for BC support only.
   * @todo: Remove in https://www.drupal.org/node/1928868.
   */
  public function offsetSet($offset, $value) {
    $this->definition[$offset] = $value;
  }

  /**
   * {@inheritdoc}
   *
   * This is for BC support only.
   * @todo: Remove in https://www.drupal.org/node/1928868.
   */
  public function offsetUnset($offset) {
    unset($this->definition[$offset]);
  }

However the referenced issue is closed - fixed - #1928868: Typed config incorrectly implements Typed Data interfaces

So do we want to remove it or just remove the comment?

Proposed resolution

- throw deprecation in ArrayAccess methods (like patch in comment #7)
- add deprecation test
- file issue to get rid of the methods in next major core release

Doing the deprecation-dance breaks just about every test known to Drupal CI: See #18.

Let's remove the comment and leave it in.

Let's trigger a deprecation error and observe what fails?

Result from #18: https://dispatcher.drupalci.org/job/drupal_patches/203233/#showFailuresLink

Test Result (2,578 failures / +2576)

It means ~10% so challenging but doable

Remaining tasks

patch/review/commit

User interface changes

no

API changes

accessing ArrayAccess methods directly or indirectly will throw deprecation
- \Drupal\Core\TypedData\DataDefinition::offsetExists()
- \Drupal\Core\TypedData\DataDefinition::offsetGet()
- \Drupal\Core\TypedData\DataDefinition::offsetSet()
- \Drupal\Core\TypedData\DataDefinition::offsetUnset()

Data model changes

no

Release notes snippet

CommentFileSizeAuthor
#18 3032503-18.patch1.82 KBspokje
#7 3032503-7.patch1.97 KBandypost
#4 3032503.patch1.86 KBlarowlan

Issue fork drupal-3032503

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

larowlan created an issue. See original summary.

larowlan’s picture

Personally, I'm +1 on retaining it and removing the comment

larowlan’s picture

Title: Decide on future of ArrayAccess support to DataDefinition » Decide on future of ArrayAccess support in DataDefinition
larowlan’s picture

Status: Active » Needs review
Issue tags: +deprecated
StatusFileSize
new1.86 KB

What does this break?

Obviously we can't do this, but we can do the trigger_error deprecation mambo

Status: Needs review » Needs work

The last submitted patch, 4: 3032503.patch, failed testing. View results

wim leers’s picture

+++ b/core/lib/Drupal/Core/TypedData/DataDefinition.php
@@ -295,51 +295,6 @@ public function addConstraint($constraint_name, $options = NULL) {
-   * @todo: Remove in https://www.drupal.org/node/1928868.

Interesting! Linking that issue.

but we can do the trigger_error deprecation mambo

Indeed. Any simplification we can make would be very welcome I think.🕺

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new1.97 KB

Let's see how many things affected by "mambo"))

Status: Needs review » Needs work

The last submitted patch, 7: 3032503-7.patch, failed testing. View results

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

spokje’s picture

StatusFileSize
new1.82 KB
spokje’s picture

Issue summary: View changes

spokje’s picture

Status: Needs work » Needs review
andypost’s picture

Looks there's no way to remove it, so removal of todo probably is enough

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Removal of todo seems good.

lauriii’s picture

Status: Reviewed & tested by the community » Needs review

It looks like even the typed data internals rely on the array access, like for example \Drupal\Core\Config\TypedConfigManager::buildDataDefinition. I guess if we could get rid of the typed data system not internally using array access, we could get a real list of test fails.

If we decide that's not worth it, I wonder if we should remove the "This is for BC support only." comment too.

wim leers’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record

Agreed with @lauriii.

I don't understand why it'd make sense to keep BC support forever and simply remove the @todo.

Let's trigger a deprecation error and observe what fails?

spokje’s picture

Let's trigger a deprecation error and observe what fails?

All patches in this issue did exactly that.

Result from #18: https://dispatcher.drupalci.org/job/drupal_patches/203233/#showFailuresLink

Test Result (2,578 failures / +2576)

andypost’s picture

+2576

It means ~10% so challenging but doable

spokje’s picture

Title: Decide on future of ArrayAccess support in DataDefinition » Remove ArrayAccess support in DataDefinition
Issue summary: View changes
spokje’s picture

Closed MR now other direction is chosen, updated IS.

andypost’s picture

Title: Remove ArrayAccess support in DataDefinition » Deprecate ArrayAccess support in DataDefinition

the real goal

andypost’s picture

Issue summary: View changes

updated IS

andypost’s picture

Issue tags: -Needs change record

Drafted CR to extend with examples and created MR to iterate

andypost’s picture

In MR I started deprecation, looks it also will need API addition

offsetUnset() - has no usage in core but has not replacement even using definition's methods,
because, for example DataDefinition::setConstraints(), require array so not useful to unset key in $this->definition

- offsetGet() is replaceable with toArray(), example fixes in commit

andypost’s picture

btw there's problem with toArray() - it is not defined by any interface(

probably needs separate issue

andypost’s picture

Status: Needs work » Needs review

The only tricky place is offsetSet() which has only one usage in \Drupal\Core\Config\TypedConfigManager::buildDataDefinition() which could be changed

OTOH createFromDataType() has 5 overrides and could change signature to pass initial values so the raw setter is not needed

wim leers’s picture

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

Seems like this change was definitely planned to be made given the inline comment. Regardless of that it would be great to get a framework manager review on this. 😇

catch’s picture

Couple of comments on the deprecation method itself but I think the overall change is fine. That we only have to update code in the config system and config_translation module suggests this won't affect a lot of contrib modules.

lauriii’s picture

Status: Reviewed & tested by the community » Needs work

Moving to needs work to get the feedback addressed.

andypost’s picture

Status: Needs work » Needs review

Rebased and squashed commits, updated deprecation wording and 10.4 core

maybe it should be deprecated in 11.1?

andypost’s picture

Tests are green and looking at changes there are only properties nullable, translatable, mapping

Also mapping property used to validate requiredKey so replaced with a hack to track changes in array processing, probably need Wim Leers to review

andypost’s picture

Updated to use static::class to get the real data-class name

andypost’s picture

legacy test added

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Reviewing the MR appears all threads have been addressed.

Deprecation appears to have test coverage.

Change record appears clear, also examples are always appreciated.

Believe this is ready.

longwave’s picture

In two minds whether we should even do this given we have multiple workarounds in this MR that look a bit weird, and we've had to add an explicit setter method.

catch’s picture

Status: Reviewed & tested by the community » Needs review

Moving back to needs review, looks like this needs more discussion.

andypost’s picture

I think we should do it anyway but amount of workarounds are debatable.

Glad to get more opinions

smustgrave’s picture

Status: Needs review » Needs work

Think we missed the 11.1 window, mind updating to 11.2, feel free to ping me to re-look at.

michaellander’s picture

Do we need to add toArray() to the \Drupal\Core\TypedData\DataDefinitionInterface()? I'm needing either toArray() or offsetGet() but the former isn't under contract.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.