Hi,

I'm in need of some help. I need to fire a rule on entity after it has saved, both hook_entity_insert and hook_entity_update must be implemented.

On entity insert, I can't get the entity field values, on entity_update I can get the values but I need to fire the rule on entity insert with the field values.

There is a module that implements this event: https://www.drupal.org/project/hook_post_action
Is it possible to add this module as an extent for Rule Events?
- hook_entity_postsave
- hook_entity_postinsert
- hook_entity_postupdate
- hook_entity_postdelete
- hook_node_postsave
- hook_node_postinsert
- hook_node_postupdate
- hook_node_postdelete

I can help with the testing and some of the work if you point me in the right direction.

Thanks,
Kind Regards,
Alex

CommentFileSizeAuthor
#3 potato-stamp.jpg147.49 KBlexsoft00

Comments

lexsoft created an issue. See original summary.

yseki’s picture

Assigned: Unassigned » yseki
Status: Active » Needs work

Hi Alex,

Thanks for the suggestion. Those triggers implementation seems to pretty straight forward and can be developed as an optional module inside business_rules. Based on that, I can't see any reason to not implement it asap. Hopefully I'm gonna do it on my next update.

I would really appreciate your tests on this feature.

Best regards,

Yuri Seki

lexsoft00’s picture

StatusFileSize
new147.49 KB

Hi @yuriseki,

That's great news! I would gladly help with anything I can.

I've done a test now by adding a hook in .module:

/**
 * Gets called after an entity has been inserted to database.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   An entity object.
 *
 * @see hook_entity_postsave()
 * @see hook_entity_postupdate()
 * @see hook_entity_postdelete()
 *
 * @ingroup entity_crud
 */
function business_rules_entity_postinsert(EntityInterface $entity) {
  // Only handle content entities and ignore config entities.
  if ($entity instanceof ContentEntityInterface) {
    $reacts_on_definition = \Drupal::getContainer()
      ->get('plugin.manager.business_rules.reacts_on')
      ->getDefinition('entity_postinsert');

    $entity_type_id = $entity->getEntityTypeId();
    $event          = new BusinessRulesEvent($entity, [
      'entity_type_id' => $entity_type_id,
      'bundle' => $entity->bundle(),
      'entity' => $entity,
      'entity_unchanged' => $entity->original,
      'reacts_on' => $reacts_on_definition,
      'loop_control' => $entity->getEntityTypeId() . $entity->id(),
    ]);
    /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */
    $event_dispatcher = \Drupal::service('event_dispatcher');
    $event_dispatcher->dispatch($reacts_on_definition['eventName'], $event);
  }
}

And adding the class to

<?php

namespace Drupal\business_rules\Plugin\BusinessRulesReactsOn;

use Drupal\business_rules\Plugin\BusinessRulesReactsOnPlugin;

/**
 * Class EntityPostInsert.
 *
 * @package Drupal\business_rules\Plugin\BusinessRulesReactsOn
 *
 * @BusinessRulesReactsOn(
 *   id = "entity_insert",
 *   label = @Translation("Entity Post insert"),
 *   description = @Translation("Reacts after a new entity has been inserted."),
 *   group = @Translation("Entity"),
 *   eventName = "business_rules.entity_postinsert",
 *   hasTargetEntity = TRUE,
 *   hasTargetBundle = TRUE,
 *   priority = 1000,
 * )
 */
class EntityPostInsert extends BusinessRulesReactsOnPlugin {

}

But it seems to have the same result to entity insert no real difference that I could spot. I'm starting to think that this will not solve my problem. I've actually got some results from getting the values on Entity Insert without adding a bundle to the action and using token for values.

I have a rule to programmatically create a node,
when the node is inserted I want to send a confirmation email with the field values of an entity reference of the current node,
which I need to custom query the DB.

Done a custom Action Rule for that but could not query the DB because I was getting NULL on field values on the current entity using bundle on Action.

Sorry for the long post, here's a drupal potato.