You are here

public function PreprocessorPluginManager::getOrderedGeocodeFields in Geocoder 8.3

Same name and namespace in other branches
  1. 8.2 modules/geocoder_field/src/PreprocessorPluginManager.php \Drupal\geocoder_field\PreprocessorPluginManager::getOrderedGeocodeFields()

Get the ordered list of fields to be Geocoded | Reverse Geocoded.

Reorders the fields based on the user-defined GeocoderField weights.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The Entity that needs to be preprocessed.

Return value

\Drupal\Core\Field\FieldItemListInterface[] An array of field item lists implementing, keyed by field name.

File

modules/geocoder_field/src/PreprocessorPluginManager.php, line 68

Class

PreprocessorPluginManager
Provides a plugin manager for geocoder data preprocessors.

Namespace

Drupal\geocoder_field

Code

public function getOrderedGeocodeFields(ContentEntityInterface $entity) {
  $geocoder_fields = [];
  $results = [];
  foreach ($entity
    ->getFields() as $field_name => $field) {

    /** @var \Drupal\Core\Field\FieldConfigInterface $field_config */
    if (!($field_config = $field
      ->getFieldDefinition()) instanceof FieldConfigInterface) {

      // Only configurable fields are subject of geocoding.
      continue;
    }
    $geocoder = $field_config
      ->getThirdPartySettings('geocoder_field');
    if (empty($geocoder['method']) || $geocoder['method'] === 'none') {

      // This field was not configured to geocode/reverse_geocode from/to
      // other field.
      continue;
    }
    $geocoder_fields[$field_name] = [
      'field_name' => $field_name,
      'field_value' => $field,
      'weight' => isset($geocoder['weight']) ? $geocoder['weight'] : 0,
    ];
  }
  usort($geocoder_fields, function ($a, $b) {
    if ($a['weight'] === $b['weight']) {
      return 0;
    }
    return $a['weight'] < $b['weight'] ? -1 : 1;
  });
  foreach ($geocoder_fields as $field) {
    $results[$field['field_name']] = $field['field_value'];
  }
  return $results;
}