class StrawSelection in Super Term Reference Autocomplete Widget 8
Provides specific access control for the taxonomy_term entity type.
Plugin annotation
@EntityReferenceSelection(
id = "straw",
label = @Translation("Straw selection"),
entity_types = {"taxonomy_term"},
group = "straw",
weight = 1
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase implements ConfigurableInterface, ConfigurablePluginInterface, DependentPluginInterface, SelectionInterface
- class \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection implements SelectionWithAutocreateInterface, ContainerFactoryPluginInterface uses DeprecatedServicePropertyTrait
- class \Drupal\taxonomy\Plugin\EntityReferenceSelection\TermSelection
- class \Drupal\straw\Plugin\EntityReferenceSelection\StrawSelection
- class \Drupal\taxonomy\Plugin\EntityReferenceSelection\TermSelection
- class \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection implements SelectionWithAutocreateInterface, ContainerFactoryPluginInterface uses DeprecatedServicePropertyTrait
- class \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase implements ConfigurableInterface, ConfigurablePluginInterface, DependentPluginInterface, SelectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of StrawSelection
File
- src/
Plugin/ EntityReferenceSelection/ StrawSelection.php, line 22
Namespace
Drupal\straw\Plugin\EntityReferenceSelectionView source
class StrawSelection extends TermSelection {
/**
* {@inheritdoc}
*/
public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
$options = [];
$bundles = $this->entityTypeBundleInfo
->getBundleInfo('taxonomy_term');
$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 = Vocabulary::load($bundle)) {
foreach ($this
->getSelectableTerms($vocabulary
->id()) as $term) {
$referenceable = TRUE;
if ($match && $match_operator == "CONTAINS") {
$referenceable = stripos($term['tree_path'], $match) !== FALSE;
}
if ($match && $match_operator == "STARTS_WITH") {
$referenceable = stripos($term['tree_path'], $match) === 0;
}
if ($match && $match_operator == "=") {
$referenceable = $term['tree_path'] == $match;
}
if ($referenceable) {
$options[$vocabulary
->id()][$term['tid']] = Html::escape($term['tree_path']);
if ($limit > 0 && count($options[$vocabulary
->id()]) == $limit) {
break;
}
}
}
}
}
return $options;
}
/**
* Gets the potentially selectable terms for a given bundle.
*
* @param string $bundle_name
* Vocabulary ID to retrieve terms for.
*
* @return array
* The searchable data.
*/
private function getSelectableTerms($bundle_name) {
$cache_context_keys = \Drupal::service('cache_contexts_manager')
->convertTokensToKeys([
'user.permissions',
])
->getKeys();
$cid = $bundle_name . ':' . implode(':', $cache_context_keys);
$straw_cache = \Drupal::cache('straw');
// Load from cache if possible rather than rebuilding the term list.
if ($cached_data = $straw_cache
->get($cid)) {
return $cached_data->data;
}
$all_terms = $this->entityTypeManager
->getStorage('taxonomy_term')
->loadTree($bundle_name);
// We want $terms to be keyed by ID rather than numerically.
$all_terms = array_reduce($all_terms, function ($carry, $item) {
$carry[$item->tid] = $item;
return $carry;
}, []);
$searchable_data = [];
foreach ($all_terms as $term) {
// Build the tree path for the term, including the names of its
// ancestors. Currently, a single term being in multiple places in the
// hierarchy is not actively supported (only one possible tree path can
// get shown in the autocomplete results)
$tree_path = $term->name;
$current = $term;
while (($parent_id = $current->parents[0]) && ($parent = $all_terms[$parent_id])) {
$tree_path = $parent->name . ' >> ' . $tree_path;
$current = $parent;
}
$searchable_data[] = [
'tid' => $term->tid,
'tree_path' => $tree_path,
];
}
// Save into cache for faster loading in the future.
\Drupal::cache('straw')
->set($cid, $searchable_data, Cache::PERMANENT, [
'taxonomy_term_list',
]);
return $searchable_data;
}
/**
* {@inheritdoc}
*/
public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
// Straw only works with terms; $entity_type_id should always be
// "taxonomy_term". Since we need term-specific handling here, we ignore
// that setting (as well as $uid, since terms don't implement
// EntityOwnerInterface).
$term_names = explode('>>', $label);
/** @var \Drupal\straw\NewTermStorage $new_term_storage */
$new_term_storage = \Drupal::service('straw.new_term_storage');
// Tracks the deepest term we've already processed.
$last_term = NULL;
// Loop to find the term deepest in the existing hierarchy which matches
// the desired tree path.
$tree_path = '';
while ($term_name = array_shift($term_names)) {
$tree_path .= ($tree_path ? ' >> ' : '') . trim($term_name);
$matching_terms_by_vocabulary = $this
->getReferenceableEntities($tree_path, '=', 1);
if (empty($matching_terms_by_vocabulary[$bundle])) {
// We'll process the unmatched term again, below, to create it.
array_unshift($term_names, $term_name);
break;
}
$last_term = key($matching_terms_by_vocabulary[$bundle]);
}
// Create terms of the tree path which have not been found (meaning that
// they don't exist). There *should* always be at least one of these if
// this function is getting called, though it should still function if there
// isn't. The NewTermStorage service is used to track which terms have
// already been created but which have not yet necessarily been saved, to
// prevent creating duplicate terms if the same new term is named by
// multiple term hierarchies being created during the same request.
while ($term_name = array_shift($term_names)) {
$tree_path .= ($tree_path ? ' >> ' : '') . trim($term_name);
if ($found_term = $new_term_storage
->get($bundle, $tree_path)) {
$last_term = $found_term;
}
else {
$last_term = Term::create([
'vid' => $bundle,
'name' => trim($term_name),
'parent' => $last_term,
]);
$new_term_storage
->set($bundle, $tree_path, $last_term);
}
}
return $last_term;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DefaultSelection:: |
protected | property | The current user. | |
DefaultSelection:: |
protected | property | ||
DefaultSelection:: |
protected | property | The entity field manager service. | |
DefaultSelection:: |
protected | property | The entity repository. | |
DefaultSelection:: |
public | property | Entity type bundle info service. | |
DefaultSelection:: |
protected | property | The entity type manager service. | |
DefaultSelection:: |
protected | property | The module handler service. | |
DefaultSelection:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
2 |
DefaultSelection:: |
public static | function | Form element validation handler; Filters the #value property of an element. | |
DefaultSelection:: |
protected | function | Helper method: Passes a query to the alteration system again. | |
DefaultSelection:: |
public | function |
Form validation handler. Overrides SelectionPluginBase:: |
|
DefaultSelection:: |
public | function |
Validates which existing entities can be referenced. Overrides SelectionInterface:: |
|
DefaultSelection:: |
public | function |
Constructs a new DefaultSelection object. Overrides SelectionPluginBase:: |
1 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DeprecatedServicePropertyTrait:: |
public | function | Allows to access deprecated/removed properties. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
SelectionPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
SelectionPluginBase:: |
protected | function | Ensures a backward compatibility level configuration. | |
SelectionPluginBase:: |
public | function |
Allows the selection to alter the SelectQuery generated by EntityFieldQuery. Overrides SelectionInterface:: |
2 |
SelectionPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
SelectionPluginBase:: |
protected | function | Moves the backward compatibility level configurations in the right place. | |
SelectionPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
SelectionPluginBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
StrawSelection:: |
public | function |
Creates a new entity object that can be used as a valid reference. Overrides TermSelection:: |
|
StrawSelection:: |
public | function |
Gets the list of referenceable entities. Overrides TermSelection:: |
|
StrawSelection:: |
private | function | Gets the potentially selectable terms for a given bundle. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TermSelection:: |
public | function |
Form constructor. Overrides DefaultSelection:: |
|
TermSelection:: |
protected | function |
Builds an EntityQuery to get referenceable entities. Overrides DefaultSelection:: |
|
TermSelection:: |
public | function |
Counts entities that are referenceable. Overrides DefaultSelection:: |
|
TermSelection:: |
public | function |
Gets default configuration for this plugin. Overrides DefaultSelection:: |
|
TermSelection:: |
public | function |
Validates which newly created entities can be referenced. Overrides DefaultSelection:: |