Does this also work with the "Opt-in" consent method, or only "Opt-in with categories"?
Will support for Drupal 10 return?
@wxman I've stuck with php 8.1
@joachim I don't feel confident enough yet to write documentation. I've been wrestling with using computed fields in Views and Charts. With the 8.x version of computed fields I needed to use aggregation on the computed field and then charts would draw as expected. In 4.x, aggregation doesn't seem to be an option (but this makes sense, right? No database storage for computed field?), so I'm tinkering with field grouping and getting somewhat close.
It seems the log message is related to php 8.2. I switched to 8.1 and the log looks good.
@wxman do you see log messages related to your views.inc file along the lines of this?
Deprecated function: Creation of dynamic property Drupal\computed_field\Field\FieldStorageDefinition::$schema is deprecated in Drupal\computed_field\Field\FieldStorageDefinition->getSchema()
@maxilein here's an example of my setup:
My custom module lives at /web/modules/custom/computed_float
In the module directory I have:
1) computed_float.info.yml
name: 'Computed Float'
type: module
description: 'Computes "computed_float" field value'
package: Custom Computed Fields
core_version_requirement: '^8 || ^9 || ^10'
2) computed_float.views.inc
<?php
/**
* @file
* Allows the computed field Computed Float (computed_float) to be used in Views.
*/
/**
* Implements hook_views_data().
*/
function computed_float_views_data() {
$data['node_field_data']['computed_float'] = [
'title' => t('Computed float'),
'entity field' => 'computed_float',
'field' => [
'id' => 'field',
],
];
return $data;
}
3) plugin directory /src/Plugin/ComputedField containing ComputedFloat.php
<?php
namespace Drupal\computed_float\Plugin\ComputedField;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\computed_field\Field\ComputedFieldDefinitionWithValuePluginInterface;
use Drupal\computed_field\Plugin\ComputedField\ComputedFieldBase;
/**
* TODO: class docs.
*
* @ComputedField(
* id = "computed_float",
* label = @Translation("Computed Float"),
* field_type = "float",
* )
*/
class ComputedFloat extends ComputedFieldBase {
/**
* {@inheritdoc}
*/
public function computeValue(EntityInterface $host_entity, ComputedFieldDefinitionWithValuePluginInterface $computed_field_definition): array {
if(!empty($host_entity->field_my_source_data->value)) {
$sourceData = $host_entity->field_my_source_data->value;
$myFloat = $sourceData;
}
return [$myFloat];
}
}
@maxilein, @marcoka - I created about a dozen computed field plugins. Each lives in it's own custom module.
You folks rock! Thank you so much.
1) I had only applied the patch for bundle fields. I needed the base fields patch.
2) I was missing the *.view.inc file - @wxman I repurposed your code for one of my computed fields and it's working.
Presently I have multiple plugins (one for each computed field) in my custom module. Is it possible to include them all in a single *.view.inc file, or is better to split the plugins into separate modules?
@wxman I'm on Drupal 10 and have the fields working as expected on nodes. When I create a View, however, I don't see any of them in the "Add fields" interface.
Maybe I'm missing something here:
* attach = {
* "scope" = "",
* "field_name" = "",
* "entity_types" = {
* "" = {},
* },
* },
I'm not sure how to apply this from the README:
"Automatically declare computed base fields to Views: https://www.drupal.org/project/drupal/issues/3349739 📌 Automatically declare computed base fields to Views Needs work "
@wxman were you successful in getting computed fields to work in views?
Thanks to this discussion I have computed fields working, but I'm stumped on the views portion.