Skip to content

Instantly share code, notes, and snippets.

@MatthieuScarset
Created April 24, 2023 21:55
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 MatthieuScarset/5af89e37f828064737a4e516de604e0b to your computer and use it in GitHub Desktop.
Save MatthieuScarset/5af89e37f828064737a4e516de604e0b to your computer and use it in GitHub Desktop.
Drupal - Get values of a View result row.
<?php
/**
* Get the value out of a view Result for a given date field.
*
* @param \Drupal\views\ResultRow $result
* A given view result.
* @param \Drupal\views\Plugin\views\field\EntityField $field
* A given date field.
*
* @return array
* Either the timestamp or nothing.
*/
function get_row_values_from_view_field(ResultRow $row, EntityField $field) {
$delta = 0;
if ($delta_field = $field->aliases['delta'] ?? NULL) {
$delta = $row->{$delta_field} ?? 0;
}
// Get the result we need from the entity.
$items = $field->getItems($row) ?? [];
$item = $items[$delta]['raw'] ?? NULL;
return $item instanceof FieldItemInterface ? $item->getValue() : [];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment