You are here

public function IpGeoLocViewsPluginStyle::pluginStyleSetMarkerColor in IP Geolocation Views & Maps 8

Set the marker color.

1 call to IpGeoLocViewsPluginStyle::pluginStyleSetMarkerColor()
IpGeoLocViewsPluginStyle::pluginStyleExtractLocations in src/Services/IpGeoLocViewsPluginStyle.php
Extract an array of locations from the supplied views_plugin_style.

File

src/Services/IpGeoLocViewsPluginStyle.php, line 984

Class

IpGeoLocViewsPluginStyle
Class IpGeoLocViewsPluginStyle.

Namespace

Drupal\ip_geoloc\Services

Code

public function pluginStyleSetMarkerColor(&$location, $row, $differentiator, $differentiator_color_associations) {
  if (!empty($differentiator)) {
    if (!empty($row->{$differentiator})) {
      $differentiator_value = $row->{$differentiator};
    }
    elseif (!empty($row->{'field_' . $differentiator})) {
      $differentiator_values = $row->{'field_' . $differentiator};
      if (is_array($differentiator_values)) {

        // Count > 1 for taxonomy term with lineage.
        $num_values = count($differentiator_values);
        if (isset($differentiator_values[0]['raw'])) {
          $differentiator_value = $differentiator_values[$num_values - 1]['raw'];
        }
        elseif (isset($differentiator_values[0]['rendered']['#markup'])) {
          $differentiator_value = $differentiator_values[$num_values - 1]['rendered']['#markup'];
        }
      }
    }
    else {
      $differentiator_value = '';
      if (!isset($row->{'field_' . $differentiator})) {
        $this->messenger
          ->addMessage($this->stringTranslation
          ->translate('IPGV&M: no differentiator values found for %diff. Cannot set marker images.', [
          '%diff' => $differentiator,
        ]), 'warning', FALSE);
      }
    }
    if (!empty($differentiator_value)) {
      foreach ($differentiator_color_associations as $association) {
        $match = TRUE;
        if (is_array($differentiator_value)) {
          if (empty($differentiator_value['tid'])) {

            // Eg AddressField, has multiple subvalues. All must match the
            // corresponding differentiator subvalues.
            foreach ($association['differentiator_value'] as $name => $subvalue) {
              if (!isset($differentiator_value[$name]) || !$this->ipGeolocGlobal
                ->isInRange($differentiator_value[$name], $subvalue)) {
                $match = FALSE;
                break;
              }
            }
          }
          else {

            // 'tid' set: taxonomy widget (plain or autocomplete)
            // $association['differentiator_value'] can have index 0 or 'tid'.
            $range = reset($association['differentiator_value']);
            $match = $this->ipGeolocGlobal
              ->isInRange($differentiator_value['tid'], $range);
          }
        }
        else {

          // Single value, either as simple value or array of length 1.
          $range = $association['differentiator_value'];
          $match = $this->ipGeolocGlobal
            ->isInRange($differentiator_value, $range);
        }
        if ($match) {
          $location->marker_color = $association['color'];
          if (!empty($association['special char'])) {
            $location->marker_special_char = $association['special char'];
          }
          if (!empty($association['special char class'])) {
            $location->marker_special_char_class = $association['special char class'];
          }
        }
      }
    }
  }
  if (isset($location->marker_color) && is_array($location->marker_color)) {
    $location->marker_color = reset($location->marker_color);
  }
}