- Issue created by @maxilein
- Status changed to Fixed
about 2 years ago 7:40am 18 February 2023 - 🇬🇧United Kingdom joachim
In version 4, the PHP code goes in a custom plugin.
- 🇦🇹Austria maxilein
I could not find any documentation for 4.
So I downloaded the current dev version and extracted the Readme.md - in any case someone else is interested while researching prerequisites:## Developers The value of a computed field is generated by a computed field plugin. Creating a computed field in the admin UI creates a config entity of type 'computed_field', similarly to how creating a normal stored field creates a 'field_config' config entity. The computed_field config entity holds the computed field plugin ID and its configuration, if there is any. A computed field plugin defines: - The field type, e.g. 'string' or 'entity_reference'. These are the same field types as used by normal stored fields, which allows existing field formatters to be used. - The code which returns the field value. - The cacheability of the field values. If a computed field's value is dynamic or depends on other things than the entity it is on, the output of the field can be rendered with a lazy builder. See the test module test_computed_field_plugins for examples of different cacheability. Computed field plugins can either automatically attach fields to entity types, or be configured to attach in the admin UI using a computed field config entity. ### Automatically attached plugins The plugin's definition automatically declares computed fields. The plugin annotation declares: - Which entity types it is attached to. - The scope: whether it is a base field or a bundle field. - Which bundles it is attached to in the latter case. There is no configuration for automatically attaching plugins. If the plugin is doing double duty as a configured attaching plugin, and has configuration, it must supply default values for the configuration. To prevent an automatically attached plugin from showing in the UI for computed fields, set its no_ui annotation property. See the test module test_computed_field_automatic for examples of these. ### Configured attaching plugins Site admins create computed fields, using this plugin as the source of the field value. The plugin may be configurable, and for this it must implement both of: - \Drupal\Component\Plugin\ConfigurableInterface - \Drupal\Core\Plugin\PluginFormInterface A computed field created by creating a computed field entity is always a bundle field, not a base field. See the test module test_computed_field_plugins for examples of these.
- 🇬🇧United Kingdom joachim
> I could not find any documentation for 4.
You found the documentation! It's the README :)
- 🇦🇹Austria maxilein
Thank you. Since I never developed a custom plugin, may I ask would a code like below in custom module still work?
e.g.
function computed_field_field_test_total_hours_compute($entity_type_manager, $entity, $fields, $delta) { // Get and set Start time $get_stime = $entity->field_friday_hours->value; $start_time = new DrupalDateTime($get_stime); // Get and set End time $get_etime = $entity->get('field_friday_hours')->getValue()[0]['end_value']; $end_time = new DrupalDateTime($get_etime); // Calculate hours to decimal $diff = $end_time->getTimestamp() - $start_time->getTimestamp(); $hours = $diff/3600; $tot_hours = round($hours, 2); // Set Computed field value $value = $tot_hours; return $value; }
That sample is taken from here: https://stackoverflow.com/questions/73128429/calculate-computed-field-fr...
- 🇬🇧United Kingdom joachim
Have a look at the TestRequestTime plugin in the module.
You can use Module Builder to generate the boilerplate for a custom module and the plugin.
- 🇦🇹Austria maxilein
Sorry. I looked at it. But it did not really help me. I never built a plugin before. So it is a steep curve...
The documentation does not explain how I would migrate above code. I could not find any examples.
Where are the $entity_type_manager, $entity, $fields, $delta and how would I access these inside the plugin class?
I understand the plugin represents the field directly?
How do I access a fields value?Thank you for providing information! Computed field is such a useful module. I will gladly put this into the documentation once I figured out all the peaces.
- 🇬🇧United Kingdom joachim
Have you installed Module Builder? Use that to generate your plugin.
The examples are in the source code -- download the module and look for TestRequestTime. Or browse the repo here: https://git.drupalcode.org/project/computed_field/-/tree/4.0.x/tests/mod...
- $entity_type_manager - get it from the \Drupal class
- $entity, - the $host_entity parameter
- $fields, $delta - your code doesn't use these it seems?