Drupal8-通过$fields访问引用的实体(在节点创建时实现验证)



在一个自定义模块中,我正在根据另一个字段的条目验证一个字段。当散列是硬编码的(即// $bookhash = 1;(时,验证工作。然而,我似乎不知道如何访问参考书的散列。

访问book_signatures中引用的书籍中的数据的正确方法是什么?

use  DrupalCoreEntityEntityTypeInterface;
/**
* Implements hook_entity_bundle_field_info_alter().
*/
function custom_validation_entity_bundle_field_info_alter(&$fields, DrupalCoreEntityEntityTypeInterface $entity_type, $bundle) {
if ($bundle === 'book_signatures') {

if (isset($fields['field_confirm_book_hash'])) {
$book = $fields['field_book']; // book is a referenced entity within book_signatures. 
$bookhash = $book->field_hash_check; // need to set this equal to the hash in the book entity.
// $bookhash = 1; works with static hash. need specific books hash
$fields['field_confirm_book_hash']->addConstraint('BookHash', ['hash' => $bookhash]);
}
}
}

编辑:字段$fields['field_book']上的devel输出。它是id输出"node.book_signatures.field_book">

采用稍微不同的方法可以更优雅地显示这两个字段。

/**
* Implements hook_entity_type_build().
*/
function custom_validation_entity_type_build(array &$entity_types) {
// Add our custom validation to the order number.
$entity_types['node']->addConstraint('BookHash');
}

最新更新