public function ShippingMethodFormatter::viewElements in Commerce Shipping 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
- src/
Plugin/ Field/ FieldFormatter/ ShippingMethodFormatter.php, line 79
Class
- ShippingMethodFormatter
- Plugin implementation of the 'commerce_shipping_method' formatter.
Namespace
Drupal\commerce_shipping\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
/** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */
$shipment = $items
->getEntity();
$shipping_service_id = $shipment
->getShippingService();
$elements = [];
foreach ($items as $delta => $item) {
/** @var \Drupal\commerce_shipping\Entity\ShippingMethodInterface $shipping_method */
$shipping_method = $item->entity;
if (!$shipping_method) {
// The shipping method could not be loaded, it was probably deleted.
continue;
}
$shipping_method = $this->entityRepository
->getTranslationFromContext($shipping_method, $langcode);
$shipping_services = $shipping_method
->getPlugin()
->getServices();
if (isset($shipping_services[$shipping_service_id])) {
$elements[$delta] = [
'#markup' => $shipping_services[$shipping_service_id]
->getLabel(),
];
}
}
return $elements;
}