You are here

public function LayerStyleLoader::load in farmOS 2.x

Load a single layer style definition, optionally filtered by conditions.

Parameters

array $conditions: An array of conditions for filtering.

Return value

\Drupal\farm_map\Entity\LayerStyleInterface|null Returns a layer style definition, or NULL if no matches were found.

Overrides LayerStyleLoaderInterface::load

File

modules/core/map/src/LayerStyleLoader.php, line 16

Class

LayerStyleLoader
Layer style loader.

Namespace

Drupal\farm_map

Code

public function load(array $conditions = []) : ?LayerStyleInterface {

  // Load all LayerStyle config entities.

  /** @var \Drupal\farm_map\Entity\LayerStyleInterface[] $layer_styles */
  $layer_styles = LayerStyle::loadMultiple();

  // If there are conditions, filter the styles.
  if (!empty($conditions)) {
    foreach ($conditions as $key => $value) {
      $layer_styles = array_filter($layer_styles, function ($layer_style) use ($key, $value) {
        $style_conditions = $layer_style
          ->getConditions();
        if (isset($style_conditions[$key])) {
          if (is_array($style_conditions[$key]) && in_array($value, $style_conditions[$key])) {
            return TRUE;
          }
          if ($style_conditions[$key] == $value) {
            return TRUE;
          }
        }
        return FALSE;
      });
    }
  }

  // If the filtered styles are not empty, return the first one.
  if (!empty($layer_styles)) {
    return reset($layer_styles);
  }
  return NULL;
}