Problem/Motivation

When using Fetch entity variable by id with a Taxonomy Entity, outputs this warning and the Entity Variable does not get set:

Notice: Undefined offset: 0 na Drupal\business_rules\Plugin\BusinessRulesAction\FetchEntityVariableAction->fetchEntityVariable() (line 193 /web/modules/contrib/business_rules/src/Plugin/BusinessRulesAction/FetchEntityVariableAction.php)

Steps to reproduce

Add an Empty Entity variable in Variables
Choose Taxonomy Term as Target Entity Type
Choose the desired vocabulary in Target Bundle
Save

Add a Fetch entity variable by id in Actions
Choose Taxonomy Term as Target Entity Type
Choose the desired vocabulary in Target Bundle
Choose tid as Entity id field
Choose the previously created variable as Empty variable
Insert the term ID on the Value text box
Save

Build a Rule to use the previous action, and the error occurs

Proposed resolution

From what I've concluded, the problem is that the [parent] field is a entity_reference field that has a cardinality of -1 because it can take unlimited values.

But this parent field has a 0 value when the term parent is root, unless the term is in a hierarchy and it has a real parent.

And because of that the fetchEntityVariable() on line 193 tries to load a reference that does not exist.

To solve this, I have checked for the value of the entity_reference, and if it is zero, don't enter the entity_reference loading area:

                    foreach ($arr as $key => $item) {
                      $arr[$key] = $item['target_id'];
                      if ((substr($variable->getId(), strlen($variable->getId()) - strlen('->label'), strlen('->label')) != '->label') && ($item['target_id'])) {

but I don't know if this is a good idea, so for the moment won't post a patch before reading some comments on this.

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

jrochate created an issue. See original summary.

jrochate’s picture

Issue summary: View changes