Remove Deprecated Function drupal_merge_attached and its references

Function definition as in core/includes/common.inc

/**
 * Merges two #attached arrays.
 *
 * The values under the 'drupalSettings' key are merged in a special way, to
 * match the behavior of:
 *
 * @code
 *   jQuery.extend(true, {}, $settings_items[0], $settings_items[1], ...)
 * @endcode
 *
 * This means integer indices are preserved just like string indices are,
 * rather than re-indexed as is common in PHP array merging.
 *
 * Example:
 * @code
 * function module1_page_attachments(&$page) {
 *   $page['a']['#attached']['drupalSettings']['foo'] = ['a', 'b', 'c'];
 * }
 * function module2_page_attachments(&$page) {
 *   $page['#attached']['drupalSettings']['foo'] = ['d'];
 * }
 * // When the page is rendered after the above code, and the browser runs the
 * // resulting <SCRIPT> tags, the value of drupalSettings.foo is
 * // ['d', 'b', 'c'], not ['a', 'b', 'c', 'd'].
 * @endcode
 *
 * By following jQuery.extend() merge logic rather than common PHP array merge
 * logic, the following are ensured:
 * - Attaching JavaScript settings is idempotent: attaching the same settings
 *   twice does not change the output sent to the browser.
 * - If pieces of the page are rendered in separate PHP requests and the
 *   returned settings are merged by JavaScript, the resulting settings are the
 *   same as if rendered in one PHP request and merged by PHP.
 *
 * @param array $a
 *   An #attached array.
 * @param array $b
 *   Another #attached array.
 *
 * @return array
 *   The merged #attached array.
 *
 * @deprecated To be removed in Drupal 8.0.x. Use
 *   \Drupal\Core\Render\Renderer::mergeAttachments() instead.
 */
function drupal_merge_attached(array $a, array $b) {
  return Renderer::mergeAttachments($a, $b);
}

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Task because it is removing deprecated code.
Issue priority Normal because nothing is really broken.
Prioritized changes Prioritized because it finalizes deprecating code marked for removal before 8.0.0 release.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

arpitr’s picture

Assigned: arpitr » Unassigned
Status: Active » Needs review
FileSize
4.51 KB

Pending:

References in -
1. Core/Render/BubbleableMetadata.php
* @todo Add unit test for this in
* \Drupal\Tests\Core\Render\BubbleableMetadataTest when
* drupal_merge_attached() no longer is a procedural function and remove
* the '@codeCoverageIgnore' annotation.
2. core/modules/system/src/Tests/Common/MergeAttachments.php
Test class MergeAttachmentsTest

arpitr’s picture

Issue summary: View changes

Status: Needs review » Needs work
lhangea’s picture

Let's see if this passes the tests

Status: Needs review » Needs work
lhangea’s picture

There is still this comment in:
/core/lib/Drupal/Core/Render/BubbleableMetadata.php

Add unit test for this in \Drupal\Tests\Core\Render\BubbleableMetadataTest when drupal_merge_attached() no longer is a procedural function and remove the '@codeCoverageIgnore' annotation.

Status: Needs review » Needs work

lhangea’s picture

Version: 8.0.0-beta7 » 8.0.x-dev

lhangea’s picture

Added unit test for the merge() method

Mile23’s picture

Status: Needs review » Needs work

Patch in #12 still applies, but unit tests fail here:

There was 1 error:

1) Drupal\Tests\Core\Render\BubbleableMetadataTest::testMerge with data set #0 (Drupal\Core\Render\BubbleableMetadata Object (...), Drupal\Core\Render\BubbleableMetadata Object (...), Drupal\Core\Render\BubbleableMetadata Object (...))
Drupal\Core\DependencyInjection\ContainerNotInitializedException: \Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.

/Users/paulmitchum/projects/drupal8/core/lib/Drupal.php:129
/Users/paulmitchum/projects/drupal8/core/lib/Drupal.php:157
/Users/paulmitchum/projects/drupal8/core/lib/Drupal/Core/Cache/Cache.php:40
/Users/paulmitchum/projects/drupal8/core/lib/Drupal/Core/Render/BubbleableMetadata.php:65
/Users/paulmitchum/projects/drupal8/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php:139

It looks like Cache's static methods depend on \Drupal. Sigh.

Other than the failing unit test, it looks good. Fix that and we're RTBC.

lhangea’s picture

Well, in the Cache class we have

\Drupal::service('cache_contexts')->validateTokens($cache_contexts);
\Drupal::service('cache_tags.invalidator')->invalidateTags($tags);
$container = \Drupal::getContainer();
    foreach ($container->getParameter('cache_bins') as $service_id => $bin) {
      $bins[$bin] = $container->get($service_id);
    }

that depends on the \Drupal
What we can do for this to pass is to make Cache class implement ContainerInjectionInterface and inject the needed services since the Cache class itself is not a service. But for that we need a separate issue and this one should be postponed until the services are properly injected into the Cache class.

The other static methods used in merge() method don't depend on the \Drupal so, it's ok.

Mile23’s picture

What we can do for this to pass is to make Cache class implement ContainerInjectionInterface and inject the needed services since the Cache class itself is not a service.

Ideally, yah, we'd refactor Cache, but that's clearly out of scope of this issue. In fact, we might leave the @todo about this test, because it's a bit out of scope anyway.

If we want to move forward with the test, though, we'd mock the cache services, place them in the container, and then assert. I'd say this is the way to go. :-)

UnitTestCase::getContainerWithCacheTagsInvalidator() can show the way. It hands you a mocked container, to which you can add your own mocked service objects with expectations.

Mile23’s picture

Issue tags: +Needs reroll
git apply drupal-remove-deprecated-function-drupal_merge_attached-2452475-12.patch 
error: patch failed: core/includes/common.inc:613
error: core/includes/common.inc: patch does not apply
error: patch failed: core/includes/theme.inc:24
error: core/includes/theme.inc: patch does not apply
error: patch failed: core/lib/Drupal/Core/Render/BubbleableMetadata.php:59
error: core/lib/Drupal/Core/Render/BubbleableMetadata.php: patch does not apply
error: core/modules/system/src/Tests/Common/MergeAttachmentsTest.php: No such file or directory
error: patch failed: core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php:131
error: core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php: patch does not apply
neetu morwani’s picture

Assigned: Unassigned » neetu morwani
neetu morwani’s picture

neetu morwani’s picture

Assigned: neetu morwani » Unassigned

Status: Needs review » Needs work
googletorp’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
5.43 KB

Rerolled patch from #12

Status: Needs review » Needs work

The last submitted patch, 21: remove_deprecated-2452475-21.patch, failed testing.

Mile23’s picture

#2407195: Move attachment processing to services and per-type response subclasses adds a unit test for BubbleableMetadata::merge(), which leads to a reroll conflict with the patch in #21.

Let's remove the unit tests for BubbleableMetadata from the patch, which are broken and a bit out of scope here anyway.

googletorp’s picture

Status: Needs work » Needs review
FileSize
3.47 KB

Status: Needs review » Needs work

The last submitted patch, 24: remove_deprecated-2452475-24.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 24: remove_deprecated-2452475-24.patch, failed testing.

Mile23’s picture

+++ b/core/includes/common.inc
@@ -530,55 +530,6 @@ function drupal_js_defaults($data = NULL) {
- * @deprecated To be removed in Drupal 8.0.x. Use
- *   \Drupal\Core\Render\BubbleableMetadata::mergeAttachments() instead.

@@ -1044,7 +995,7 @@ function drupal_pre_render_links($element) {
-      $element['#attached'] = drupal_merge_attached($element['#attached'], $child['#attached']);
+      $element['#attached'] = Renderer::mergeAttachments($element['#attached'], $child['#attached']);

We want to use BubbleableMetadata::mergeAttachments() like it says in the deprecation message.

Mile23’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs change record, +Needs beta evaluation

Nice, thanks.

Setting to RTBC. If the tests fail, it'll be NW, but I doubt it.

Still needs a change record and a beta evaluation.

Mile23’s picture

Issue summary: View changes
Issue tags: -Needs beta evaluation
Mile23’s picture

Issue tags: -Needs change record

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 29: 2452475-29-remove-drupal_merge_attached.patch, failed testing.

JeroenT’s picture

Status: Needs work » Needs review

Random test failures?

ianthomas_uk’s picture

Looks like the bot was out of disk space to me

Status: Needs review » Needs work

The last submitted patch, 29: 2452475-29-remove-drupal_merge_attached.patch, failed testing.

Status: Needs work » Needs review
googletorp’s picture

Status: Needs review » Reviewed & tested by the community

Seems like something weird happened with test bot. Since tests are passing I'm moving issue back to RTBC.

Mile23’s picture

+1 RTBC. Patch applies and the testbot seems happy, plus there aren't any calls to drupal_merge_attached() remaining.

Mile23’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 2ca39a4 and pushed to 8.0.x. Thanks!

  • alexpott committed 2ca39a4 on 8.0.x
    Issue #2452475 by lhangea, googletorp, arpitr, neetu morwani,...

Status: Fixed » Closed (fixed)

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