1

I'm using the Rules module to update my user email signatures.

The rule get's triggered whenever an account is updated and performs a simple custom action.

This is the action, all it does is save a new value to the signature field (textfield, full_html).

<?php

namespace Drupal\frontview_general\Plugin\RulesAction;

use Drupal\rules\Core\RulesActionBase;
use Drupal\user\Entity\User;

/**
 * Change event date status to ( selected, open, closed )
 *
 * @RulesAction(
 *   id = "rules_update_signature",
 *   label = @Translation("Update User Signature"),
 *   category = @Translation("Custom"),
 *   context_definitions = {
 *     "uid" = @ContextDefinition("integer",
 *       label = @Translation("User id"),
 *       description = @Translation("Specifies the id of the user that needs to be updated.")
 *     )
 *   }
 * )
 */
class UpdateSignature extends RulesActionBase {

  /**
   * {@inheritdoc}
   */
  public function doExecute($uid) {

    $user = User::load($uid);

    $first_name = $user->get("field_first_name")->value;
    $last_name = $user->get("field_last_name")->value;

    $name = $first_name . " " . $last_name;

    $user->set("field_signature", $name);
    $user->field_signature->format = "full_html";

    $user->save();

  }
}

This all works fine until it hits the $user->save(); part. That's where something goes wrong and I end up with this error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /Users/lennertslabbinck/Sites/Frontview/web/core/lib/Drupal/Core/Cache/DatabaseBackend.php on line 167

This works great in other actions with other content so it probably has something to do with this being users but I can't figure out what.

1
  • Try this on the ->set line instead let me know if it works or not: $user->set("field_signature", ['value' => $name, 'format' => 'full_html']); (and remove the $user->field_signature->format = "full_html"; line below)
    – Beebee
    Sep 29 '20 at 10:35
1

Your problem is loop update user infinity. When user update, hook_entity_update will be call:

/**
 * Implements hook_entity_update().
 */
function rules_entity_update(EntityInterface $entity) {
  // Only handle content entities and ignore config entities.
  if ($entity instanceof ContentEntityInterface) {
    $entity_type_id = $entity->getEntityTypeId();
    $event = new EntityEvent($entity, [
      $entity_type_id => $entity,
      $entity_type_id . '_unchanged' => $entity->original,
    ]);
    $event_dispatcher = \Drupal::service('event_dispatcher');
    $event_dispatcher->dispatch("rules_entity_update:$entity_type_id", $event);
  }
}

soure

This event will be call your rule action. So when you call $user->save(), the hook_entity_update will call again... That's your problem. To be resolve, you can follow example.

  • Edit your annotation. "user" = @ContextDefinition("entity:user",
  • Update logic method doExecute
  • Update your rule.

Remember don't call $user->save() again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.