You are here

function uc_product_get_picture in Ubercart 8.4

Same name and namespace in other branches
  1. 5 uc_product/uc_product.module \uc_product_get_picture()
  2. 6.2 uc_product/uc_product.module \uc_product_get_picture()
  3. 7.3 uc_product/uc_product.module \uc_product_get_picture()

Returns a product node's first attached image.

Parameters

int $nid: The node's id.

string $style: The image style used to format the image. 'uc_product' by default.

Return value

array A renderable array of the first product image, linked to the product node, or an empty array if no image is available.

1 call to uc_product_get_picture()
CartForm::buildForm in uc_cart/src/Form/CartForm.php
Form constructor.

File

uc_product/uc_product.module, line 799
The product module for Ubercart.

Code

function uc_product_get_picture($nid, $style = 'uc_product') {
  $product = Node::load($nid);
  if (!$product) {
    return [];
  }
  $field_name = $product->type->entity
    ->getThirdPartySetting('uc_product', 'image_field', 'uc_product_image');
  $output = [];
  if ($field_name && !empty($product->{$field_name})) {
    $elements = $product->{$field_name}
      ->view([
      'label' => 'hidden',
      'type' => 'image',
      'settings' => [
        'image_link' => 'content',
        'image_style' => $style,
      ],
    ]);

    // Extract the part of the render array we need.
    $output = isset($elements[0]) ? $elements[0] : [];
    if (isset($elements['#access'])) {
      $output['#access'] = $elements['#access'];
    }
  }
  return $output;
}