TaxonomyHierarchySelection.php in Workbench Access 8
File
src/Plugin/EntityReferenceSelection/TaxonomyHierarchySelection.php
View source
<?php
namespace Drupal\workbench_access\Plugin\EntityReferenceSelection;
use Drupal\Component\Utility\Html;
use Drupal\Core\Session\AccountInterface;
use Drupal\taxonomy\Plugin\EntityReferenceSelection\TermSelection;
use Drupal\workbench_access\Entity\AccessSchemeInterface;
use Drupal\workbench_access\UserSectionStorageInterface;
use Drupal\workbench_access\WorkbenchAccessManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TaxonomyHierarchySelection extends TermSelection {
protected $scheme;
protected $currentUser;
protected $userSectionStorage;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
return $instance
->setScheme($container
->get('entity_type.manager')
->getStorage('access_scheme')
->load($plugin_definition['scheme']))
->setCurrentUser($container
->get('current_user'))
->setUserSectionStorage($container
->get('workbench_access.user_section_storage'));
}
public function setCurrentUser(AccountInterface $currentUser) {
$this->currentUser = $currentUser;
return $this;
}
public function setUserSectionStorage(UserSectionStorageInterface $userSectionStorage) {
$this->userSectionStorage = $userSectionStorage;
return $this;
}
public function setScheme(AccessSchemeInterface $scheme) {
$this->scheme = $scheme;
return $this;
}
public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
if ($match || $limit) {
$options = parent::getReferenceableEntities($match, $match_operator, $limit);
}
else {
$options = [];
$bundles = $this->entityTypeBundleInfo
->getBundleInfo('taxonomy_term');
if (isset($this->configuration['handler_settings'])) {
$handler_settings = $this->configuration['handler_settings'];
}
$bundle_names = !empty($handler_settings['target_bundles']) ? $handler_settings['target_bundles'] : array_keys($bundles);
foreach ($bundle_names as $bundle) {
if ($vocabulary = $this->entityTypeManager
->getStorage('taxonomy_vocabulary')
->load($bundle)) {
if ($terms = $this->entityTypeManager
->getStorage('taxonomy_term')
->loadTree($vocabulary
->id(), 0, NULL, TRUE)) {
foreach ($terms as $term) {
$options[$vocabulary
->id()][$term
->id()] = str_repeat('-', $term->depth) . Html::escape($this->entityRepository
->getTranslationFromContext($term)
->label());
}
}
}
}
}
if ($this->currentUser
->hasPermission('bypass workbench access')) {
return $options;
}
$user_sections = $this->userSectionStorage
->getUserSections($this->scheme);
foreach ($options as $key => $values) {
if (WorkbenchAccessManager::checkTree($this->scheme, [
$key,
], $user_sections)) {
continue;
}
else {
foreach ($values as $id => $value) {
if (!WorkbenchAccessManager::checkTree($this->scheme, [
$id,
], $user_sections)) {
unset($options[$key][$id]);
}
}
}
}
return $options;
}
public function validateReferenceableEntities(array $ids) {
$valid = [];
if ($allowed = $this
->getReferenceableEntities()) {
foreach ($ids as $id) {
foreach ($allowed as $parent => $child) {
if (isset($child[$id])) {
$valid[$id] = $id;
break;
}
}
}
}
return $valid;
}
}