Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 292486

Always return an array from field_get_items()

$
0
0

While un-common in core, the following pattern is used a lot in the wild:

<?php
if ($items = field_get_items($entity, $field_name, $langcode)) {
  foreach (
$items as $item) {
    ...
  }
}
?>

So couldn't we just always return an array?

<?php
-  return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
+  return isset(
$entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
?>

So in core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/argument_default/Tid.php

<?php
$items
= field_get_items($node, $name);
if (
is_array($items)) {
  foreach (
field_get_items($node, $name) as $item) {
   
$taxonomy[$item['tid']] = $field_info['settings']['allowed_values'][0]['vocabulary'];
  }
}
?>

would become

<?php
foreach (field_get_items($node, $name) as $item) {
 
$taxonomy[$item['tid']] = $field_info['settings']['allowed_values'][0]['vocabulary'];
}
?>
AttachmentSizeStatusTest resultOperations
drupal-return-array-from-field_get_items.patch675 bytesTest request sentNoneView details

Viewing all articles
Browse latest Browse all 292486

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>