You are here

UcDimensionsFormatter.php in Ubercart 8.4

File

uc_product/src/Plugin/Field/FieldFormatter/UcDimensionsFormatter.php
View source
<?php

namespace Drupal\uc_product\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;

/**
 * Plugin implementation of the Ubercart dimensions formatter.
 *
 * @FieldFormatter(
 *   id = "uc_dimensions",
 *   label = @Translation("Dimensions"),
 *   field_types = {
 *     "uc_dimensions",
 *   }
 * )
 */
class UcDimensionsFormatter extends FormatterBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($items as $delta => $item) {
      $dimensions = [];
      foreach ([
        'length',
        'width',
        'height',
      ] as $dimension) {
        if ((double) $item->{$dimension}) {
          $dimensions[] = uc_length_format($item->{$dimension}, $item->units);
        }
      }
      if ($dimensions) {
        $elements[$delta] = [
          '#markup' => implode(' × ', $dimensions),
        ];
      }
    }
    return $elements;
  }

}

Classes

Namesort descending Description
UcDimensionsFormatter Plugin implementation of the Ubercart dimensions formatter.