Created
April 24, 2023 21:55
-
-
Save MatthieuScarset/5af89e37f828064737a4e516de604e0b to your computer and use it in GitHub Desktop.
Drupal - Get values of a View result row.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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