Problem/Motivation

The callback process plugin works with any global callback function: either a global function or a public, static method.

Drupal has a lot of services that are essentially global. It would be convenient to use a method from a service class as a callback.

Proposed resolution

Add a new plugin, extending the callback plugin. Example usage:

process:
  filemime:
    plugin: service
    service: file.mime_type.guesser
    method: guess
    source: filename

Remaining tasks

User interface changes

None

API changes

None

Data model changes

None

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

benjifisher created an issue. See original summary.

benjifisher’s picture

Assigned: benjifisher » Unassigned
Issue summary: View changes
Status: Active » Needs work
Issue tags: +Needs tests

I created a MR to implement the proposed resolution. It is so simple that it feels like cheating.

I am marking this issue NW for some error handling and an automated test.

benjifisher’s picture

If we like this approach, then I think we can close #3055302: Process plugin: file_mimetype as a duplicate. I borrowed the usage example from there.

benjifisher’s picture

Issue summary: View changes
Status: Needs work » Needs review

There are now about 4 times as many lines to validate the configuration as there are to do everything else. And the test class is more than twice as long (by line count) as the plugin class.

It all seems kind of silly until I remind myself how annoyed I get when I make a mistake and the error message is not helpful. ;)

Matroskeen’s picture

Added some feedback, but overall it looks good to me!
I'm wondering if we need to mention somehow the unpack feature - https://www.drupal.org/node/3205079.

benjifisher’s picture

@Matroskeen:

Thanks for the review! I made most of the changes you recommended.

I'm wondering if we need to mention somehow the unpack feature

That is a little tricky, since that option is only available with Drupal 9.2.0 or later. Along with an @see reference to the core callback plugin, I added

All options for the callback plugin can be used, except for 'callable', which will be ignored.

I think we can leave it at that.

Matroskeen’s picture

Thanks! It looks good to me. I pushed a quick change for the namespace in @see section.
Current test coverage looks good to me, so I'm removing "Needs tests" tag.

I would RTBC, but I didn't test it yet 😉

Matroskeen’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

Finally, I had a chance to test it manually - works like a charm!
I did a quick review again and couldn't find anything to complain.

Therefore, this is RTBC for me.

P.S. I was using the example from IS:

process:
  filemime:
    plugin: service
    service: file.mime_type.guesser
    method: guess
    source: filename

If you're trying on Drupal 9.1.0, consider using guessMimeType method. See: https://www.drupal.org/node/3126004

Matroskeen’s picture

Checked again - still looks good.
When we do this, we can deprecate the existing Transliterate plugin. Here is a follow-up issue: #3232488: Deprecate Transliteration process plugin.

heddn made their first commit to this issue’s fork.

heddn’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for your contributions.

heddn’s picture

Status: Fixed » Closed (fixed)

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