You are here

function IpGeoLocViewsPluginStyle::getViewResult in IP Geolocation Views & Maps 8

Returns as an array the raw value(s) of a field in the results set.

Parameters

object $views_plugin_style: The Views plugin.

mixed, field array or field name (string): The field or name of the field in the View.

int index: The row index into the Views result set.

Return value

array The raw value(s) of the field in the specified row of the results set.

File

src/Services/IpGeoLocViewsPluginStyle.php, line 1215

Class

IpGeoLocViewsPluginStyle
Class IpGeoLocViewsPluginStyle.

Namespace

Drupal\ip_geoloc\Services

Code

function getViewResult($views_plugin_style, $field, $index) {
  $field_name = $field
    ->getName();
  if (!isset($views_plugin_style->view->result[$index])) {
    $this
      ->messenger($this->stringTranslation
      ->translate("Field %name: no value found amongst view results.", array(
      '%name' => $field_name,
    )), 'warning', FALSE);
    return NULL;
  }
  $row = $views_plugin_style->view->result[$index];
  $row = $row->_entity
    ->getFields();
  if (isset($row->{"field_{$field_name}"}) && $row->{"field_{$field_name}"} != array()) {
    $values = $row->{"field_{$field_name}"};
  }
  elseif (isset($row->{"views_{$field_name}"})) {
    $values = $row->{"views_{$field_name}"};
  }
  elseif (isset($row->{$field_name})) {
    $values = $row->{$field_name};
  }
  elseif (isset($views_plugin_style->view->field[$field_name])) {
    $values = $views_plugin_style
      ->getFieldValue($index, $field_name);
  }
  elseif (!empty($row->_entity_properties)) {

    // Search API emergency fallback
    $values = $this
      ->pluginStyleGetSearchApiValue($row, $field_name);
  }
  else {
    return array(
      '',
    );
  }
  if (!is_array($values)) {
    return array(
      $values,
    );
  }
  $single_values = array();
  foreach ($values as $value) {
    if (isset($value['raw'])) {
      $value = $value['raw'];
    }
    if (isset($field['type'])) {
      if ($field['type'] == 'addressfield') {
        return $value;
      }
      if (is_array($value)) {
        $value = $field['type'] == 'taxonomy_term_reference' ? $value['tid'] : reset($value);
      }
    }
    elseif (is_array($value)) {
      $value = reset($value);
    }
    $single_values[] = $value;
  }
  return $single_values;
}