Problem/Motivation

I found that @RenderElement("ajax") (Drupal\Core\Render\Element\Ajax) is not used. What's is the purpose? Looks like this was the thing in Drupal 7 when we need to return plain JSON response without full page markup. But in Drupal 8 there are other and better ways to do this.

This render element not used by core and any contrib module. Looks like this is dead code.

Proposed resolution

  • If the element is involved somewhere, it is necessary to write a more detailed comment to the element, because now it is not clear its purpose.
  • If it is not used anywhere, and it looks like neither the core nor the contrib modules are using it, it should be removed, as it is a useless piece of code.

Remaining tasks

  • Understand, why we have this element.
  • Decide what to do with it.
  • Do it :)

User interface changes

The interface won't get hurt in any way.

API changes

Looks like 0 API changes.

Data model changes

No data model changes.

Comments

Niklan created an issue. See original summary.

niklan’s picture

Issue summary: View changes
niklan’s picture

Patch for tests.

niklan’s picture

Status: Active » Needs review

Trigger patch

chi’s picture

Seems like a relic from Drupal 7.

alexpott’s picture

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

Nice find!

Unless this is broken code this needs to go through the deprecation process as this is a plugin and potentially could be being used by contrib or custom code. Deprecating would mean overriding the constructor and throwing an @trigger_error() in there. And adding a @deprecated doc tag to the class docs.

niklan’s picture

Status: Needs work » Needs review
StatusFileSize
new1019 bytes

Added deprecation comment and notice in the constructor.

init90’s picture

Thanks! I've found one small moment which need to fix. In Drupal coding standards is recomendation about parametrs order in docblock:

https://www.drupal.org/project/drupal/issues/3067580 section "Drupal API documentation standards for order of documentation sections"

Would be good apply it to the patch.

chi’s picture

Does anyone know how to make use of this element in Drupal 8? If this is a dead code from Drupal 7 there is no need in deprecation process and change record.

Status: Needs review » Needs work
andypost’s picture

1748 failed tests means it used somehow

tim.plunkett’s picture

As #3 shows, removing it passes tests.
The problem is that on a cold cache, all RenderElement plugins are instantiated and their getInfo() method is called.
I'm not sure how best to indicate deprecations of these plugins... Maybe a #deprecated = TRUE flag or something.

wim leers’s picture

Title: What's the purpose of AJAX RenderElement? » Deprecate the AJAX RenderElement
Category: Support request » Task
Issue tags: +@deprecated

Nice find indeed!

tim.plunkett’s picture

For the record, the last usage of this was removed in #1957590: Remove remaining procedural ajax command usages., April 2013. 381 commits before 8.0-alpha1!

wim leers’s picture

😮

Nice git archeology work!

andypost’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record
StatusFileSize
new2.66 KB
new2.4 KB

Added initial CR https://www.drupal.org/node/3068104

Patch introduce "deprecated" annotation for elements plugins as #14 suggested and fixed deprecation message

alexpott’s picture

+++ b/core/lib/Drupal/Core/Render/ElementInfoManager.php
@@ -131,6 +131,17 @@ protected function buildInfo($theme_name) {
+  /**
+   * {@inheritdoc}
+   */
+  protected function findDefinitions() {
+    $definitions = parent::findDefinitions();
+    // Filter out definitions that deprecated.
+    return array_filter($definitions, function ($definition) {
+      return !empty($definition['deprecated']);
+    });
+  }

This doesn't work - we need deprecated definitions to be findable.

alexpott’s picture

Issue tags: +Needs tests
StatusFileSize
new2.27 KB
new2.27 KB

Here's a patch based on a comment from @andypost online.

We need to add tests how this works but it should allow us to build element info and use the current plugin deprecation standard (i.e. @trigger_error in the constructor).

alexpott’s picture

Issue tags: -Needs tests
StatusFileSize
new2.86 KB
new5.12 KB

Here's a test/

alexpott’s picture

StatusFileSize
new1.2 KB
new4.91 KB

Fixed up the deprecation message. We can add an @see if we feel a change record is warranted. @tim.plunkett what would a CR say?

chi’s picture

drupal:8.8.0

Is this a new format for referencing Drupal versions in deprecation messages? It used to be "Drupal 8.8.0".

alexpott’s picture

StatusFileSize
new1.15 KB
new5.1 KB

Oops there is a CR already. Nice. Added it to the deprecations. @Chi yes this is the format.

alexpott’s picture

Hmmm... so I've tested whether or not the Ajax element works. And it doesn't. It is totally busted. I set up a controller to do:

  public function testAjax() {
    $commands[] = new ReplaceCommand('#edit-date-format-suffix','<small id="edit-date-format-suffix">A test</small>');
    return ['#type' => 'ajax', '#commands' => $commands];
  }

And the only JS loaded on the page is

<!--[if lte IE 8]>
<script src="/core/assets/vendor/html5shiv/html5shiv.min.js?v=3.7.3"></script>
<![endif]-->

So I ponder if the CR is even worth it and if removing it isn't actually the right thing.

andypost’s picture

This element does not work, so probably removal is better choice

+++ b/core/lib/Drupal/Core/Render/Element/Ajax.php
@@ -10,9 +10,22 @@
+    @trigger_error('\Drupal\Core\Render\Element\Ajax is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Return an \Drupal\Core\Ajax\AjaxResponse instead. See https://www.drupal.org/node/3068104', E_USER_DEPRECATED);

+++ b/core/modules/system/tests/modules/element_info_test/src/Element/Deprecated.php
@@ -0,0 +1,29 @@
+    @trigger_error(__NAMESPACE__ . '\Deprecated is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. See https://www.example.com', E_USER_DEPRECATED);

Could use __CLASS__

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.

andypost’s picture

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

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.

pooja saraah’s picture

StatusFileSize
new5.1 KB
new1.98 KB

Addressed the comment #24
Attached patch against Drupal 10.1.x

andypost’s picture

Status: Needs review » Needs work

Deprecation messages also needs update to 10.1 and 11.0 from 8 and 9

pooja saraah’s picture

Assigned: Unassigned » pooja saraah
pooja saraah’s picture

StatusFileSize
new5.1 KB
new3.02 KB

Fixed failed commands on #32
Addressed the comment #33
Attached interdiff patch

pooja saraah’s picture

Assigned: pooja saraah » Unassigned
andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new3.78 KB
new5.08 KB

Here's a fix, also it needs extra test case for the deprecated Ajax class

Maybe extra CR needed to announce ability to deprecate elements

+++ b/core/lib/Drupal/Core/Render/ElementInfoManager.php
@@ -106,6 +106,16 @@ protected function buildInfo($theme_name) {
+    $previous_error_handler = set_error_handler(function ($severity, $message, $file, $line, $context) use (&$previous_error_handler) {
...
+        return $previous_error_handler($severity, $message, $file, $line, $context);

since PHP 8.0 $context is removed https://www.php.net/manual/en/function.set-error-handler.php that's why

2) Drupal\KernelTests\Core\Asset\AttachedAssetsTest::testAddExternalFiles
ArgumentCountError: Too few arguments to function Drupal\Core\Render\ElementInfoManager::Drupal\Core\Render\{closure}(), 4 passed and exactly 5 expected
andypost’s picture

and will need postponed issue to remove the element

smustgrave’s picture

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

This issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge require as a guide.

Reviewing #37

Deprecation message appears correct
Applies cleanly locally.
Verified test fails and passes with the deprecation fix.
CR looks good.

+1 for me

Unless you meant in #38 this should be postponed?

  • catch committed d4da802f on 10.1.x
    Issue #3067580 by alexpott, andypost, pooja saraah, Niklan, smustgrave:...
catch’s picture

Status: Reviewed & tested by the community » Fixed
+++ b/core/lib/Drupal/Core/Render/ElementInfoManager.php
@@ -107,6 +107,16 @@ protected function buildInfo($theme_name) {
     $info = [];
+    $previous_error_handler = set_error_handler(function ($severity, $message, $file, $line) use (&$previous_error_handler) {
+      // Ignore deprecations while building element information.
+      if ($severity === E_USER_DEPRECATED) {
+        // Don't execute PHP internal error handler.
+        return TRUE;
+      }
+      if ($previous_error_handler) {
+        return $previous_error_handler($severity, $message, $file, $line);
+      }
+    });

Shame we have to do this but the only way around it would seem to be moving ::getInfo() to attributes or something. Not for here.

Everything looks good to me. I don't think we need an explicit postponed issue to remove this, we can do it as part of the general 'remove deprecated stuff' Drupal 11 issues.

Committed d4da802 and pushed to 10.1.x. Thanks!

alexpott’s picture

We could keep that code and the test for it there - so contrib can deprecate render elements too.

Alternatively we could deprecate not having getInfo() as a static method - or we could even try to move getInfo() into PHP attributes somehow - although things like the date format loading in \Drupal\Core\Datetime\Element\Datetime::getInfo() are likely to be tricky but we could have an alter or something similar for that.

Status: Fixed » Closed (fixed)

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