- Issue created by @jamesgrobertson
- πΊπΈUnited States danflanagan8 St. Louis, US
Hi!
Is the custom module enabled?
Could you paste the contents of the custom plugin on this issue? The whole thing including namespace, but feel free to redact anything that may be confidential in nature. :)
- πΊπΈUnited States jamesgrobertson
Yes, it's enabled :D It's a fair question, because I have done that before.
namespace Drupal\custom_module\Plugin\migrate_conditions\condition; use Drupal\migrate_conditions\Plugin\SimpleComparisonBase; /** * @MigrateConditionsConditionPlugin( * id = "array_intersect" * ) */ class ArrayIntersect extends SimpleComparisonBase { /** * {@inheritdoc} */ protected function compare($source, $value) { // TODO: Implement compare() method. } }
Thank you!
- πΊπΈUnited States danflanagan8 St. Louis, US
That code looks good to me.
Is the file named
ArrayIntersect.php
?Would you be able to share the yml that results in the "plugin does not exist" message? I'm wondering if that's where the problem is.
Thanks!
- πΊπΈUnited States jamesgrobertson
Yes, the file is named
ArrayIntersect.php
This is the relevant part:
type: plugin: switch_on_condition source: page_category cases: - condition: plugin: array_intersect value: - dining - services - shopping default_value: stores - condition: default default_value: basic_page
- πΊπΈUnited States danflanagan8 St. Louis, US
Hm...I pasted that into Migrate Sandbox and copy/pasted your plugin code into a custom module and I'm not having any problems. That's annoying!
Did you clear cache?
I don't know that I have any good ideas. I might try to use the custom condition in a simpler process. Like just with evaluate_condition, as simpler test where syntax is easier.
Or do you even need this custom plugin? Is it any different from in_array β ? Regardless though I'm extremely curious why this isn't working for you and would like to see this resolved!
- πΊπΈUnited States jamesgrobertson
Thanks for your help! I will try that and see what happens.
I tried the
in_array
plugin, but basically I need to change the bundle of a node based on whether the post contains the term "dining" OR "services" OR "shopping". Perhaps I'm also doing something wrong with that one, but it seems likein_array
is an "AND" situation (i.e. dining AND services AND shopping)?I can always switch to using the
or
plugin too. I was just trying to make it a little easier to read and perhaps contribute it back, in case someone else needed something similar. - πΊπΈUnited States danflanagan8 St. Louis, US
in_array
works like OR, so that should work for you. If it wasn't doing what you expected it may be thatpage_category
is an array and you need to use something likepage_category/value
orpage_category/0
as your source. (Obviously depending on the structure of the array if it's even an array at all!)I was just trying to make it a little easier to read and perhaps contribute it back
Contributions would be very much welcome! One overlooked way to contribute back is to update module documentation if you find something in the docs you'd like to edit. You can even make an issue to track it and we can get you issue credit and all that jazz.
- πΊπΈUnited States jamesgrobertson
This is the only thing that ended up working for me:
type: plugin: if_condition source: page_category condition: plugin: or conditions: - plugin: has_element condition: equals(dining) - plugin: has_element condition: equals(services) - plugin: has_element condition: equals(shopping) do_get: constants/stores else_get: constants/basic_page
I never could get the
in_array
plugin to work. It seems backwards to me, like$source
and$value
should be switched as parameters whenin_array()
is actually called in the plugin. It always evaluated to FALSE, no matter what I did. I was passing an array as both$needle
and$haystack
, and it might just be PHP that is behaving differently than I expect.Anyway, thanks for all your help. I have something that works, so you can close this if you wish!
- Status changed to Fixed
over 1 year ago 7:18pm 16 June 2023 - πΊπΈUnited States danflanagan8 St. Louis, US
I'm happy you got something working.
It sounds like
page_category
is an array. Thein_array
condition checks if the entire source value is found as any element of the value array. (All the process plugins in migration_condition have handle_multiples set to true). So ifpage_condition
is an array and the value array is an array of scalars, thenin_array
will indeed always be false. I see wherearray_intersect
would have come in handy here.Something like this would probably have worked, using
page_category/0
instead ofpage_category
as the source. Or if0
is not the right array key, some other array key.type: plugin: switch_on_condition source: page_category/0 cases: - condition: plugin: in_array value: - dining - services - shopping default_value: stores - condition: default default_value: basic_page
Automatically closed - issue fixed for 2 weeks with no activity.