Store.php in Commerce Core 8.2
File
modules/store/src/Plugin/views/field/Store.php
View source
<?php
namespace Drupal\commerce_store\Plugin\views\field;
use Drupal\views\Plugin\views\field\EntityField;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
class Store extends EntityField {
protected function defineOptions() {
$options = parent::defineOptions();
$options['hide_single_store'] = [
'default' => TRUE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['hide_single_store'] = [
'#type' => 'checkbox',
'#title' => $this
->t("Hide if there's only one store."),
'#default_value' => $this->options['hide_single_store'],
];
}
public function access(AccountInterface $account) {
$store_query = $this->entityTypeManager
->getStorage('commerce_store')
->getQuery();
$store_count = $store_query
->count()
->accessCheck(TRUE)
->execute();
if ($this->options['hide_single_store'] && $store_count <= 1) {
return FALSE;
}
return parent::access($account);
}
}