# Create asset upon purchase?

**URL:** <https://farmos.discourse.group/t/create-asset-upon-purchase/207>\
**Category:** Using farmOS\
**Created:** [January 27, 2020, 7:25am UTC](https://farmos.discourse.group/t/create-asset-upon-purchase/207 "2020-01-27T07:25:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scissors](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/scissors/32/68_2.png) [@scissors](https://farmos.discourse.group/u/scissors)\
**Post date:** [January 27, 2020, 7:25am UTC](https://farmos.discourse.group/t/create-asset-upon-purchase/207/1 "2020-01-27T07:25:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mstenta](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/mstenta/32/4_2.png) [@mstenta](https://farmos.discourse.group/u/mstenta)\
**Post date:** [January 27, 2020, 3:25pm UTC](https://farmos.discourse.group/t/create-asset-upon-purchase/207/2 "2020-01-27T15:25:25Z")

</div>

There is a module for Drupal called Rules which may be able to help with this: [https://drupal.org/project/rules](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](https://farmos.discourse.group/t/schedule-a-log-based-on-a-quantity-value/115/4)

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](https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_entity_insert/7.x)

Something like:

```php
/**
 * 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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![scissors](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/scissors/32/68_2.png) [@scissors](https://farmos.discourse.group/u/scissors)\
**Post date:** [March 16, 2020, 2:40am UTC](https://farmos.discourse.group/t/create-asset-upon-purchase/207/3 "2020-03-16T02:40:56Z")

</div>

I will try this in the next few days.

---

<div class="post-metadata">

**Author:** ![scissors](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/scissors/32/68_2.png) [@scissors](https://farmos.discourse.group/u/scissors)\
**Post date:** [March 28, 2020, 7:19am UTC](https://farmos.discourse.group/t/create-asset-upon-purchase/207/4 "2020-03-28T07:19:30Z")

</div>

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:

> **[Creating custom modules](https://www.drupal.org/docs/7/creating-custom-modules/)**
>
> This guide includes tutorials and other information that you need to create modules for Drupal 7. Other helpful resources: API reference: https://api.drupal.org/api/drupal/7 Coder module: https://www.drupal.org/project/coder The successor to this...

They recommend the following:

> **[Show all errors while developing](https://www.drupal.org/docs/7/creating-custom-modules/show-all-errors-while-developing)**
>
> Set Drupal to show all errors when developing your module. Some errors are only reported when all PHP error reporting is switched on. Without the error reporting on, you get the dreaded White Screen of Death. Check for errors behind the scenes As an...

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!

---

<div class="post-metadata">

**Author:** ![scissors](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/scissors/32/68_2.png) [@scissors](https://farmos.discourse.group/u/scissors)\
**Post date:** [March 28, 2020, 7:22am UTC](https://farmos.discourse.group/t/create-asset-upon-purchase/207/5 "2020-03-28T07:22:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mstenta](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/mstenta/32/4_2.png) [@mstenta](https://farmos.discourse.group/u/mstenta)\
**Post date:** [March 28, 2020, 12:23pm UTC](https://farmos.discourse.group/t/create-asset-upon-purchase/207/6 "2020-03-28T12:23:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mstenta](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/mstenta/32/4_2.png) [@mstenta](https://farmos.discourse.group/u/mstenta)\
**Post date:** [March 28, 2020, 12:30pm UTC](https://farmos.discourse.group/t/create-asset-upon-purchase/207/7 "2020-03-28T12:30:22Z")

</div>

Another recommendation: install the Devel module ([https://drupal.org/project/devel](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.
