public function TrackingLinkFormatter::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/ TrackingLinkFormatter.php, line 27
Class
- TrackingLinkFormatter
- Plugin implementation of the 'commerce_tracking_link' formatter.
Namespace
Drupal\commerce_shipping\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
if ($items
->isEmpty()) {
return [];
}
/** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */
$shipment = $items[0]
->getEntity();
$shipping_method = $shipment
->getShippingMethod();
if (!$shipping_method) {
return [
[
'#markup' => Xss::filterAdmin((string) $shipment
->getTrackingCode()),
],
];
}
/** @var \Drupal\commerce_shipping\Plugin\Commerce\ShippingMethod\SupportsTrackingInterface $shipping_method_plugin */
$shipping_method_plugin = $shipment
->getShippingMethod()
->getPlugin();
if (!$shipping_method_plugin instanceof SupportsTrackingInterface) {
return [
[
'#markup' => Xss::filterAdmin((string) $shipment
->getTrackingCode()),
],
];
}
$tracking_url = $shipping_method_plugin
->getTrackingUrl($shipment);
if (!$tracking_url) {
return [];
}
$elements = [];
$elements[] = [
'#type' => 'link',
'#title' => $shipment
->getTrackingCode(),
'#url' => $tracking_url,
];
return $elements;
}