Commit f6a24536 authored by anon's avatar anon Committed by anon

Issue #2844788 by anon: Add a contact form matcher

parent a59f1fbe
<?php
namespace Drupal\linkit\Plugin\Linkit\Matcher;
/**
* Provides specific linkit matchers for contact forms.
*
* @Matcher(
* id = "entity:contact_form",
* label = @Translation("Contact form"),
* target_entity = "contact_form",
* provider = "contact"
* )
*/
class ContactFormMatcher extends EntityMatcher {
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return parent::calculateDependencies() + [
'module' => ['contact'],
];
}
/**
* {@inheritdoc}
*/
protected function buildEntityQuery($search_string) {
$query = parent::buildEntityQuery($search_string);
// Remove the personal contact form from the suggestion list.
$query->condition('id', 'personal', '<>');
return $query;
}
}
......@@ -3,6 +3,7 @@
namespace Drupal\linkit\Plugin\Linkit\Matcher;
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
......@@ -336,7 +337,15 @@ class EntityMatcher extends ConfigurableMatcherBase {
$label_key = $entity_type->getKey('label');
if ($label_key) {
$query->condition($label_key, '%' . $search_string . '%', 'LIKE');
// For configuration entities, the condition needs to be CONTAINS as
// the matcher does not support LIKE.
if ($entity_type instanceof ConfigEntityTypeInterface) {
$query->condition($label_key, $search_string, 'CONTAINS');
}
else {
$query->condition($label_key, '%' . $search_string . '%', 'LIKE');
}
$query->sort($label_key, 'ASC');
}
......
......@@ -27,7 +27,14 @@ class LinkitDialogTest extends JavascriptTestBase {
*
* @var array
*/
public static $modules = ['node', 'ckeditor', 'filter', 'linkit', 'entity_test', 'language'];
public static $modules = [
'node',
'ckeditor',
'filter',
'linkit',
'entity_test',
'language',
];
/**
* An instance of the "CKEditor" text editor plugin.
......@@ -234,8 +241,6 @@ class LinkitDialogTest extends JavascriptTestBase {
JS;
$session->executeScript($javascript);
// Test edit link in the editor.
// Click on the drupallink plugin.
$page->find('css', 'a.cke_button__drupallink')->click();
......
<?php
namespace Drupal\Tests\linkit\Kernel\Matchers;
use Drupal\contact\Entity\ContactForm;
use Drupal\Tests\linkit\Kernel\LinkitKernelTestBase;
/**
* Tests contact form matcher.
*
* @group linkit
*/
class ContactFormMatcherTest extends LinkitKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['contact'];
/**
* The matcher manager.
*
* @var \Drupal\linkit\MatcherManager
*/
protected $manager;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->manager = $this->container->get('plugin.manager.linkit.matcher');
ContactForm::create([
'id' => 'lorem',
'label' => 'Lorem',
])->save();
}
/**
* Tests contact form matcher.
*/
public function testMatcherWidthDefaultConfiguration() {
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->manager->createInstance('entity:contact_form', []);
$suggestions = $plugin->execute('Lorem');
$this->assertEquals(1, count($suggestions->getSuggestions()), 'Correct number of suggestions');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment