You are here

function uc_catalog_add_image_field in Ubercart 8.4

Same name and namespace in other branches
  1. 7.3 uc_catalog/uc_catalog.module \uc_catalog_add_image_field()

Sets up a default image field on the Catalog vocabulary.

2 calls to uc_catalog_add_image_field()
CatalogAdminController::repairField in uc_catalog/src/Controller/CatalogAdminController.php
Repairs the catalog taxonomy field if it is lost or deleted.
uc_catalog_install in uc_catalog/uc_catalog.install
Implements hook_install().

File

uc_catalog/uc_catalog.module, line 320
Ubercart Catalog module.

Code

function uc_catalog_add_image_field() {
  if (!FieldStorageConfig::loadByName('taxonomy_term', 'uc_catalog_image')) {
    FieldStorageConfig::create([
      'entity_type' => 'taxonomy_term',
      'field_name' => 'uc_catalog_image',
      'type' => 'image',
    ])
      ->save();
  }

  // Only add the instance if it doesn't exist. Don't overwrite any changes.
  if (!FieldConfig::loadByName('taxonomy_term', 'catalog', 'uc_catalog_image')) {
    FieldConfig::create([
      'field_name' => 'uc_catalog_image',
      'entity_type' => 'taxonomy_term',
      'bundle' => 'catalog',
      'label' => t('Image'),
    ])
      ->save();
    \Drupal::service('entity_display.repository')
      ->getFormDisplay('taxonomy_term', 'catalog')
      ->setComponent('uc_catalog_image', [
      'type' => 'image_image',
    ])
      ->save();
    \Drupal::service('entity_display.repository')
      ->getViewDisplay('taxonomy_term', 'catalog')
      ->setComponent('uc_catalog_image', [
      'label' => 'hidden',
      'type' => 'image',
      'settings' => [
        'image_link' => 'content',
        'image_style' => 'uc_category',
      ],
    ])
      ->save();
  }
}