Created
October 21, 2021 12:22
-
-
Save opi/cc026d34d402587b6af1b4f14e11f046 to your computer and use it in GitHub Desktop.
Computed field example for Drupal 8/9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_entity_bundle_field_info(). | |
*/ | |
function MY_MODULE_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { | |
$new_definitions = []; | |
if ($entity_type->id() === 'node') { | |
if ($bundle == 'blog') { | |
$new_definitions['blog_author'] = \Drupal\Core\Field\BaseFieldDefinition::create('entity_reference') | |
->setLabel(t('Author (computed)')) | |
->setComputed(TRUE) | |
->setClass('\Drupal\MY_MODULE\ComputedField\BlogAuthorComputed') | |
->setSettings(array( | |
'target_type' => 'user', | |
'default_value' => 0, | |
)) | |
->setDisplayConfigurable('view', TRUE); | |
} | |
} | |
return $new_definitions; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment