1

I have defined new custom entity through hook_entity_info:

function mymodule_entity_info(){
  return array(
    'mymodule_account' => array(
      'label' => 'MyModule Account',
      'entity class' => 'MymoduleAccount',
      'controller class' => 'EntityAPIController',
      'views controller class' => 'EntityDefaultViewsController',
      'base table' => 'mymodule_accounts',
      'entity keys' => array(
        'id' => 'aid',
      ),
      'fieldable' => TRUE,
      'load hook' => 'mymodule_account_load',
      'view modes' => array(
        'full' => array('label' => 'Full', 'custom settings' => FALSE),
        'teaser' => array('label' => 'Teaser', 'custom settings' => FALSE),
      ),
    ),

  );
}

I have extended the Entity class, overriding the constructor:

class MymoduleAccount extends Entity{

  public function __construct(array $values = array(), $entityType = 
  'mymodule_account'){

    global $user;
    if(empty($values)){
      $values['created'] = REQUEST_TIME;
      $values['title'] = '';
      $values['balance'] = 0.00;
      $values['uid'] = $user->uid;
    }

    $this->entityType = $entityType;
    $this->setUp();
    // Set initial values.
    foreach ($values as $key => $value) {
      $this->$key = $value;
    }
  }
}

I have defined a reaction-rule as follows:

On events segment: I selected

After saving a new user account

On actions segment: I defined two elements:

Create a new entity
Parameter: Entity type: Mymodule Account
Provides variables: Created entity (entity_created)

And:

Save entity
Parameter: Entity: [entity-created], Force saving immediately: true

I have tested these codes by creating new user account, but no mymodule_account has been created in it's database table. What am I missing?

1 Answer 1

0

Your issue (about "no account has been created in DB table") might possibly be because of the event (= "after saving new user account") you are using, which might simply not be the right event.

Moreover, have a look at issue # 430274, about an event which is a variation of your event here, and somehow explains that "after saving something" actually happens ... "before saving something" (which is not a bug, simply how the Rules module works ...).

A possible compromise to avoid these kind of issues, is to transform what's you're trying to do into a Rules Component, so that your original Rule "schedules the execution" (using the Rules Scheduler submodule) of that Rules Component (eg X secs or mins "after saving that new user account" ... if your cron job runs frequent enough, if not it'll be next time cron runs). This compromise will ensure that at the time the Rules Component is executed, the content is for sure saved ... (so that can't be the reason anymore then why it would still be empty ...).

Not sure if this is "the" reason for your issue, but I'd definitely give it a try to see if it helps (debugging is the art of taking away all possible causes for an issue ...).

1
  • Thanks for your response. I tested the scenario, replacing custom entity with a simple message in response to new user saving. It worked. The rules sequences completed as expected. So the problem must be with my custom entity. It's setup/confg must not be incomplete or faulty, thus failing to create & save. Can you help me with that?
    – okey_on
    Jul 6, 2017 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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