The "Entity has field" condition is working for me to detect whether an entity has a field, however it doesn't give me access to that field. The only that I've been able to do to get access to the field in later conditions/actions is to use the "Entity is of bundle" condition, which then gives access to all the fields of the bundle. This can be a major hassle, though, if there are a large number of bundles that you want to have the same rule applied to based on a field.

Unless I'm doing something wrong, I feel like "Entity has field" needs to actually give access to use the field regardless of checking for a bundle.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bmcclure created an issue. See original summary.

bmcclure’s picture

I spent a while going through API documentation and code, and it's not clear to me what the best way to provide access to individual fields without specifying a bundle is. The EntityDataDefinition class doesn't seem to have any mechanism for either adding individual fields to the properties list or manually specifying fields, so it seems this would either have to be a class extending EntityDataDefinition that could be used to override the properties that are returned, or a separate class altogether that might wrap EntityDataDefinition and also add field definitions that should be included based on any other Rules conditions.

It would be great if you could simply use assertMetadata to inject the field, similarly to how the EntityIsOfBundle class works, but again it just doesn't seem like EntityDataDefinition supports that use case.

I'm happy to put some effort into a patch for solving this, but I'm not sure what the best direction to go is.

errev’s picture

+

rferguson’s picture

+ 1 for this, I can't find a way to get it to validate on field values using data comparison, even when having "entity has field"

superlolo95’s picture

+1

StuartL’s picture

+1 - I was hoping to be able to use Token values while this issue is being worked on, but I haven't had any success.

jonathan1055’s picture

Hi, I have been working on #2664280: Select lists in action & condition configuration forms and this now gives a drop-down selection list for the fields to choose. Maybe this will help you as it seems to be working fine for me and does not require the 'Entity is of bundle' condition to be added.

StuartL’s picture

Thanks for your contribution, Jonathan. Unfortunately this didn't work for me in Drupal 8.4.0 - I have yet to try it in Drupal 8.4.2.

jonathan1055’s picture

Hi StuartL
What exactly is not working for you? I will try to help if you can be more specific.
Jonathan

StuartL’s picture

Hi Jonathan,

I'll try and get something more specific for you, but many of the pages are now blank when displayed (e.g. after choosing a condition). Also, I am not able to select fields using the selector when setting up a redirect action, even after checking that a field exist.

Thanks,
Stuart

StuartL’s picture

Hi Jonathan,

A quick update: I updated to Drupal 8.4.2 and the behavior has not changed. Trying to add the condition 'Content type' leads to a blank page. After adding 'Entity has field', I am able to select a field from a dropdown (better than before), but when adding the action 'Page ridirect', the field does not appear when using the data selector method - I start typing 'node.' and there is nothing that resembles the field in the list.

Thanks,
Stuart

jonathan1055’s picture

Trying to add the condition 'Content type'

What event is the rule reacting to? For node-related events I do not see a condition named "Content Type". Did you mean "Entity is of type" or "Node is of type" or something else?

StuartL’s picture

FileSize
53.64 KB

Hi Jonathan,

Please see the attached screenshot for the configuration.

The event is: 'After saving new content'. The conditions are checking that the content type is 'Video', and that the entity has a certain field. That field is an entity reference. The redirect should direct to the referenced node.

After applying your patch, and choosing 'After saving new content' as the event, 'Content type' is an available condition but directs to a blank page.

Thanks,
Stuart

jonathan1055’s picture

FileSize
134.95 KB

Hi Stuart,
This is a bit strange, because I do not get any condition which is labelled simply "content type", as mentioned before. Here's my condition selection (on 8.4, latest Rules dev, for event 'after saving new content').

condition selection list

Neither "Entity is of type" nor "Node is of type" produce the caption "Condition: Content Type" on the saved page.

StuartL’s picture

Was your initial event set to 'After saving new content'?

jonathan1055’s picture

Was your initial event set to 'After saving new content'?

Yes it was. See my last sentence in #14 :-)

Do you also get the options I have shown, or is your list completely different?

StuartL’s picture

My list has some additional options. See attached screenshots.

Thanks,
Stuart

jonathan1055’s picture

Ah, yes, your screenshot does give something that is relevent here. I had noticed before that conditions in the "Other" section are not actually Rules conditions! This is why it does not work for you.

I had been meaning to raise this problem at some point, but now you have prompted me to do it. See #2927132: List of conditions includes Core conditions which do not work with Rules

StuartL’s picture

Thanks - that helps clarify things. Unfortunately, I'm still not able to select the field when creating a redirect (action).

TR’s picture

Janner’s picture

I am attempting to create a rule that will assign a role to a user based upon the status of a custom user field on the registration form and user edit page (in this case a boolean single checkbox).

Unfortunately, I have been unable to find a way of accessing this custom field when attempting to create a data comparison condition.

I have tried to make the field available by following other suggestions of adding conditions “Entity has field” and/or “Entity is of type” (and saved the rule before adding the data comparison condition).

This hasn't worked.

I have attempted to follow the suggestion offered in #11 here Finding custom fields & creating users - which is to add the condition that “Entity is of bundle”.

According to the suggestion, the values for Entity, Type, and Bundle should all be “user”. It's stated that the latter two values can be entered by Direct Input.

However, in my case, where I have patched the latest dev release of Rules to add support for select lists, both of those values use select lists with no option of switching to Direct Input.

For Type, “user” is available from the select list.

However, it is not available in the Bundle select list.

What am I missing or doing wrong? thanks

TR’s picture

If you are having problems with #2664280: Select lists in action & condition configuration forms please discuss them in THAT issue, not here. Nothing in this issue has been on-topic since #2.