I am new to Drupal8. In one of the preprocess functions in my customtheme.theme file, I need to get the value of a field from a view that I have already created. I searched the web and got:
$view = $variables['view'];
$view_result = $view->result;
if (!empty($view_result)) {
foreach ($view->result as $rid => $row) {
foreach ($view->field as $fid => $field ) {
$values[$rid][$fid] = $field->getValue($row);
}
}
}
$array[0] = $values[0]['nid'];
AND i got another one too :
$view = $variables['view'];
$values = [];
foreach ($view->result as $row_id => $row) {
foreach ($view->field as $field_id => $field ) {
$values[$row_id][$field_id] = $field->getValue($row);
}
}
The first solution worked but I want an explanation of it please especially that I tried to print_r ($view_result) and that didn't work, so I managed to print its keys and got onyl one key which is 0. Then I got many weird keys when I did a foreach to print the keys of $view_result[0]. I would be grateful if someone can explain to me the code and why did the getValue function take an argument ? I usually write it as getValue() only.