You are here

public function Subdivision::render in Address 8

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

Overrides FieldPluginBase::render

File

src/Plugin/views/field/Subdivision.php, line 83

Class

Subdivision
Displays the subdivision.

Namespace

Drupal\address\Plugin\views\field

Code

public function render(ResultRow $values) {
  $value = $this
    ->getValue($values);
  if (empty($value) || empty($this->options['display_name'])) {
    return $this
      ->sanitizeValue($value);
  }
  $entity = $this
    ->getEntity($values);

  /** @var \Drupal\address\AddressInterface $address */
  $address = $entity->{$this->definition['field_name']}
    ->first();
  switch ($this->definition['property']) {
    case 'administrative_area':
      $code = $address
        ->getAdministrativeArea();
      $parents = [
        $address
          ->getCountryCode(),
      ];
      break;
    case 'locality':
      $code = $address
        ->getLocality();
      $parents = [
        $address
          ->getCountryCode(),
        $address
          ->getAdministrativeArea(),
      ];
      break;
    case 'dependent_locality':
      $code = $address
        ->getDependentLocality();
      $parents = [
        $address
          ->getCountryCode(),
        $address
          ->getAdministrativeArea(),
        $address
          ->getLocality(),
      ];
      break;
  }

  /** @var \CommerceGuys\Addressing\Subdivision\Subdivision $subdivision */
  $subdivision = $this->subdivisionRepository
    ->get($code, $parents);
  if ($subdivision) {
    $use_local_name = Locale::matchCandidates($address
      ->getLocale(), $subdivision
      ->getLocale());
    $value = $use_local_name ? $subdivision
      ->getLocalName() : $subdivision
      ->getName();
  }
  return $this
    ->sanitizeValue($value);
}