Let's add a generic condition that allows checking for multiple entity bundles (and thus asserts related fields).

  • Let's work on top of the info provided by hook_entity_info() only, so we can support all entity types having bundles including terms and comments
  • Allow choosing multiple bundles.
  • The asserted properties/fields will be the intersection of the available properties. We should add a help text explaining that.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mitchell’s picture

Component: Rules Engine » Rules Core

Updated component.

mitchell’s picture

David Stosik’s picture

Status: Active » Needs review
FileSize
4.73 KB

Hello,

May I suggest this patch? This could do what this issue asks for, and works for me, at least.
It adds an "Entity is of type and bundle" condition, which requires that you set a type first, before you can select bundles (the reason is that many bundles in different entity types can share the same name).

Thanks for your review,
David

David Stosik’s picture

Hello again,

I added a simpletest test that verifies that an article node passes the condition successfully.

Thanks,
David

Itangalo’s picture

Just adding a David Stosik++ on this issue.

micnap’s picture

I am getting errors when I click on the "Entity is of type and bundle" option:

Notice: Undefined index: type in rules_entity_action_bundle_options() (line 262 of /home/xxxx/public_html/sites/all/modules/rules/modules/entity.rules.inc)
Notice: Undefined index: bundles in rules_entity_action_bundle_options() (line 264 of /home/xxxx/public_html/sites/all/modules/rules/modules/entity.rules.inc)
Warning: Invalid argument supplied for foreach() in rules_entity_action_bundle_options() (line 264 of /home/xxxx/public_html/sites/all/modules/rules/modules/entity.rules.inc)

However, I am able to continue adding the condition and it seems to work fine.

Thanks,
Mickey

mstrelan’s picture

Status: Needs review » Needs work

Notices in #6 need to be fixed otherwise it seems to work well.

kingandy’s picture

Those notices should all be fixable with an existence check...

function rules_entity_action_bundle_options($element) {
  $bundles = array();
  if (!empty($element->settings['type'])) {
    $entity_info = entity_get_info($element->settings['type']);
    foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
      $bundles[$bundle_name] = $bundle_info['label'];
    }
  }
  return $bundles;
}

Assuming the empty array doesn't cause further problems, of course. Given that @micnap was able to continue with the notices, it seems like we should be good.

kingandy’s picture

Sorry I can't provide an amended patch, I'm a bit rushed at the moment.

Simon Georges’s picture

Status: Needs work » Needs review
FileSize
5.89 KB

Patch as per #8, all credits go to David Stosik and kingandy.

berdyshev’s picture

Status: Needs review » Reviewed & tested by the community

thank you, guys. it works for me perfect.

fago’s picture

Status: Reviewed & tested by the community » Needs work

Thanks. Patch looks good, however I think it should be just checking the bundle as the 90% use case is that. If you want to check the entity type also, we already have the condition for that.

fago’s picture

Assigned: Unassigned » fago

I'm going to take care of this.

David Stosik’s picture

Hi, thanks for your reply!
My concern is that if you only check bundle and not entity type, it seems impossible to make assertions on fields, and thus, afterwards, in actions, impossible to use fields attached to the exact entity_type:bundle we tried to match.
I know that 90% of the cases need only to match the bundle, but matching entity+bundle makes easier to use fields, without having to create a "entity has field" for each field we want to use.
Maybe providing both (bundle only AND entity + bundle) would be a better compromise?

Thanks,
David

kingandy’s picture

Agree with #14, an entity/bundle check that registers the relevant fields would greatly improve usability. Is that outside the scope of this issue, though? The goal here is simply to be able to check the bundle type, creating a conjoined entity/bundle condition seems like it would be a separate feature request.

fago’s picture

I know that 90% of the cases need only to match the bundle, but matching entity+bundle makes easier to use fields, without having to create a "entity has field" for each field we want to use.

That should not be problem in general. However, I figured that we might require the type check in case a general 'entity' argument is passed in. Thus, I'll add the entity-type parameter and try to hide it if we can derive it from the metadata.

kingandy’s picture

Or maybe I'm just being pedantic and we should go ahead and do it if it will improve the module...

I'm just wary of scope creep, and would like to see the bundle condition committed first.

fago’s picture

>Agree with #14, an entity/bundle check that registers the relevant fields would greatly improve usability.

That's the way it works automatically - thanks to the metadata assertions. :-)

fago’s picture

Status: Needs work » Fixed
FileSize
7.9 KB

I've completed it as discussed + pushed it (see attached patch). Thanks!

David Stosik’s picture

Thanks!

phenaproxima’s picture

Hi all - I just updated Rules to 7.x-2.3, which according to the release notes should include this patch (unless I'm misreading them). Once I do that, though, I start getting these warnings on the main Rules admin screen:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'rules_condition_entity_is_of_type_bundle_assertions' not found or invalid function name in FacesExtendable->__call() (line 123 of sites/all/modules/rules/includes/faces.inc).

Looking in entity.rules.inc, the function it's looking for doesn't seem to exist, although it is supplied by the patch. Do I need to apply the patch? If so, does it apply against 7.x-2.3 or the dev version?

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

micnap’s picture

Looks like the patch is included except the entity_is_of_type_bundle condition was changed to entity_is_of_bundle, so the rules need to be updated to use the different condition.

basitparacha’s picture

Issue summary: View changes

Hi all,

I want to get help from you guys regarding my issues related to RULES integration with CUSTOM ENTITY types. I have a Custom Entity type with 4 bundles in custom entity, and i want to use features of Rules Module with my custom entity type like "create new entity" with "After saving a new apricot", or "Delete Entity" when given expiry date crossed to site-date and time.

There are few issues related to query:
1) When i go to 'Add Action' and i chose my custom entity name from a list, it does not show me Bundle Types of my Custom Entity.
2) There is also not showing the my entity type fields like TITLE or other fields of my costom entity type.

It is workable from Rules-Modules? if Yes then what will be critaria, and if it is does not workable from Rules Modiles only, then how it will be achieved from custom code/without adding custom code of my entity type.

Looking for your help

thanks and regards,
A.Basit