You are here

class FieldHelper in Address 8

Provides property names and autocomplete attributes for AddressField values.

Hierarchy

Expanded class hierarchy of FieldHelper

5 files declare their use of FieldHelper
Address.php in src/Element/Address.php
AddressDefaultFormatter.php in src/Plugin/Field/FieldFormatter/AddressDefaultFormatter.php
AddressFormatConstraintValidator.php in src/Plugin/Validation/Constraint/AddressFormatConstraintValidator.php
AddressItem.php in src/Plugin/Field/FieldType/AddressItem.php
ZoneTerritory.php in src/Element/ZoneTerritory.php

File

src/FieldHelper.php, line 10

Namespace

Drupal\address
View source
class FieldHelper {

  /**
   * Gets the property name matching the given AddressField value.
   *
   * @param string $field
   *   An AddressField value.
   *
   * @return string
   *   The property name.
   */
  public static function getPropertyName($field) {
    $property_mapping = [
      AddressField::ADMINISTRATIVE_AREA => 'administrative_area',
      AddressField::LOCALITY => 'locality',
      AddressField::DEPENDENT_LOCALITY => 'dependent_locality',
      AddressField::POSTAL_CODE => 'postal_code',
      AddressField::SORTING_CODE => 'sorting_code',
      AddressField::ADDRESS_LINE1 => 'address_line1',
      AddressField::ADDRESS_LINE2 => 'address_line2',
      AddressField::ORGANIZATION => 'organization',
      AddressField::GIVEN_NAME => 'given_name',
      AddressField::ADDITIONAL_NAME => 'additional_name',
      AddressField::FAMILY_NAME => 'family_name',
    ];
    return isset($property_mapping[$field]) ? $property_mapping[$field] : NULL;
  }

  /**
   * Gets the autocomplete attribute for the given AddressField value.
   *
   * Source: https://html.spec.whatwg.org/multipage/forms.html#autofill.
   *
   * @param string $field
   *   An AddressField value.
   *
   * @return string
   *   The autocomplete attribute.
   */
  public static function getAutocompleteAttribute($field) {
    $autocomplete_mapping = [
      AddressField::ADMINISTRATIVE_AREA => 'address-level1',
      AddressField::LOCALITY => 'address-level2',
      AddressField::DEPENDENT_LOCALITY => 'address-level3',
      AddressField::POSTAL_CODE => 'postal-code',
      AddressField::SORTING_CODE => 'sorting-code',
      AddressField::ADDRESS_LINE1 => 'address-line1',
      AddressField::ADDRESS_LINE2 => 'address-line2',
      AddressField::ORGANIZATION => 'organization',
      AddressField::FAMILY_NAME => 'family-name',
      AddressField::ADDITIONAL_NAME => 'additional-name',
      AddressField::GIVEN_NAME => 'given-name',
    ];
    return isset($autocomplete_mapping[$field]) ? $autocomplete_mapping[$field] : NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldHelper::getAutocompleteAttribute public static function Gets the autocomplete attribute for the given AddressField value.
FieldHelper::getPropertyName public static function Gets the property name matching the given AddressField value.