function uc_product_add_default_image_field in Ubercart 8.4
Same name and namespace in other branches
- 6.2 uc_product/uc_product.module \uc_product_add_default_image_field()
- 7.3 uc_product/uc_product.module \uc_product_add_default_image_field()
Creates a file field with an image field widget, and attach it to products.
This field is used by default on the product page, as well as on the cart and catalog pages to represent the products they list. Instances are added to new product classes, and other node types that claim product-ness should call this function for themselves.
Parameters
$type: The content type to which the image field is to be attached. This may be a a single type as a string, or an array of types. If NULL, all product types get an instance of the field.
3 calls to uc_product_add_default_image_field()
- ProductController::setImageDefaults in uc_product/
src/ Controller/ ProductController.php - Sets up the default image field for products.
- uc_product_kit_install in uc_product_kit/
uc_product_kit.install - Implements hook_install().
- uc_product_node_type_insert in uc_product/
uc_product.module - Implements hook_node_type_insert().
File
- uc_product/
uc_product.module, line 1021 - The product module for Ubercart.
Code
function uc_product_add_default_image_field($type = NULL) {
// Set up field if it doesn't exist.
if (!FieldStorageConfig::loadByName('node', 'uc_product_image')) {
FieldStorageConfig::create([
'entity_type' => 'node',
'field_name' => 'uc_product_image',
'type' => 'image',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'settings' => [
'display_field' => 0,
],
])
->save();
}
if ($type) {
// Accept single or multiple types as input.
$types = (array) $type;
}
else {
$types = uc_product_types();
}
foreach ($types as $type) {
$field_name = NodeType::load($type)
->getThirdPartySetting('uc_product', 'image_field', 'uc_product_image');
// Only add the instance if it doesn't exist. Don't overwrite any changes.
if ($field_name && !FieldConfig::loadByName('node', $type, $field_name)) {
FieldConfig::create([
'entity_type' => 'node',
'bundle' => $type,
'field_name' => $field_name,
'label' => t('Image'),
'weight' => -2,
])
->save();
NodeType::load($type)
->setThirdPartySetting('uc_product', 'image_field', $field_name)
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('node', $type)
->setComponent($field_name, [
'type' => 'image_image',
])
->save();
\Drupal::service('entity_display.repository')
->getViewDisplay('node', $type)
->setComponent($field_name, [
'label' => 'hidden',
'type' => 'uc_product_image',
])
->save();
\Drupal::service('entity_display.repository')
->getViewDisplay('node', $type, 'teaser')
->setComponent($field_name, [
'label' => 'hidden',
'type' => 'uc_product_image',
])
->save();
}
}
}