Created
September 13, 2022 02:02
-
-
Save twfahey1/92fd9b84f5239d27effbf381acb50968 to your computer and use it in GitHub Desktop.
Drupal example - Get a default entity reference target from a field and check if a node has the default value
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 | |
// Get the default target_id for this field from the field settings. | |
$default_target_uuid = $loaded_node->field_some_entity_ref_field->getFieldDefinition()->get('default_value')[0]['target_uuid']; | |
$default_media_entity = $this->entityRepository->loadEntityByUuid('media', $default_target_uuid); | |
$default_media_entity_id = $default_media_entity->id(); | |
// Check if is the default one. | |
$photo = $loaded_node->field_some_entity_ref_field->getValue(); | |
if ($photo[0]['target_id'] == $default_media_entity_id) { | |
$total_staff_with_no_photo++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment