i have the following array:
$keyvisual_data = array(
\'video_file\' => $row->field_field_video[0][\'rendered\'][\'#item\'][\'uri\'],
No there is not
You can do an isset()
:
if(isset($array[0])){
echo $array[0];
}
else {
//some error?
}
Or if you know that you are only going to be checking index 0:
$array = $array + array(null);
So if the original $array[0]
was unset, now it is null
You could add a shorthand check if each value is null
$default_value = "Default value";
$keyvisual_data = array(
'video_file' => ($row->field_field_video[0]['rendered']['#item']['uri'] == null) ? $default_value : $row->field_field_video[0]['rendered']['#item']['uri']
// etc.. etc...
);
A less messy version of this, to explain the code more clearly:
<?php
$default_value = "I am a Default value";
$video_file = null;
$new_array = array(
'video_file' => ($video_file == null) ? $default_value : $video_file
);
// Will output "I am a Default value"
echo $new_array['video_file'];
?>
Yes, add @
before the field like:
$keyvisual_data = array(
'video_file' => @$row->field_field_video[0]['rendered']['#item']['uri'],
'bild_file' => @$row->field_field_bild[0]['rendered']['#item']['uri'],
'subline_src' => @$row->_field_data['nid']['entity']->field_key_titel['und'][0]['safe_value'],
'screenreader_src' => @$row->field_field_alt_screenreader[0]['rendered']['#markup'],
'alt_src' => @$row->field_field_bild[0]['rendered']['#item']['alt']
);
and then initialize the nulls:
if($keyvisual_data['video_file'] === null)
$keyvisual_data['video_file'] = $default_video_file;
etc...
To be honest, the structure of the row data object doesnt seem very convinient and error prone. Especially if you are using $row in some other context, too, I would suggest you wrap it in an object:
class RowWrapper
{
private $_row;
public function __construct($row)
{
$this->_row = $row;
}
public function getVideoFile()
{
if (!isset($row->field_field_video[0])) {
return null;
}
return $row->field_field_video[0]['rendered']['#item']['uri'];
}
...
}
$rowWrapper = new RowWrapper($row);
$keyvisual_data = array(
'video_file' => $rowWrapper->getVideoFile(),
...
);
If you there is only one value e. g. inside of $row->field_field_video
then it shouldnt be an array. I would use a wrapper class as a kind of anti corruption layer if that strange datastructure comes from a remote source or the like. Otherwise it will creep through your while application with $row->field_field_video[0]...
stuff all over the place.
I know this is not the fast and easy solution you want, but swallowing errors is never a good idea.
You should use "isset" function to check the existence of the field, and infact its a good coding practice..
$keyvisual_data = array(
'video_file' => isset($rowWrapper->getVideoFile()) ? $rowWrapper->getVideoFile() : "any default value",
...
);
or you can use it in this way :
if(isset($rowWrapper->getVideoFile()))
{
$row['video_file'] = $rowWrapper->getVideoFile();
}
else {
$row['video_file'] = "";
}