You are here

public function Products::productSelectSearch in Ubercart 8.4

Sets the order pane to show the product selection form.

File

uc_order/src/Plugin/Ubercart/OrderPane/Products.php, line 443

Class

Products
Manage the products an order contains.

Namespace

Drupal\uc_order\Plugin\Ubercart\OrderPane

Code

public function productSelectSearch($form, FormStateInterface $form_state) {
  $types = uc_product_types();
  $options = [];
  $query = \Drupal::database()
    ->select('node_field_data', 'n')
    ->fields('n', [
    'nid',
    'title',
  ])
    ->condition('n.type', $types, 'IN')
    ->orderBy('n.title')
    ->addTag('node_access');
  if (!$form_state
    ->isValueEmpty([
    'product_controls',
    'product_search',
  ])) {
    $search = strtolower(str_replace('*', '%', $form_state
      ->getValue([
      'product_controls',
      'product_search',
    ])));
    $query
      ->leftJoin('uc_products', 'p', 'n.nid = p.nid');
    $query
      ->condition((new Condition('OR'))
      ->condition('n.title', $search, 'LIKE')
      ->condition('p.model', $search, 'LIKE'));
  }
  $result = $query
    ->execute();
  foreach ($result as $row) {
    $options[$row->nid] = $row->title;
  }
  if (count($options) == 0) {
    $options[0] = $this
      ->t('No products found.');
  }
  $form_state
    ->set('products_action', 'select');
  $form_state
    ->set('product_select_options', $options);
  $form_state
    ->set('refresh_products', NULL);
  $form_state
    ->setRebuild();
}