Create asset upon purchase?

Is it possible to have drupal automatically create an asset when you create a purchase log? It would be nice to have this as an option. Especially if it could assign a group and create or update a quantity.

Is this already a thing?

2 Likes

There is a module for Drupal called Rules which may be able to help with this: https://drupal.org/project/rules - it allows you to configure trigger/event logic via the UI - no coding required.

See my comment in this related issue for more information: Schedule a log based on a quantity value

I’ve decided NOT to include Rules in farmOS 1.x because it does not have an automated upgrade path to Drupal 8, so it would add a hurdle to our upgrade process. If you self-host farmOS you are welcome to install it and play around - just know that it may need to be redone when we migrate to farmOS 2.x

Alternatively, it would be relatively simple to code this in a custom module using hook_entity_insert(): https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_entity_insert/7.x

Something like:

/**
 * Implements hook_entity_insert().
 */
function mymodule_entity_insert($entity, $type) {

  // If this is a purchase log, create an asset.
  if ($type == 'log' && $entity->type = 'farm_purchase') {
    
    $asset = array(
      'name' => 'My Asset',
      'type' => 'equipment',
    );
    farm_asset_save($asset);
  }
}
2 Likes

I will try this in the next few days.

1 Like

Okay, I have created and installed this module, and now I am running into an unspecified error…so progress!

I currently have:

<?php
/**
 * Implements hook_entity_insert().
 */
function purchaseasset_entity_insert($entity, $type) {

 // If this is a purchase log, create an asset.
 if ($type == 'log' && $entity->type = 'farm_purchase') {

$asset = array(
  'name' => 'My Asset',
  'type' => 'equipment',
);
farm_asset_save($asset);
  }
}

When I go to save the purchase log I get:

The website encountered an unexpected error. Please try again later.

Does drupal have some kind of console I can read to troubleshoot the error?

Thanks a bunch for your help, I am using this guide to learn about modules:

They recommend the following:

Is this what you would recommend to try? Also are there any lists of what kind of info I could pull from the purchase log to add to the new asset? Or how to change the purchase logs to allow this as an option? I will keep RTFMing for a while, so don’t feel obligated to answer every question I am spewing right now. Again, really appreciate all your work already!

1 Like

I am doing the module thing instead of using Rules in case this ends up working and being useful. Then less work to do updating later as you mentioned.

1 Like

Awesome!

Does drupal have some kind of console I can read to troubleshoot the error?

Yes, try going to /admin/reports/dblog and see if the error details are displayed there.

Another recommendation: install the Devel module (https://drupal.org/project/devel).

This will give you a nifty dpm() function that you can use to print variables to the screen for easy browsing. This is easier than setting up a full debugger in an IDE.

So for example, in your code (once you resolve the error), you can run dpm($entity) to view what’s inside the $entity object, which will represent the purchase log.