The "custom_condition_plugin" plugin doesn't exist

Created on 16 June 2023, over 1 year ago

Problem/Motivation

I'm trying to implement a custom condition plugin in a custom module, but the condition plugin manager doesn't seem to be able to find it.

Steps to reproduce

Create a class that extends SimpleComparisonBase, place it in custom_module/src/Plugin/migrate_conditions/condition, and implement MigrateConditionsConditionPlugin annotation.

Is there anything else I need to do to create a custom condition plugin?

πŸ’¬ Support request
Status

Fixed

Version

2.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States jamesgrobertson

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • 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 like in_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 that page_category is an array and you need to use something like page_category/value or page_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 when in_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
  • πŸ‡ΊπŸ‡ΈUnited States danflanagan8 St. Louis, US

    I'm happy you got something working.

    It sounds like page_category is an array. The in_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 if page_condition is an array and the value array is an array of scalars, then in_array will indeed always be false. I see where array_intersect would have come in handy here.

    Something like this would probably have worked, using page_category/0 instead of page_category as the source. Or if 0 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.

Production build 0.71.5 2024