You are here

function uc_catalog_add_node_type in Ubercart 8.4

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

Adds a catalog taxonomy reference field to the specified node type.

4 calls to uc_catalog_add_node_type()
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().
uc_catalog_node_type_insert in uc_catalog/uc_catalog.module
Implements hook_node_type_insert().
uc_product_kit_install in uc_product_kit/uc_product_kit.install
Implements hook_install().

File

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

Code

function uc_catalog_add_node_type($type) {
  $vid = \Drupal::config('uc_catalog.settings')
    ->get('vocabulary');
  if (!FieldStorageConfig::loadByName('node', 'taxonomy_catalog')) {
    FieldStorageConfig::create([
      'entity_type' => 'node',
      'field_name' => 'taxonomy_catalog',
      'type' => 'entity_reference',
      'settings' => [
        'target_type' => 'taxonomy_term',
      ],
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ])
      ->save();
  }
  if (!FieldConfig::loadByName('node', $type, 'taxonomy_catalog')) {
    FieldConfig::create([
      'field_name' => 'taxonomy_catalog',
      'entity_type' => 'node',
      'bundle' => $type,
      'label' => t('Catalog'),
      'settings' => [
        'handler_settings' => [
          'target_bundles' => [
            $vid => $vid,
          ],
          'auto_create' => TRUE,
        ],
      ],
    ])
      ->save();
  }

  // Make sure catalog field shows up on node edit form.
  \Drupal::service('entity_display.repository')
    ->getFormDisplay('node', $type)
    ->setComponent('taxonomy_catalog', [
    'type' => 'options_select',
  ])
    ->save();

  // Display catalog value on node view.
  \Drupal::service('entity_display.repository')
    ->getViewDisplay('node', $type)
    ->setComponent('taxonomy_catalog', [
    'type' => 'entity_reference_label',
  ])
    ->save();
}