Problem/Motivation

All the usages for new EditorDialogSave($values) is passing in $form_state->getValues(), but the phpdoc for the __construct() method and the $values property says it should be a string. That appears to be incorrect and it should typehint as an array.

  /**
   * An array of values that will be passed back to the editor by the dialog.
   *
   * @var string // But this just said above it would be an array???
   */
  protected $values;
Method
    __construct
Found usages  (2 usages found)
        web\core\modules\editor\src\Form  (2 usages found)
            EditorImageDialog.php  (1 usage found)
                EditorImageDialog  (1 usage found)
                    submitForm  (1 usage found)
                        232 $response->addCommand(new EditorDialogSave($form_state->getValues()));
            EditorLinkDialog.php  (1 usage found)
               EditorLinkDialog  (1 usage found)
                    submitForm  (1 usage found)
                        85 $response->addCommand(new EditorDialogSave($form_state->getValues()));

Steps to reproduce

Proposed resolution

EditorDialogSave should typehint as an array

Remaining tasks

Review

User interface changes

NA

API changes

Add typehint to EditorDialogSave

Data model changes

NA

Release notes snippet

NA

Issue fork drupal-3098201

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

Dave Reid created an issue. See original summary.

dave reid’s picture

Issue summary: View changes
dave reid’s picture

Issue summary: View changes
dave reid’s picture

Issue summary: View changes
dave reid’s picture

Status: Active » Needs review
Issue tags: +Needs backport to D8
StatusFileSize
new816 bytes

Here's the fix for Drupal 9, I think in order to not break backwards compatibility, the Drupal 8 patch should not include the typehint, but I don't know how any code would work without the value not being an array.

dave reid’s picture

Version: 9.1.x-dev » 9.0.x-dev
xjm’s picture

Another "How do we typehint with BC?" issue. We're not able to make the typehint change in 9.0 at this point, and we need to figure out how we can add typehints with the continuous upgrade path.

dave reid’s picture

I would think this is safe to fix no matter what just based on it was never supposed to work with strings.

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.

abhijith s’s picture

Patch #5 cant be applied in 9.2.x.Need reroll.

Checking patch core/modules/editor/src/Ajax/EditorDialogSave.php...
error: while searching for:
  /**
   * An array of values that will be passed back to the editor by the dialog.
   *
   * @var string
   */
  protected $values;

  /**
   * Constructs a EditorDialogSave object.
   *
   * @param string $values
   *   The values that should be passed to the form constructor in Drupal.
   */
  public function __construct($values) {
    $this->values = $values;
  }


error: patch failed: core/modules/editor/src/Ajax/EditorDialogSave.php:15
error: core/modules/editor/src/Ajax/EditorDialogSave.php: patch does not apply
abhijith s’s picture

StatusFileSize
new813 bytes

Rerolled patch #5 for 9.2.x.

guilhermevp’s picture

Status: Needs review » Reviewed & tested by the community

Makes sense to change, I agree with comment #8.

guilhermevp’s picture

StatusFileSize
new816 bytes

Sending D8 backport.

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.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 13: d8_3098201-13.patch, failed testing. View results

catch’s picture

Status: Needs work » Reviewed & tested by the community

Restoring status after HEAD was broken.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Needs backport to D8 +Bug Smash Initiative

In terms of typehinting, https://3v4l.org/h9cYK shows that this doesn't break child implementations.

However, it might break calling code.

http://grep.xnddx.ru/search?text=new+EditorDialogSave&filename= shows there are some places where the return is not an array

http://grep.xnddx.ru/node/31368503 and http://grep.xnddx.ru/node/31370333 are two examples

So I think perhaps we should change the phpdoc only in D9, and then in D10, add a typehint.

Which means we need to trigger_error for a deprecation for anyone calling the constructor where $values is not an array.

Needs work for those changes

larowlan’s picture

Crediting myself for trawling through 3 pages of contrib grep to find those two cases 👩‍💻

guilhermevp’s picture

Status: Needs work » Needs review
StatusFileSize
new732 bytes

Crediting myself for trawling through 3 pages of contrib grep to find those two cases

Totally deserved!

Sending comment only patch.

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.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Seems simple and makes sense after reading the ticket.

catch’s picture

Status: Reviewed & tested by the community » Needs work

#19 looks OK for 9.5 at this point, however per #17 we need a (now 10.1.x) patch to @trigger_error('....', E_USER_DEPRECATED) when the constructor is called without an array, so that we can add the actual type hint in Drupal 11.

Also I haven't sent it for a retest because DrupalCI is having trouble, but the last green run on the patch was from 9.3.x, it's good to check things are green on both 9.5.x and 10.1.x (if they're eligible for both branches) before RTBCing.

smustgrave’s picture

Status: Needs work » Needs review
StatusFileSize
new603 bytes
new968 bytes

You mean something like this?

catch’s picture

+++ b/core/modules/editor/src/Ajax/EditorDialogSave.php
@@ -15,17 +15,20 @@ class EditorDialogSave implements CommandInterface {
   public function __construct($values) {
+    if (!is_array($values)) {
+      @trigger_error('Constructor $values must be of type array. It was added in drupal:10.1.0 and will be required before drupal:11.0.0.', E_USER_DEPRECATED);
+    }
     $this->values = $values;

So yes, but this should something like 'The $values parameter to ' . __METHOD___ . ' must be an array' so people know where the error has come from. Should be existing constructor deprecation examples in 9.5.x using __METHOD__ to copy (usually for missing arguments but this is similar enough).

Ratan Priya’s picture

Assigned: Unassigned » Ratan Priya
Ratan Priya’s picture

Assigned: Ratan Priya » Unassigned
StatusFileSize
new895 bytes

@catch,

I made the changes you required at comments #25

Needs review.

smustgrave’s picture

@Ratan Priya can you please upload an interdiff if you're going to upload a patch please.

Ratan Priya’s picture

StatusFileSize
new705 bytes

@smustgrave,

Added interdiff against #27

Thankyou.

smustgrave’s picture

Status: Needs review » Needs work

Thank you!

Looking at #27 appears it's just missing the E_USER_DEPRECATED variable.

Example copied from core

@trigger_error('Passing a \'throw_exception\' option to ' . __METHOD__ . ' is deprecated in drupal:9.2.0 and is removed in drupal:10.0.0. Always catch exceptions. See https://www.drupal.org/node/3201187', E_USER_DEPRECATED);

@catch do you want this ticket number in the comment?

Ratan Priya’s picture

StatusFileSize
new914 bytes
new724 bytes

@smustgrave,

Made changes as per the example given in #30

needs review.

smustgrave’s picture

Status: Needs work » Needs review

Looks good. Will let the test runner complete before marking RTBC.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Deprecation added per #25 looks good.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 31: 3098201-31.patch, failed testing. View results

ameymudras’s picture

Status: Needs work » Reviewed & tested by the community

- Retested the patch #31 and now it seems to pass.
- The issue summary is clear and describes the problem
- Code is simple and does the type change along with triggering a depreciation error as expected

Marking this issue as RTBC

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/editor/src/Ajax/EditorDialogSave.php
@@ -15,17 +15,20 @@ class EditorDialogSave implements CommandInterface {
+    if (!is_array($values)) {
+      @trigger_error('The $values parameter to ' . __METHOD___ . ' must be an array', E_USER_DEPRECATED);
+    }

This deprecation message should say when we're going to fix this and add a typehint. I think it should be something like:

@trigger_error('Calling \Drupal\editor\Ajax\EditorDialogSave::__construct with a non-array $values parameter is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0.', E_USER_DEPRECATED);

FWIW I'm not convince that this is ever called with a non-array anywhere by anything. Here's the code and docs that is triggered by this:

  /**
   * Command to save the contents of an editor-provided modal.
   *
   * This command does not close the open modal. It should be followed by a
   * call to `Drupal.AjaxCommands.prototype.closeDialog`. Editors that are
   * integrated with dialogs must independently listen for an
   * `editor:dialogsave` event to save the changes into the contents of their
   * interface.
   *
   * @param {Drupal.Ajax} [ajax]
   *   The Drupal.Ajax object.
   * @param {object} response
   *   The server response from the ajax request.
   * @param {Array} response.values
   *   The values that were saved.
   * @param {number} [status]
   *   The status code from the ajax request.
   *
   * @fires event:editor:dialogsave
   */
  Drupal.AjaxCommands.prototype.editorDialogSave = function (
    ajax,
    response,
    status,
  ) {
    $(window).trigger('editor:dialogsave', [response.values]);
  };

So we document that response.values is expected to be an array. There are other places in the JS code base where we expect an array here so I'm not 100% that this check is necessary. And maybe we could just add the typehint...

narendra.rajwar27’s picture

Status: Needs work » Needs review
StatusFileSize
new1.03 KB
new826 bytes

Updating patch as suggested in comment #36

smustgrave’s picture

@alexpott see what you are saying but would this just add an extra layer IF a string is passed. If never triggered maybe we can remove later down the line

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.

shubham chandra’s picture

StatusFileSize
new1.03 KB

Added patch against #37 in Drupal 10.1.x

bnjmnm’s picture

@Shubham Sharma: Rerolls are not automatically needed if a Drupal dev branch version changes. In many cases, the patch still applies. If you click the "Add test / retest" link on the most recent patch of a given issue, you can test the patch on 10.1.x (or whatever the current dev branch is). If the patch applies there, then no reroll is needed.

You added a reroll that was not needed - I noticed you doing the same in at least 7 other recent issues. Credit is not granted for unnecessary rerolls.

smustgrave’s picture

Status: Needs review » Needs work

Reviewing #37 it appears to be adding the typehint already (array $values) {

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.

scott_euser’s picture

Status: Needs work » Reviewed & tested by the community

Agreed, typehint is there, and what I can see in contrib modules like https://git.drupalcode.org/project/ckeditor5_embedded_content/-/blob/1.0... they are also passing arrays. Testing this patch and the contrib module also continues to work fine + PhpStorm stops complaining about the value as an array not matching the string hint. I believe this can be RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/editor/src/Ajax/EditorDialogSave.php
    @@ -15,17 +15,20 @@ class EditorDialogSave implements CommandInterface {
       protected $values;
    

    Let's add a parameter typehint while we're at it.

  2. +++ b/core/modules/editor/src/Ajax/EditorDialogSave.php
    @@ -15,17 +15,20 @@ class EditorDialogSave implements CommandInterface {
    -  public function __construct($values) {
    +  public function __construct(array $values) {
    +    if (!is_array($values)) {
    +      @trigger_error('Calling \Drupal\editor\Ajax\EditorDialogSave::__construct with a non-array $values parameter is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0.', E_USER_DEPRECATED);
    +    }
    

    If we have the typehint then the deprecation is pointless because it will never occur.

    I think in this instance adding the typehint is okay given the earlier comment.

scott_euser’s picture

Status: Needs work » Needs review

Updated as per feedback in #45. Hiding patches in favour of merge request.

smustgrave’s picture

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

Feedback appears to have been addressed.

Updated the issue summary slightly to use standard template.

longwave’s picture

Status: Reviewed & tested by the community » Fixed

Can't quite believe it took us four years to commit pretty much the original patch from #5, but here we are.

Not backported just in case this breaks some strange edge case somewhere, although you are probably doing something wrong if you run into this.

Committed d2d6d0c and pushed to 11.x. Thanks!

  • longwave committed d2d6d0c1 on 11.x
    Issue #3098201 by Ratan Priya, guilhermevp, scott_euser, smustgrave,...

Status: Fixed » Closed (fixed)

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