Computed Field is a very powerful field module that lets you add a custom "computed fields" to your content types. These computed fields are populated with values that you define via PHP code. You may draw on anything available to Drupal, including other fields, the current user, database tables, you name it. (Feeling the power yet? :) ) You can also choose whether to store your computed field values in the database with other content fields, or have them "calculated" on the fly during node views. (Although you should note that Views use requires database stored values.) This field is literally the Swiss Army knife of fields. So start cooking up your PHP based values!
This module is intended to be used by PHP developers to insert & execute PHP code on a Drupal website. It does not provide a macro interface, a code builder or anything other than insert your working code here into this text area. You need to have a good working knowledge of PHP and the Drupal API to use this module. To get the best results with this module, pre-test your PHP code in the Body field of a Page in Drupal.
Note that entering PHP code into the database is not recommended, as it is a security risk if someone is able to bypass access controls on the Web front-end. Additionally, the code is also not version controlled, and any mistakes can take down the site.
The recommended approach is to implement the provided hooks in custom modules. If you're not doing this, expect problems. You've been warned!
Source & download: https://www.drupal.org/project/computed_field
Links to read:
- Computed Field code snippets: https://www.drupal.org/node/149228
- Community documentation: https://www.drupal.org/node/126522
- Developer documentation: https://www.drupal.org/docs/8/modules/computed-field-plugin/developer-documentation
- Drupal coding standard: https://www.drupal.org/node/318
- What are hook?: https://drupalize.me/tutorial/what-are-hooks?p=2766
- Hook: https://api.drupal.org/api/drupal/includes%21module.inc/group/hooks/7.x
- Understanding the hook: https://www.drupal.org/docs/8/creating-custom-modules/understanding-hooks
- Hooks Functions: https://api.drupal.org/api/drupal/core%21core.api.php/group/hooks/8.8.x
- How drupal module/hook system work: https://alanstorm.com/drupal_module_hooks/
- If statement: https://www.drupal.org/forum/support/module-development-and-code-questions/2014-01-16/solvedcomputed-field-help-with-code
- Multiply two fields: https://www.drupal.org/forum/support/post-installation/2012-04-07/how-to-multiply-two-fields-using-computed-field
- Views PHP: It lets you add code to Views from the Web UI, but you can't implement hooks in your own modules for your computations, at least not yet. See #1326780: Provide function templates for code to use instead of text areas for the feature request.
- Math Field (Mathematical Expression Field Formatter): Another potential alternative for field calculations.
- Field Token Value: Could be used for simple cases where Computed Field is too heavy.