Created
September 20, 2020 08:43
-
-
Save init90/0e48acd74d5a380a0da9cf351f0f97ea to your computer and use it in GitHub Desktop.
Product type block visibility condition plugin.
This file contains 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 | |
namespace Drupal\swimsports_commerce\Plugin\Condition; | |
use Drupal\Core\Condition\ConditionPluginBase; | |
use Drupal\Core\Entity\EntityStorageInterface; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* Provides a 'Product Type' condition. | |
* | |
* @Condition( | |
* id = "product_type", | |
* label = @Translation("Product Bundle"), | |
* context_definitions = { | |
* "product" = @ContextDefinition("entity:commerce_product", label = @Translation("Product")) | |
* } | |
* ) | |
*/ | |
class ProductType extends ConditionPluginBase implements ContainerFactoryPluginInterface { | |
/** | |
* The entity storage. | |
* | |
* @var \Drupal\Core\Entity\EntityStorageInterface | |
*/ | |
protected $entityStorage; | |
/** | |
* Creates a new ProductType instance. | |
* | |
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage | |
* The entity storage. | |
* @param array $configuration | |
* The plugin configuration, i.e. an array with configuration values keyed | |
* by configuration option name. The special key 'context' may be used to | |
* initialize the defined contexts by setting it to an array of context | |
* values keyed by context names. | |
* @param string $plugin_id | |
* The plugin_id for the plugin instance. | |
* @param mixed $plugin_definition | |
* The plugin implementation definition. | |
*/ | |
public function __construct(EntityStorageInterface $entity_storage, array $configuration, $plugin_id, $plugin_definition) { | |
parent::__construct($configuration, $plugin_id, $plugin_definition); | |
$this->entityStorage = $entity_storage; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
return new static( | |
$container->get('entity_type.manager')->getStorage('commerce_product_type'), | |
$configuration, | |
$plugin_id, | |
$plugin_definition | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | |
$options = []; | |
$bundles = $this->entityStorage->loadMultiple(); | |
foreach ($bundles as $type) { | |
$options[$type->id()] = $type->label(); | |
} | |
$form['bundles'] = [ | |
'#title' => $this->t('Product types'), | |
'#type' => 'checkboxes', | |
'#options' => $options, | |
'#default_value' => $this->configuration['bundles'], | |
]; | |
return parent::buildConfigurationForm($form, $form_state); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { | |
$this->configuration['bundles'] = array_filter($form_state->getValue('bundles')); | |
parent::submitConfigurationForm($form, $form_state); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function summary() { | |
if (count($this->configuration['bundles']) > 1) { | |
$bundles = $this->configuration['bundles']; | |
$last = array_pop($bundles); | |
$bundles = implode(', ', $bundles); | |
return $this->t('The product bundle is @bundles or @last', ['@bundles' => $bundles, '@last' => $last]); | |
} | |
$bundle = reset($this->configuration['bundles']); | |
return $this->t('The product bundle is @bundle', ['@bundle' => $bundle]); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function evaluate() { | |
if (empty($this->configuration['bundles']) && !$this->isNegated()) { | |
return TRUE; | |
} | |
$product = $this->getContextValue('product'); | |
return !empty($this->configuration['bundles'][$product->bundle()]); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function defaultConfiguration() { | |
return ['bundles' => []] + parent::defaultConfiguration(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment