summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoranon2017-02-28 23:27:17 +0100
committerEmil Stjerneman2017-02-28 23:27:17 +0100
commitf6a245360bbb74c2709ee2e2a5977859c5075af4 (patch)
treec6978ef208d69fbc21414dad1c041e5375fe7d5d
parenta59f1fbe664ed06bbdc71062d21826d90c6f4101 (diff)
Issue #2844788 by anon: Add a contact form matcher
-rw-r--r--src/Plugin/Linkit/Matcher/ContactFormMatcher.php38
-rw-r--r--src/Plugin/Linkit/Matcher/EntityMatcher.php11
-rw-r--r--tests/src/FunctionalJavascript/LinkitDialogTest.php11
-rw-r--r--tests/src/Kernel/Matchers/ContactFormMatcherTest.php53
4 files changed, 109 insertions, 4 deletions
diff --git a/src/Plugin/Linkit/Matcher/ContactFormMatcher.php b/src/Plugin/Linkit/Matcher/ContactFormMatcher.php
new file mode 100644
index 0000000..e9326b9
--- /dev/null
+++ b/src/Plugin/Linkit/Matcher/ContactFormMatcher.php
@@ -0,0 +1,38 @@
+<?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;
+ }
+
+}
diff --git a/src/Plugin/Linkit/Matcher/EntityMatcher.php b/src/Plugin/Linkit/Matcher/EntityMatcher.php
index 5dc3420..70adf3e 100644
--- a/src/Plugin/Linkit/Matcher/EntityMatcher.php
+++ b/src/Plugin/Linkit/Matcher/EntityMatcher.php
@@ -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');
}
diff --git a/tests/src/FunctionalJavascript/LinkitDialogTest.php b/tests/src/FunctionalJavascript/LinkitDialogTest.php
index 002000f..3f2cafe 100644
--- a/tests/src/FunctionalJavascript/LinkitDialogTest.php
+++ b/tests/src/FunctionalJavascript/LinkitDialogTest.php
@@ -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();
diff --git a/tests/src/Kernel/Matchers/ContactFormMatcherTest.php b/tests/src/Kernel/Matchers/ContactFormMatcherTest.php
new file mode 100644
index 0000000..a058272
--- /dev/null
+++ b/tests/src/Kernel/Matchers/ContactFormMatcherTest.php
@@ -0,0 +1,53 @@
+<?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');
+ }
+
+}