Created
April 17, 2023 08:25
-
-
Save cmcintosh/0093bc282084f79f7883a53f66507584 to your computer and use it in GitHub Desktop.
Example Code for hiding products for Commerce Stock.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file mymodule.module | |
* Includes drupal hooks for module. | |
*/ | |
use Drupal\mymodule\PremiumContentAccessControlHandler; | |
/** | |
* Implements hook_entity_type_alter(). | |
*/ | |
function mymodule_entity_type_alter(array &$entity_types) { | |
$entity_types['commerce_product']->setHandlerClass('access', ProductStockAccessControlHandler::class); | |
$entity_types['commerce_product_variation']->setHandlerClass('access', ProductVariationStockAccessControlHandler::class); | |
} | |
<?php | |
namespace Drupal\mymodule; | |
use Drupal\Core\Access\AccessResult; | |
use Drupal\Core\Field\FieldDefinitionInterface; | |
use Drupal\Core\Field\FieldItemListInterface; | |
use Drupal\Core\Session\AccountInterface; | |
use Drupal\node\NodeAccessControlHandler; | |
/** | |
* Defines the custom access control handler for the node entity type. | |
*/ | |
class StockAccessControlHandler extends NodeAccessControlHandler { | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function checkFieldAccess( | |
$operation, | |
FieldDefinitionInterface $field_definition, | |
AccountInterface $account, | |
FieldItemListInterface $items = NULL | |
) { | |
/* | |
* Allow specific roles to access premium content. | |
*/ | |
$hasAccess = FALSE; | |
if ($field_definition->getName() === 'field_stock_level') { | |
if ($operation == 'view') { | |
if (!($variation->stock_level->value > 0) && !$account->hasRole('administrator')) { | |
return AccessResult::forbidden(); | |
} | |
return AccessResult::allowedIfHasPermission( | |
$account, | |
'view premium content' | |
); | |
} | |
} | |
// Run parent's checkFieldAccess. | |
return parent::checkFieldAccess( | |
$operation, | |
$field_definition, | |
$account, | |
$items | |
); | |
} | |
} | |
<?php | |
namespace Drupal\mymodule; | |
use Drupal\Core\Access\AccessResult; | |
use Drupal\Core\Field\FieldDefinitionInterface; | |
use Drupal\Core\Field\FieldItemListInterface; | |
use Drupal\Core\Session\AccountInterface; | |
use Drupal\node\NodeAccessControlHandler; | |
/** | |
* Defines the custom access control handler for the node entity type. | |
*/ | |
class StockAccessControlHandler extends NodeAccessControlHandler { | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function checkFieldAccess( | |
$operation, | |
FieldDefinitionInterface $field_definition, | |
AccountInterface $account, | |
FieldItemListInterface $items = NULL | |
) { | |
/* | |
* Allow specific roles to access premium content. | |
*/ | |
$hasAccess = FALSE; | |
if ($field_definition->getName() === 'field_premium_content') { | |
if ($operation == 'view') { | |
foreach($entity->variations as $variation) { | |
if ($field_definition->getName() === 'field_stock_level') { | |
if ($variation->field_stock_level->value > 0) { | |
$hasAccess = true; | |
break; | |
} | |
} | |
} | |
if (!$hasAccess && !$account->hasRole('administrator')) { | |
return AccessResult::forbidden(); | |
} | |
return AccessResult::allowedIfHasPermission( | |
$account, | |
'view premium content' | |
); | |
} | |
} | |
// Run parent's checkFieldAccess. | |
return parent::checkFieldAccess( | |
$operation, | |
$field_definition, | |
$account, | |
$items | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment