简体   繁体   中英

Drupal 9 Computed Fields Module

I installed the computed fields modules and I'm trying to make a computed field hook for the below.

计算域钩子

But I am not sure whether I put the code in the right place.

I just added the code below to the existing compute_field_api.php that comes with the compute field module. Is this the correct place to put this hook?

It doesn't seem to work and it doesn't display.

function computed_field_field_rating_average_compute($entity_type_manager, $entity, $fields, $delta)
{
  // Get rating fields to compute
  $facilities_and_services = $entity->field_facilities_and_services->value;
  $fairway_rating = $entity->field_fairway_rating->value;
  $recommendable_to_friends_rating = $entity->field_recommendable_to_friends->value;
  $food_rating = $entity->field_food_rating->value;
  $value_rating = $entity->field_value_rating->value;
  $english_rating = $entity->field_english_rating->value;
  $layout_rating = $entity->field_layout_rating->value;
  $quality_rating = $entity->field_quality_rating->value;
  $greens_rating = $entity->field_greens_rating->value;
  $length_rating = $entity->field_length_rating->value;

  // Set Computed field value
  $value = ($facilities_and_services + $fairway_rating + $recommendable_to_friends_rating + $food_rating + $value_rating + $english_rating + $layout_rating + $quality_rating + $greens_rating + $length_rating) / 10;
  return $value;
}

I tried to clear all the caches after adding this code. But it doesn't seem to work.

Had the same issue. Had to do the following:

  1. Installed the module per the readme instructions (follow those).
  2. Create the computed field function as you have above inside an enabled module (you may need to create a custom module for that). Note that the name of your custom module should not precede the function name (so it shouldn't be mymodule_computed_field_field_rating_average_compute , but rather as you have it above - ie, how you've written your function is fine).
  3. Flushed all caches.
  4. Edit and save the node(s) where the new value should appear. That causes it to create a row in the field table.

After that, the computed value should appear in the saved content, and in views for that resaved content.

Note that it doesn't seem to be dynamic (like the old dynamic fields in D6 used to be), so it won't just magically appear for all existing content. To achieve that en-mass, I had to do a bit of back-end SQL to avoid having to manually re-save every entity (didn't want to do that for over 120 of 'em). Still figuring out if I have to do some other trickery to keep the field up-to-date despite my cache settings, as the value I'm attempting to display is dynamic, and not dependent upon the node in which the field exists...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM