You are here

function uc_product_form_node_type_form_alter in Ubercart 8.4

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

Implements hook_form_FORM_ID_alter() for node_type_form().

Adds a default image field setting to product content types.

File

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

Code

function uc_product_form_node_type_form_alter(&$form, FormStateInterface $form_state) {

  /** @var \Drupal\node\NodeTypeInterface $type */
  $type = $form_state
    ->getFormObject()
    ->getEntity();
  $form['uc_product'] = [
    '#type' => 'details',
    '#title' => t('Ubercart product settings'),
    '#group' => 'additional_settings',
    '#tree' => TRUE,
    '#attached' => [
      'library' => [
        'uc_product/uc_product.scripts',
      ],
    ],
  ];
  $form['uc_product']['product'] = [
    '#type' => 'checkbox',
    '#title' => t('Content type is a product'),
    '#default_value' => $type
      ->getThirdPartySetting('uc_product', 'product', FALSE),
    '#weight' => -10,
  ];

  // Shippable.
  $form['uc_product']['shippable'] = [
    '#type' => 'checkbox',
    '#title' => t('Product is shippable'),
    '#default_value' => $type
      ->getThirdPartySetting('uc_product', 'shippable', TRUE),
    '#description' => t('This setting can still be overridden on the node form.'),
    '#weight' => -5,
  ];

  // Image field.
  $entity_type = $type
    ->getEntityType();
  if (!empty($entity_type)) {
    $options = [
      '' => t('None'),
    ];
    $instances = \Drupal::service('entity_field.manager')
      ->getFieldDefinitions('node', $type
      ->id());
    foreach ($instances as $field_name => $instance) {
      if ($instance
        ->getType() == 'image') {
        $options[$field_name] = $instance
          ->label();
      }
    }
    $form['uc_product']['image_field'] = [
      '#type' => 'select',
      '#title' => t('Product image field'),
      '#default_value' => $type
        ->getThirdPartySetting('uc_product', 'image_field', 'uc_product_image'),
      '#options' => $options,
      '#description' => t('The selected field will be used on Ubercart pages to represent the products of this content type.'),
      '#weight' => -4,
    ];
  }
  $form['#entity_builders'][] = 'uc_product_form_node_type_form_builder';
}