- last update
over 1 year ago 83 pass - πΊπΈUnited States jacobbell84
I had need of this functionality so I put together a patch. Because there's a lot of possible use cases, more then can be handled by a form field, I introduced hooks so that the developer can add support for whatever field they're looking for. One allows for the field to be searched on, the other allows you override the label that gets returned.
- π§π·Brazil igorgoncalves
Ok, i faced a similar situation like many others here, so i will share the step-by-step i made to achieve the solution.
1 - We have a product content type with a product_id field. So the editor wants to search not only by the product title, but by the product id as well.
2 - On yours custom module you will need to create a new Linkit Matcher.
3 - so, o my 'custom_module' i created:
docroot/modules/custom/custom_module/src/Plugin/Linkit/Matcher/NodeProductsMatcher.php
4 - and the code below:
<?php namespace Drupal\custom_module\Plugin\Linkit\Matcher; use Drupal\linkit\Plugin\Linkit\Matcher\NodeMatcher; // Those anotations below it is VERY important to follow the patterns from NodeMatcher parent. /** * Provide specific linkit matcher for products CT. * * @Matcher( * id = "custom_module:node:products", * label = @Translation("Custom Matcher Products"), * target_entity = "node", * provider = "node" * ) */ class NodeProductsMatcher <strong>extends NodeMatcher</strong> { /** * {@inheritdoc} */ protected function buildEntityQuery($search_string) { $search_string = $this->database->escapeLike($search_string); $entity_type = $this->entityTypeManager->getDefinition($this->targetType); $nodeQuery = $this->entityTypeManager->getStorage('node')->getQuery(); $nodeQuery->condition('field_product_id', '%' . $search_string . '%', 'LIKE'); // Bundle check. if (!empty($this->configuration['bundles']) && $bundle_key = $entity_type->getKey('bundle')) { $nodeQuery->condition($bundle_key, $this->configuration['bundles'], 'IN'); } if ($this->configuration['limit']) { $nodeQuery->range(0, $this->configuration['limit']); } $this->addQueryTags($nodeQuery); return $nodeQuery; } }
5 - Save, clear your cache, and go back to UI and add your new Custom Matcher at: admin/config/content/linkit/manage/[Your-profile-default]/matchers
6 - Select the content which have your product_id field, and save.
7 - long story short here, the new NodeProductsMatcher will be called and the buildEntityQuery function there will look for field_product_id, instead of the $node->label() as the default one. The bundle check and the config['limit'] will heritage what you've selected on step 6.
8 - thats it, you now have your suggestions updated.