class EntityMatcher in Linkit 8.4
Same name and namespace in other branches
- 8.5 src/Plugin/Linkit/Matcher/EntityMatcher.php \Drupal\linkit\Plugin\Linkit\Matcher\EntityMatcher
Plugin annotation
@Matcher(
id = "entity",
label = @Translation("Entity"),
deriver = "\Drupal\linkit\Plugin\Derivative\EntityMatcherDeriver"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\linkit\MatcherBase implements ContainerFactoryPluginInterface, MatcherInterface
- class \Drupal\linkit\ConfigurableMatcherBase implements ConfigurableMatcherInterface
- class \Drupal\linkit\Plugin\Linkit\Matcher\EntityMatcher uses MatcherTokensTrait
- class \Drupal\linkit\ConfigurableMatcherBase implements ConfigurableMatcherInterface
- class \Drupal\linkit\MatcherBase implements ContainerFactoryPluginInterface, MatcherInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of EntityMatcher
File
- src/
Plugin/ Linkit/ Matcher/ EntityMatcher.php, line 28 - Contains \Drupal\linkit\Plugin\Linkit\Matcher\EntityMatcher.
Namespace
Drupal\linkit\Plugin\Linkit\MatcherView source
class EntityMatcher extends ConfigurableMatcherBase {
use MatcherTokensTrait;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The target entity type id
*
* @var string
*/
protected $target_type;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, AccountInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
if (empty($plugin_definition['target_entity'])) {
throw new \InvalidArgumentException("Missing required 'target_entity' property for a matcher.");
}
$this->database = $database;
$this->entityManager = $entity_manager;
$this->moduleHandler = $module_handler;
$this->currentUser = $current_user;
$this->target_type = $plugin_definition['target_entity'];
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('database'), $container
->get('entity.manager'), $container
->get('module_handler'), $container
->get('current_user'));
}
/**
* {@inheritdoc}
*/
public function getSummary() {
$summery = parent::getSummary();
$entity_type = $this->entityManager
->getDefinition($this->target_type);
$result_description = $this->configuration['result_description'];
if (!empty($result_description)) {
$summery[] = $this
->t('Result description: @result_description', [
'@result_description' => $result_description,
]);
}
if ($entity_type
->hasKey('bundle')) {
$has_bundle_filter = !empty($this->configuration['bundles']);
$bundles = [];
if ($has_bundle_filter) {
$bundles_info = $this->entityManager
->getBundleInfo($this->target_type);
foreach ($this->configuration['bundles'] as $bundle) {
$bundles[] = $bundles_info[$bundle]['label'];
}
}
$summery[] = $this
->t('Bundle filter: @bundle_filter', [
'@bundle_filter' => $has_bundle_filter ? implode(', ', $bundles) : t('None'),
]);
$summery[] = $this
->t('Group by bundle: @bundle_grouping', [
'@bundle_grouping' => $this->configuration['group_by_bundle'] ? $this
->t('Yes') : $this
->t('No'),
]);
}
return $summery;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'result_description' => '',
'bundles' => [],
'group_by_bundle' => FALSE,
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$entity_type = $this->entityManager
->getDefinition($this->target_type);
$form['result_description'] = [
'#title' => $this
->t('Result description'),
'#type' => 'textfield',
'#default_value' => $this->configuration['result_description'],
'#size' => 120,
'#maxlength' => 255,
'#weight' => -100,
];
$this
->insertTokenList($form, [
$this->target_type,
]);
// Filter the possible bundles to use if the entity has bundles.
if ($entity_type
->hasKey('bundle')) {
$bundle_options = [];
foreach ($this->entityManager
->getBundleInfo($this->target_type) as $bundle_name => $bundle_info) {
$bundle_options[$bundle_name] = $bundle_info['label'];
}
$form['bundles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Restrict to the selected bundles'),
'#options' => $bundle_options,
'#default_value' => $this->configuration['bundles'],
'#description' => $this
->t('If none of the checkboxes is checked, allow all bundles.'),
'#element_validate' => [
[
get_class($this),
'elementValidateFilter',
],
],
'#weight' => -50,
];
// Group the results by bundle.
$form['group_by_bundle'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Group by bundle'),
'#default_value' => $this->configuration['group_by_bundle'],
'#weight' => -50,
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['result_description'] = $form_state
->getValue('result_description');
$this->configuration['bundles'] = $form_state
->getValue('bundles');
$this->configuration['group_by_bundle'] = $form_state
->getValue('group_by_bundle');
}
/**
* Form element validation handler; Filters the #value property of an element.
*/
public static function elementValidateFilter(&$element, FormStateInterface $form_state) {
$element['#value'] = array_filter($element['#value']);
$form_state
->setValueForElement($element, $element['#value']);
}
/**
* {@inheritdoc}
*/
public function getMatches($string) {
$query = $this
->buildEntityQuery($string);
$result = $query
->execute();
if (empty($result)) {
return [];
}
$matches = [];
$entities = $this->entityManager
->getStorage($this->target_type)
->loadMultiple($result);
foreach ($entities as $entity_id => $entity) {
// Check the access against the defined entity access handler.
/** @var \Drupal\Core\Access\AccessResultInterface $access */
$access = $entity
->access('view', $this->currentUser, TRUE);
if (!$access
->isAllowed()) {
continue;
}
$matches[] = [
'title' => $this
->buildLabel($entity),
'description' => $this
->buildDescription($entity),
'path' => $this
->buildPath($entity),
'group' => $this
->buildGroup($entity),
];
}
return $matches;
}
/**
* Builds an EntityQuery to get entities.
*
* @param $match
* Text to match the label against.
*
* @return \Drupal\Core\Entity\Query\QueryInterface
* The EntityQuery object with the basic conditions and sorting applied to
* it.
*/
protected function buildEntityQuery($match) {
$match = $this->database
->escapeLike($match);
$entity_type = $this->entityManager
->getDefinition($this->target_type);
$query = $this->entityManager
->getStorage($this->target_type)
->getQuery();
$label_key = $entity_type
->getKey('label');
if ($label_key) {
$query
->condition($label_key, '%' . $match . '%', 'LIKE');
$query
->sort($label_key, 'ASC');
}
// Bundle check.
if (!empty($this->configuration['bundles']) && ($bundle_key = $entity_type
->getKey('bundle'))) {
$query
->condition($bundle_key, $this->configuration['bundles'], 'IN');
}
// Add tags to let other modules alter the query.
$query
->addTag('linkit_entity_autocomplete');
$query
->addTag('linkit_entity_' . $this->target_type . '_autocomplete');
// Add access tag for the query.
$query
->addTag('entity_access');
$query
->addTag($this->target_type . '_access');
return $query;
}
/**
* Builds the label string used in the match array.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The matched entity.
*
* @return string
* The label for this entity.
*/
protected function buildLabel($entity) {
return Html::escape($entity
->label());
}
/**
* Builds the description string used in the match array.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The matched entity.
*
* @return string
* The description for this entity.
*/
protected function buildDescription($entity) {
$description = \Drupal::token()
->replace($this->configuration['result_description'], [
$this->target_type => $entity,
], []);
return LinkitXss::descriptionFilter($description);
}
/**
* Builds the path string used in the match array.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The matched entity.
*
* @return string
* The URL for this entity.
*/
protected function buildPath($entity) {
return $entity
->toUrl()
->toString();
}
/**
* Builds the group string used in the match array.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The matched entity.
*
* @return string
* The match group for this entity.
*/
protected function buildGroup($entity) {
$group = $entity
->getEntityType()
->getLabel();
// If the entities by this entity should be grouped by bundle, get the
// name and append it to the group.
if ($this->configuration['group_by_bundle']) {
$bundles = $this->entityManager
->getBundleInfo($entity
->getEntityTypeId());
$bundle_label = $bundles[$entity
->bundle()]['label'];
$group .= ' - ' . $bundle_label;
}
return $group;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 | |
EntityMatcher:: |
protected | property | The current user. | |
EntityMatcher:: |
protected | property | The database connection. | |
EntityMatcher:: |
protected | property | The entity manager. | |
EntityMatcher:: |
protected | property | The module handler service. | |
EntityMatcher:: |
protected | property | The target entity type id | |
EntityMatcher:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
4 |
EntityMatcher:: |
protected | function | Builds the description string used in the match array. | 2 |
EntityMatcher:: |
protected | function | Builds an EntityQuery to get entities. | 3 |
EntityMatcher:: |
protected | function | Builds the group string used in the match array. | |
EntityMatcher:: |
protected | function | Builds the label string used in the match array. | |
EntityMatcher:: |
protected | function | Builds the path string used in the match array. | 1 |
EntityMatcher:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
EntityMatcher:: |
public | function |
Gets default configuration for this plugin. Overrides MatcherBase:: |
3 |
EntityMatcher:: |
public static | function | Form element validation handler; Filters the #value property of an element. | |
EntityMatcher:: |
public | function |
Gets an array with search matches that will be presented in the autocomplete
widget. Overrides MatcherInterface:: |
|
EntityMatcher:: |
public | function |
Returns the summarized configuration of the matcher. Overrides MatcherBase:: |
3 |
EntityMatcher:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
3 |
EntityMatcher:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
EntityMatcher:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides MatcherBase:: |
|
MatcherBase:: |
protected | property | The matcher ID. | |
MatcherBase:: |
protected | property | The weight of the matcher compared to others in a matcher collection. | |
MatcherBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
4 |
MatcherBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurablePluginInterface:: |
|
MatcherBase:: |
public | function |
Returns the matcher label. Overrides MatcherInterface:: |
|
MatcherBase:: |
public | function |
Returns the unique ID representing the matcher. Overrides MatcherInterface:: |
|
MatcherBase:: |
public | function |
Returns the weight of the matcher. Overrides MatcherInterface:: |
|
MatcherBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface:: |
|
MatcherBase:: |
public | function |
Sets the weight for the matcher. Overrides MatcherInterface:: |
|
MatcherTokensTrait:: |
public | function | Gets all available tokens. | |
MatcherTokensTrait:: |
public | function | Inserts a form element with a list of available tokens. | |
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. | |
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. |