Skip to content

Instantly share code, notes, and snippets.

@twfahey1
Created September 13, 2022 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twfahey1/92fd9b84f5239d27effbf381acb50968 to your computer and use it in GitHub Desktop.
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
<?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