Usage with Entity_lookup

Created on 5 June 2023, over 1 year ago
Updated 15 June 2023, over 1 year ago

Problem/Motivation

Can we use skip_on_condition with entity lookup plugin?

In my source (CSV migration) it is ok to have a entity reference field "PCT Match" be null if source field "Company" is X. The "PCT Match" field is a text field that I need to run the entity_lookup process on to get the entity id to put into the entity reference destination field.

However if "Company" field is not X AND the entity_lookup fails for the "PCT Match" field then I want to skip the source. Here is the config that I have tried (which doesn't work)

field_pct_match:
    plugin: skip_on_condition
    source: Company
    method: row
    message: 'Non-X products must have a valid pct_match %s %s'
    message_context:
      - 'PCT Match'
      - 'Item Number'
    condition:
      plugin: and
      conditions:
        -
          plugin: equals
          negate: true
          value: "X"
        -
          plugin: entity_exists
          entity_type: node
          negate: true
          bundle: pct_ceilingproduct
          source: 'PCT Match'
          value_key: title

This skips all rows where company does not equal X because the value of the entity_exists plugin doesn't do a lookup but just a id load, and thus the lookup fails.

When I tried to use the results of entity_lookup with the condition "and" it errors out with

In DiscoveryTrait.php line 53:

  The "entity_lookup" plugin does not exist. Valid plugin IDs for Drupal\migrate_conditions\ConditionPluginManager are: contains, and, has_element, empty, is_null, all_elements, less_than, entity_exists, callback, in_migrate_map, matches, isset, greater_than, default, ol
  der_than, or, is_stub, equals, in_array


Is there a way to use the results of the Entity Lookup with the migrate_conditions?

πŸ’¬ Support request
Status

Needs review

Version

2.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States dshumaker

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

Comments & Activities

  • Issue created by @dshumaker
  • πŸ‡ΊπŸ‡ΈUnited States dshumaker
  • πŸ‡ΊπŸ‡ΈUnited States dshumaker
  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States danflanagan8 St. Louis, US

    Hi @dshumaker!

    This is an interesting case! I'm going to think "out load" here so it's clear to you whether I'm understanding the problem correctly.

    Let's forget about the possibility of an X just to start simple. We would have this:

    field_pct_match:
      plugin: entity_lookup
      entity_type: node
      negate: true
      bundle: pct_ceilingproduct
      source: 'PCT Match'
      value_key: title
    

    Is that right?

    But we want to skip the row if that lookup is null unless 'Company' is 'X'. So let's add that logic as a second operation in the pipeline.

    field_pct_match:
      -
        plugin: entity_lookup
        entity_type: node
        negate: true
        bundle: pct_ceilingproduct
        source: 'PCT Match'
        value_key: title
      -
        plugin: skip_on_condition
        method: row
        message: 'Non-X products must have a valid pct_match %s %s'
        message_context:
          - 'PCT Match'
          - 'Item Number'
        condition:
           plugin: and
           conditions:
             -
               plugin: is_null // This condition is using the same source and the skip_on_condition plugin, which is the entity_lookup value in the process pipeline
             -
               plugin: not:equals
               value: 'X'
               source: Company // This condition is using a different source that we specify
    

    That's untested, but I think it should work, assuming I understand the problem correctly.

    Hopefully that helps. I always like seeing the interesting ways people use this module. It gives me ideas for ways to make it better. I especially like seeing your use of message_context, which I was really excited to add but haven't had a chance to use in the real world.

    Cheers!

  • πŸ‡ΊπŸ‡ΈUnited States dshumaker

    Hi @danflanagan8 , I was pressed for time and so extended the php class to get it working. When I have a little more time I can work with you to try out some more tests.

    But here are the gist of the changes I made to the EntityLookup process plugin to satisfy my requirements:

       /**
        * {@inheritdoc}
        */
       public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
         // If the source data is an empty array, return the same.
    -    if (gettype($value) === 'array' && count($value) === 0) {
    +    if (gettype($value) === 'array' && count($value) === 0 || ($row->get('Company') == "XX")) {
           return [];
         }
    

    and

    @@ -256,7 +86,7 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
         $results = $query->execute();
    
         if (empty($results)) {
    -      return NULL;
    +      throw new MigrateException('PCT Match field is null or invalid and is required.');
         }
    
    

    hopefully helpful! :)

Production build 0.71.5 2024