简体   繁体   English

Drupal 9 计算域模块

[英]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.我只是将下面的代码添加到计算字段模块附带的现有 compute_field_api.php 中。 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).在启用的模块中创建计算字段 function(您可能需要为此创建一个自定义模块)。 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).请注意,您的自定义模块的名称不应在 function 名称之前(因此它不应是mymodule_computed_field_field_rating_average_compute ,而是如上所示 - 即,您编写 function 的方式很好)。
  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.请注意,它似乎不是动态的(就像 D6 中的旧动态字段一样),因此它不会神奇地出现在所有现有内容中。 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).为了实现这一点,我不得不做一些后端 SQL 以避免必须手动重新保存每个实体(不想为超过 120 个实体这样做)。 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...仍然想知道我是否必须做一些其他的技巧来保持字段最新,尽管我的缓存设置,因为我试图显示的值是动态的,而不依赖于字段所在的节点。 .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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