You are here

public function TaxNumberDefaultFormatter::viewElements in Commerce Core 8.2

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

modules/tax/src/Plugin/Field/FieldFormatter/TaxNumberDefaultFormatter.php, line 67

Class

TaxNumberDefaultFormatter
Plugin implementation of the 'commerce_tax_number_default' formatter.

Namespace

Drupal\commerce_tax\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $states = [
    'success' => $this
      ->t('Success'),
    'failure' => $this
      ->t('Failure'),
    'unknown' => $this
      ->t('Unknown'),
  ];
  $entity = $items
    ->getEntity();
  $elements = [];
  foreach ($items as $delta => $item) {
    $element = [];
    $element['value'] = [
      '#plain_text' => $item->value,
    ];
    if ($this
      ->getSetting('show_verification')) {
      $element['#attached']['library'][] = 'commerce_tax/tax_number';
      $context = UrlData::encode([
        $entity
          ->getEntityTypeId(),
        $entity
          ->id(),
        $this->fieldDefinition
          ->getName(),
        $this->viewMode,
      ]);
      if ($item->verification_result) {
        $element['value'] = [
          '#type' => 'link',
          '#title' => $item->value,
          '#url' => Url::fromRoute('commerce_tax.verification_result', [
            'tax_number' => $item->value,
            'context' => $context,
          ]),
          '#attributes' => [
            'class' => [
              'use-ajax',
            ],
            'data-dialog-type' => 'modal',
            'data-dialog-options' => Json::encode([
              'width' => 500,
              'title' => $item->value,
            ]),
          ],
        ];
      }
      if ($item->verification_state && isset($states[$item->verification_state])) {
        $element['verification_state'] = [
          '#type' => 'html_tag',
          '#tag' => 'span',
          '#attributes' => [
            'title' => $this
              ->t('Verification state: @state', [
              '@state' => $states[$item->verification_state],
            ]),
            'class' => [
              'commerce-tax-number__verification-icon',
              'commerce-tax-number__verification-icon--' . $item->verification_state,
            ],
          ],
        ];
        $options = [
          'query' => [
            'destination' => Url::fromRoute('<current>')
              ->toString(),
          ],
        ];
        $element['reverify'] = [
          '#type' => 'container',
          '#access' => $item->verification_state == VerificationResult::STATE_UNKNOWN,
        ];
        $element['reverify']['link'] = [
          '#type' => 'link',
          '#title' => $this
            ->t('Reverify'),
          '#url' => Url::fromRoute('commerce_tax.verify', [
            'tax_number' => $item->value,
            'context' => $context,
          ], $options),
        ];
      }
    }
    $elements[$delta] = $element;
  }
  return $elements;
}