Many contrib modules (and core itself) use file_stream_wrapper_uri_normalize() to ensure that a URI is correct. As is, there is no way for a contrib module adding a stream wrapper to add it's own validation or conditions when normalizing a URL. For example, with the S3 SDK, all URIs must be within a bucket, and a URI like s3://image.jpg is invalid. With this alter hook, a module can alter a URI as needed to meet it's own conditions.

A cleaner option might be to add a normalize method to the stream wrapper interface, and call that directly, but an alter hook seems like the least invasive option while preserving API compatibility.

Comments

deviantintegral’s picture

StatusFileSize
new1.38 KB
bleen’s picture

Status: Active » Needs review

Here testbot ... here boy

larowlan’s picture

Status: Needs review » Needs work

We need a test for the new functionality, but other than that - looks good to me.

travelertt’s picture

Applied this patch to site running Drupal 7.37 and my Upload field disappeared.

Using Media and patches from the AmazonS3 project page:
https://www.drupal.org/node/2479483#comment-9872933
https://www.drupal.org/node/2482757#comment-9889991
https://www.drupal.org/files/issues/2479473.5-upload-params.patch

amaisano’s picture

This patch had no effect for me - Drupal still cannot delete s3:// files, even when the entities they are attached to have been deleted. With or without this patch I had no issues uploading/adding s3:// files.

This is the error I am getting (upon trying to delete):

User warning: A URI must be set before calling getOptions(). in Aws\S3\StreamWrapper->triggerError() (line 785 of /Users/Adamo/GIT/nexamp/public_html/sites/all/libraries/composer/aws/aws-sdk-php/src/Aws/S3/StreamWrapper.php).

cilefen’s picture

@amaisano Did you implement hook_file_stream_wrapper_uri_normalize_alter() with this patch applied?

amaisano’s picture

All I did was apply the patch - no other steps...

I did however just switch to the latest dev version of amazons3 7.x-2.x-dev and now files are getting deleted as expected when the entity they are attached to is deleted. I'm no longer getting these errors on the latest dev, so something from one of the last 8 commits fixed it for me.

cilefen’s picture

@amaisano For this particular patch to work, you have to make a module with an implementation of hook_file_stream_wrapper_uri_normalize_alter(). That is why I asked.

amaisano’s picture

Ah, I see. However, I checked both the stable and dev releases of amazon s3 and both did have such a hook:

/**
 * Implements hook_file_stream_wrapper_uri_normalize_alter().
 */
function amazons3_file_stream_wrapper_uri_normalize_alter(&$uri, $scheme, $target) {
cilefen’s picture

I am confused—there is no such hook in core. This issue would add one.

+++ b/modules/system/system.api.php
@@ -3071,6 +3071,22 @@ function hook_file_url_alter(&$uri) {
+function hook_file_stream_wrapper_uri_normalize_alter(&$uri, $scheme, $target) {
+
+}

It would be good to offer some example code here.

amaisano’s picture

To clarify, I reverted my core files to vanilla (both files addressed in this patch) and AmazonS3 continues to work fine, even though the amazons3.module has this hook. Neither the 1.1 or 2.x versions of the AmazonS3 module are giving me problems, but perhaps I'm just not noticing them.

bleen’s picture

As is, there is no way for a contrib module adding a stream wrapper to add it's own validation or conditions when normalizing a URL. For example, with the S3 SDK, all URIs must be within a bucket, and a URI like s3://image.jpg is invalid. With this alter hook, a module can alter a URI as needed to meet it's own conditions.

So, if you are testing with a valid URI (ex. s3://my-bucket/image.jpg) then you will not see any problems at all, but without the patch in #1 there is nothing to validateenforce that your URI is acceptable to S3. Make sense?

deviantintegral’s picture

Status: Needs work » Needs review

I'm pretty sure the issues all reported above are related to contrib modules or patches to contrib, and not this specific alter hook, so I'm bumping this back to needs review.

jwilson3’s picture

Status: Needs review » Needs work

Comment #10 is asking for some example code inside the api hook definition. Sounds like a good idea to me. I couldnt find one hook in system.api.php that didnt have at least one line of example code.

Also the docblock for the new hook is very light, compared to all of the other examples in system.api.php

Finally, shouldn't this be added to Drupal 8 then backported first?

cilefen’s picture

Finally, shouldn't this be added to Drupal 8 then backported first?

Yes, all feature requests should be in 8.1.x now.

deviantintegral’s picture

Version: 7.x-dev » 8.1.x-dev
Status: Needs work » Needs review
StatusFileSize
new1.57 KB

Good call - I'd thought this function was removed entirely in D8. Here's a reroll with simplified docs based on the hook implementation in the amazons3 module.

Status: Needs review » Needs work

The last submitted patch, 16: 2479523.16-normalize-alter-hook.patch, failed testing.

deviantintegral’s picture

StatusFileSize
new1.63 KB

Fixes trying to call alter() in tests when the moduler_handler service isn't available.

deviantintegral’s picture

Status: Needs work » Needs review

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.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.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.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.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.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.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.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.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.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.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

pedrosp’s picture

Version: 8.7.x-dev » 7.64

Still applying .1 patch to core 7.64, but honestly not sure if still needed :)
I update this issue to ping other folks in this thread.

andypost’s picture

Version: 7.64 » 8.8.x-dev
Related issues: +#3034072: Move file uri/scheme functions from file.inc and FileSystem to StreamWrapperManager
berdir’s picture

I'm not sure if a hook is the right approach here, maybe this should instead be a method on the stream wrapper that could be implemented?

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.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new153 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

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.

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.