Reputation: 1
Within a custom module, I'm working on validating a field based on an entry from another field. The validation works when the hash is hard coded (i.e. // $bookhash = 1;
). However, I cannot seem to figure out how to access hash of the referenced book.
What's the proper way of accessing that data from book referenced within book_signatures?
use \Drupal\Core\Entity\EntityTypeInterface;
/**
* Implements hook_entity_bundle_field_info_alter().
*/
function custom_validation_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $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]);
}
}
}
Edit: output from devel on the field $fields['field_book']. It's id outputs "node.book_signatures.field_book"
Upvotes: 0
Views: 423
Reputation: 1
Taking a slightly different approach exposed both fields more elegantly.
/**
* 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');
}
Upvotes: 0