Skip to content

Fix entity reference field key and add null safety for taxonomy term loading#17

Merged
protitude merged 2 commits into
media-1202from
copilot/sub-pr-16
Mar 25, 2026
Merged

Fix entity reference field key and add null safety for taxonomy term loading#17
protitude merged 2 commits into
media-1202from
copilot/sub-pr-16

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 25, 2026

field_service_alert_dash_cat is an entity reference field, but its referenced entity ID was being read via ['value'] instead of ['target_id'], causing load() to receive bad input and return NULL — followed by a fatal on $taxonomy->get(...).

Changes

  • Wrong array key: getValue()[0]['value']getValue()[0]['target_id'] to correctly retrieve the referenced term ID
  • Null safety: Wrap taxonomy field access in if ($taxonomy !== NULL) guard after load()
  • Empty field guard: Check !empty($tax_image_values) before accessing [0]['target_id'] on field_twitter_image
// Before
$set_dashboard = $data['node']->get('field_service_alert_dash_cat')->getValue()[0]['value'];
$taxonomy = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($set_dashboard);
$tax_image = $taxonomy->get('field_twitter_image'); // fatal if load() returns NULL

// After
$set_dashboard = $data['node']->get('field_service_alert_dash_cat')->getValue()[0]['target_id'];
$taxonomy = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($set_dashboard);
$image_id = NULL;
if ($taxonomy !== NULL) {
  $tax_image_values = $taxonomy->get('field_twitter_image')->getValue();
  $image_id = !empty($tax_image_values) ? ($tax_image_values[0]['target_id'] ?? NULL) : NULL;
}

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

Copilot AI mentioned this pull request Mar 25, 2026
3 tasks
Copilot AI changed the title [WIP] [WIP] Address feedback from review on Media 1202 pull request Fix entity reference field key and add null safety for taxonomy term loading Mar 25, 2026
Copilot AI requested a review from protitude March 25, 2026 16:20
@protitude protitude marked this pull request as ready for review March 25, 2026 16:28
@protitude protitude merged commit 4379621 into media-1202 Mar 25, 2026
@protitude protitude deleted the copilot/sub-pr-16 branch March 25, 2026 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants