You are here

function tca_commerce_product_views_query_alter in Token Content Access 8

Same name and namespace in other branches
  1. 2.0.x modules/tca_commerce_product/tca_commerce_product.module \tca_commerce_product_views_query_alter()

Implements hook_views_query_alter().

File

modules/tca_commerce_product/tca_commerce_product.module, line 55
Contains tca_commerce_product.module.

Code

function tca_commerce_product_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  $account = \Drupal::currentUser();
  $bypass_permitted = $account
    ->hasPermission('tca bypass commerce_product');
  if (!$bypass_permitted) {
    $disabled_types = _tca_commerce_product_get_disabled_types();
    $info = $query
      ->getEntityTableInfo('commerce_product_field_data');
    if (isset($info['commerce_product'])) {
      $alias = $info['commerce_product']['alias'];
      $group_name = 'tca_commerce_product';
      $query->where[$group_name] = [
        'conditions' => [
          [
            'field' => $alias . '.tca_active',
            'value' => NULL,
            'operator' => 'IS NULL',
          ],
          [
            'field' => $alias . '.tca_active',
            'value' => 0,
            'operator' => '=',
          ],
        ],
        'args' => [],
        'type' => 'OR',
      ];

      // Add disabled types condition.
      if (!empty($disabled_types)) {
        $query->where[$group_name]['conditions'][] = [
          'field' => $alias . '.type',
          'value' => $disabled_types,
          'operator' => 'IN',
        ];
      }
    }
  }
}