Problem/Motivation

The documentation for StrReplace.php (the str_replace process plugin) indicates that the search and replace parameters are fields or variables ("foo" and "bar" which correspond to different values in different examples), when they need to be string values defined directly in the process configuration.

Steps to reproduce

In your source configuration, define constants foo: '/.*/' and bar: 'x'. In the process configuration for a field, use

    plugin: str_replace
    regex: true
    source: field_source
    search: constants/foo
    replace: constants/bar

When you import this configuration and run the migration, you will get the error, "[warning] preg_replace(): Delimiter must not be alphanumeric or backslash StrReplace.php:103"

Now remove the two constants and define search: '/.*/' and replace: 'x' directly in the process plugin. Import config and run the migration. The strings will be replaced as expected.

Proposed resolution

Modify the documentation to remove the references to "foo" and "bar" variables, defining the search and replace strings directly in the configuration.

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

BenStallings created an issue. See original summary.

alison’s picture

I agree that the documentation isn't clear. After reading your report, I tried a pseudo field for the "search" parameter, just to see what would happen -- it didn't work out 😆

  my_search_string:
    -
      plugin: default_value
      default_value: '/\?itok\=\S+$/'
  pseudo_filename:
    -
      plugin: str_replace
      regex: true
      source: my_text_field
      search: '@my_search_string'
      replace: ''

When I ran the migration, I got:
[warning] preg_replace(): No ending delimiter '@' found StrReplace.php:103

-------
If nothing else, the documentation would benefit from an explanation before the examples, about what foo and bar are / that "IRL" you need to provide a literal string (string literal?).

-------
P.S. Feature request to add "properties" option to str_replace 😮 (the issue might be outdated -- if that's the case, I'll fix my comment later (unless I forget)):
#3113461: Properties in StrReplace

matroskeen’s picture

Issue tags: +LutskGCW22

matroskeen’s picture

Thanks for the report! The merge request is ready for review.

heddn’s picture

Status: Needs review » Fixed
matroskeen’s picture

Status: Fixed » Needs review

@heddn, did you mean to commit this one? The merge request is still open and looking for review :)

  • Matroskeen committed 9de72b0 on 8.x-5.x
    Issue #3225569: Improve str_replace documentation.
    
matroskeen’s picture

Status: Needs review » Fixed

It was approved and merged by @heddn. I'm changing status to Fixed.
Thanks!

matroskeen’s picture

Crediting @BenStallings for the issue report - IS summary was clear is easy to understand.

Status: Fixed » Closed (fixed)

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