Commit 3b8e2a33 authored by anon's avatar anon

by anon: Redesigned the matcher settings and refactored the update tests.

parent c689cd00
langcode: en
status: true
dependencies:
module:
- node
id: default
label: Default
description: 'A default Linkit profile'
matchers:
556010a3-e317-48b3-b4ed-854c10f4b950:
uuid: 556010a3-e317-48b3-b4ed-854c10f4b950
id: 'entity:node'
weight: 0
settings:
result_description: 'by [node:author] | [node:created:medium]'
bundles: { }
group_by_bundle: false
include_unpublished: false
substitution_type: canonical
langcode: en
status: true
dependencies:
module:
- node
id: default
label: Default
description: 'A default Linkit profile'
matchers:
556010a3-e317-48b3-b4ed-854c10f4b950:
uuid: 556010a3-e317-48b3-b4ed-854c10f4b950
id: 'entity:node'
weight: 0
settings:
metadata: 'by [node:author] | [node:created:medium]'
bundles: { }
group_by_bundle: false
include_unpublished: false
substitution_type: canonical
......@@ -34,7 +34,7 @@ linkit.matcher.*:
linkit.matcher.entity:
type: mapping
mapping:
result_description:
metadata:
type: string
bundles:
type: sequence
......
......@@ -153,3 +153,21 @@ function linkit_update_8501() {
$profile->save(TRUE);
}
}
/**
* Rename profile property 'result_description' to 'metadata'.
*/
function linkit_update_8502() {
$config_factory = \Drupal::configFactory();
foreach ($config_factory->listAll('linkit.linkit_profile.') as $id) {
$profile = $config_factory->getEditable($id);
foreach ($profile->get('matchers') as $key => $matcher) {
$old_value = $profile->get('matchers.' . $key . '.settings.result_description');
$profile->set('matchers.' . $key . '.settings.metadata', $old_value);
$profile->clear('matchers.' . $key . '.settings.result_description');
}
$profile->save(TRUE);
}
}
......@@ -18,11 +18,11 @@ trait MatcherTokensTrait {
public function insertTokenList(&$form, array $types = []) {
if (\Drupal::moduleHandler()->moduleExists('token')) {
// Add the token tree UI.
$form['token_tree'] = [
$form['metadata']['token_tree'] = [
'#theme' => 'token_tree_link',
'#token_types' => $types,
'#dialog' => TRUE,
'#weight' => -90,
'#weight' => 10,
];
}
else {
......@@ -35,13 +35,13 @@ trait MatcherTokensTrait {
}
if (count($token_items)) {
$form['tokens'] = [
$form['metadata']['tokens'] = [
'#type' => 'details',
'#title' => t('Available tokens'),
'#weight' => -90,
'#weight' => 10,
];
$form['tokens']['list'] = [
$form['metadata']['tokens']['list'] = [
'#theme' => 'item_list',
'#items' => $token_items,
];
......
......@@ -133,10 +133,10 @@ class EntityMatcher extends ConfigurableMatcherBase {
$summery = parent::getSummary();
$entity_type = $this->entityTypeManager->getDefinition($this->targetType);
$result_description = $this->configuration['result_description'];
if (!empty($result_description)) {
$summery[] = $this->t('Result description: @result_description', [
'@result_description' => $result_description,
$metadata = $this->configuration['metadata'];
if (!empty($metadata)) {
$summery[] = $this->t('Metadata: @metadata', [
'@metadata' => $metadata,
]);
}
......@@ -168,7 +168,7 @@ class EntityMatcher extends ConfigurableMatcherBase {
*/
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'result_description' => '',
'metadata' => '',
'bundles' => [],
'group_by_bundle' => FALSE,
'substitution_type' => SubstitutionManagerInterface::DEFAULT_SUBSTITUTION,
......@@ -180,13 +180,22 @@ class EntityMatcher extends ConfigurableMatcherBase {
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$entity_type = $this->entityTypeManager->getDefinition($this->targetType);
$form['result_description'] = [
'#title' => $this->t('Result description'),
$form['metadata'] = array(
'#type' => 'details',
'#title' => t('Suggestion metadata'),
'#open' => TRUE,
'#weight' => -100,
);
$form['metadata']['metadata'] = [
'#title' => $this->t('Metadata'),
'#type' => 'textfield',
'#default_value' => $this->configuration['result_description'],
'#default_value' => $this->configuration['metadata'],
'#description' => $this->t('Metadata is shown together with each suggestion in the suggestion list.'),
'#size' => 120,
'#maxlength' => 255,
'#weight' => -100,
'#weight' => 0,
];
$this->insertTokenList($form, [$this->targetType]);
......@@ -198,34 +207,51 @@ class EntityMatcher extends ConfigurableMatcherBase {
$bundle_options[$bundle_name] = $bundle_info['label'];
}
$form['bundles'] = [
$form['bundle_restrictions'] = array(
'#type' => 'details',
'#title' => t('Bundle restrictions'),
'#open' => TRUE,
'#weight' => -90,
);
$form['bundle_restrictions']['bundles'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Restrict to the selected bundles'),
'#title' => $this->t('Restrict suggestions 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.'),
'#description' => $this->t('If none of the checkboxes is checked, all bundles are allowed.'),
'#element_validate' => [[get_class($this), 'elementValidateFilter']],
'#weight' => -50,
];
// Group the results by bundle.
$form['group_by_bundle'] = [
$form['bundle_grouping'] = array(
'#type' => 'details',
'#title' => t('Bundle grouping'),
'#open' => TRUE,
);
// Group the suggestions by bundle.
$form['bundle_grouping']['group_by_bundle'] = [
'#type' => 'checkbox',
'#title' => $this->t('Group by bundle'),
'#default_value' => $this->configuration['group_by_bundle'],
'#weight' => -50,
'#description' => $this->t('Group suggestions by their bundle.'),
];
}
$substitution_options = $this->substitutionManager->getApplicablePluginsOptionList($this->targetType);
$form['substitution_type'] = [
$form['substitution'] = array(
'#type' => 'details',
'#title' => t('URL substitution'),
'#open' => TRUE,
'#weight' => 100,
'#access' => count($substitution_options) !== 1,
);
$form['substitution']['substitution_type'] = [
'#title' => $this->t('Substitution Type'),
'#type' => 'select',
'#default_value' => $this->configuration['substitution_type'],
'#options' => $substitution_options,
'#description' => $this->t('Configure how the selected entity should be transformed into a URL for insertion.'),
'#weight' => -49,
'#access' => count($substitution_options) !== 1,
];
return $form;
......@@ -241,7 +267,7 @@ class EntityMatcher extends ConfigurableMatcherBase {
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['result_description'] = $form_state->getValue('result_description');
$this->configuration['metadata'] = $form_state->getValue('metadata');
$this->configuration['bundles'] = $form_state->getValue('bundles');
$this->configuration['group_by_bundle'] = $form_state->getValue('group_by_bundle');
$this->configuration['substitution_type'] = $form_state->getValue('substitution_type');
......@@ -343,16 +369,16 @@ class EntityMatcher extends ConfigurableMatcherBase {
}
/**
* Builds the description string used in the match array.
* Builds the metadata string used in the match array.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The matched entity.
*
* @return string
* The description for this entity.
* The metadata for this entity.
*/
protected function buildDescription(EntityInterface $entity) {
$description = \Drupal::token()->replace($this->configuration['result_description'], [$this->targetType => $entity], []);
$description = \Drupal::token()->replace($this->configuration['metadata'], [$this->targetType => $entity], []);
return LinkitXss::descriptionFilter($description);
}
......
......@@ -88,8 +88,15 @@ class FileMatcher extends EntityMatcher {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['extensions'] = array(
'#type' => 'details',
'#title' => t('File extensions'),
'#open' => TRUE,
'#weight' => -100,
);
$file_extensions = str_replace(' ', ', ', $this->configuration['file_extensions']);
$form['file_extensions'] = [
$form['extensions']['file_extensions'] = [
'#type' => 'textfield',
'#title' => $this->t('Allowed file extensions'),
'#default_value' => $file_extensions,
......@@ -102,6 +109,7 @@ class FileMatcher extends EntityMatcher {
'#type' => 'details',
'#title' => t('Image file settings'),
'#description' => t('Extra settings for image files in the result.'),
'#open' => TRUE,
'#tree' => TRUE,
];
......
......@@ -53,11 +53,17 @@ class NodeMatcher extends EntityMatcher {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['include_unpublished'] = [
$form['unpublished_nodes'] = array(
'#type' => 'details',
'#title' => t('Unpublished nodes'),
'#open' => TRUE,
);
$form['unpublished_nodes']['include_unpublished'] = [
'#title' => t('Include unpublished nodes'),
'#type' => 'checkbox',
'#default_value' => $this->configuration['include_unpublished'],
'#description' => t('In order to see unpublished nodes, the requesting user must also have permissions to do so.'),
'#description' => t('In order to see unpublished nodes, users must also have permissions to do so.'),
];
return $form;
......
......@@ -40,7 +40,7 @@ class TermMatcher extends EntityMatcher {
* {@inheritdoc}
*/
protected function buildDescription(EntityInterface $entity) {
$description = \Drupal::token()->replace($this->configuration['result_description'], ['term' => $entity], []);
$description = \Drupal::token()->replace($this->configuration['metadata'], ['term' => $entity], []);
return LinkitXss::descriptionFilter($description);
}
......
......@@ -60,20 +60,33 @@ class UserMatcher extends EntityMatcher {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['roles'] = [
$form['role_restrictions'] = array(
'#type' => 'details',
'#title' => t('Role restrictions'),
'#open' => TRUE,
'#weight' => -90,
);
$form['role_restrictions']['roles'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Restrict to the selected roles'),
'#options' => array_diff_key(user_role_names(TRUE), [RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID]),
'#default_value' => $this->configuration['roles'],
'#description' => $this->t('If none of the checkboxes is checked, allow all roles.'),
'#description' => $this->t('If none of the checkboxes is checked, all roles are allowed.'),
'#element_validate' => [[get_class($this), 'elementValidateFilter']],
];
$form['include_blocked'] = [
$form['blocked_users'] = array(
'#type' => 'details',
'#title' => t('Blocked users'),
'#open' => TRUE,
);
$form['blocked_users']['include_blocked'] = [
'#title' => t('Include blocked user'),
'#type' => 'checkbox',
'#default_value' => $this->configuration['include_blocked'],
'#description' => t('In order to see blocked users, the requesting user must also have permissions to do so.'),
'#description' => t('In order to see blocked users, users must have permissions to do so.'),
];
return $form;
......
<?php
namespace Drupal\linkit\Tests\Update;
use Drupal\filter\Entity\FilterFormat;
use Drupal\system\Tests\Update\UpdatePathTestBase;
/**
* Tests Linkit upgrade path for update 8501.
*
* @group Update
*/
class LinkitUpdate8501 extends UpdatePathTestBase {
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->configFactory = $this->container->get('config.factory');
}
/**
* Set database dump files to be used.
*/
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../tests/fixtures/update/linkit-4-to-5/drupal-8.linkit-enabled.standard.php.gz',
__DIR__ . '/../../../tests/fixtures/update/8501/linkit-update-8501.php',
];
}
/**
* Tests linkit_update_8501().
*
* @see linkit_update_8501()
*/
public function testLinkitUpdate8501() {
$this->runUpdates();
$test_profile = $this->configFactory->get('linkit.linkit_profile.test_profile');
$this->assertEqual('canonical', $test_profile->get('matchers.fc48c807-2a9c-44eb-b86b-7e134c1aa252.settings.substitution_type'), 'Content matcher has a substitution type of canonical.');
$this->assertEqual('file', $test_profile->get('matchers.b8d6d672-6377-493f-b492-3cc69511cf17.settings.substitution_type'), 'File matcher has a substitution type of file.');
$htmlRestrictions = FilterFormat::load('format_1')->getHtmlRestrictions();
$this->assertTrue(array_key_exists("data-entity-type", $htmlRestrictions['allowed']['a']));
$this->assertTrue(array_key_exists("data-entity-uuid", $htmlRestrictions['allowed']['a']));
$this->assertTrue(array_key_exists("data-entity-substitution", $htmlRestrictions['allowed']['a']));
}
}
......@@ -32,8 +32,8 @@ class LinkitUpdateTest extends UpdatePathTestBase {
*/
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../tests/fixtures/update/linkit-4-to-5/drupal-8.linkit-enabled.standard.php.gz',
__DIR__ . '/../../../tests/fixtures/update/linkit-4-to-5/linkit-4-to-5.php',
__DIR__ . '/../../../tests/fixtures/update/drupal-8.linkit-enabled.standard.php.gz',
__DIR__ . '/../../../tests/fixtures/update/linkit-additions.php',
];
}
......@@ -98,4 +98,38 @@ class LinkitUpdateTest extends UpdatePathTestBase {
$this->assertTrue(array_key_exists("data-entity-uuid", $htmlRestrictions['allowed']['a']));
}
/**
* Tests linkit_update_8501().
*
* @see linkit_update_8501()
*/
public function testLinkitUpdate8501() {
$this->runUpdates();
$test_profile = $this->configFactory->get('linkit.linkit_profile.test_profile');
$this->assertEqual('canonical', $test_profile->get('matchers.fc48c807-2a9c-44eb-b86b-7e134c1aa252.settings.substitution_type'), 'Content matcher has a substitution type of canonical.');
$this->assertEqual('file', $test_profile->get('matchers.b8d6d672-6377-493f-b492-3cc69511cf17.settings.substitution_type'), 'File matcher has a substitution type of file.');
$htmlRestrictions = FilterFormat::load('format_1')->getHtmlRestrictions();
$this->assertTrue(array_key_exists("data-entity-type", $htmlRestrictions['allowed']['a']));
$this->assertTrue(array_key_exists("data-entity-uuid", $htmlRestrictions['allowed']['a']));
$this->assertTrue(array_key_exists("data-entity-substitution", $htmlRestrictions['allowed']['a']));
}
/**
* Tests linkit_update_8502().
*
* @see linkit_update_8502()
*/
public function testLinkitUpdate8502() {
$test_profile = $this->configFactory->get('linkit.linkit_profile.test_profile');
$this->assertNotNull($test_profile->get('matchers.fc48c807-2a9c-44eb-b86b-7e134c1aa252.settings.result_description'), 'Profile have result_description');
$this->runUpdates();
$test_profile = $this->configFactory->get('linkit.linkit_profile.test_profile');
$this->assertNull($test_profile->get('matchers.fc48c807-2a9c-44eb-b86b-7e134c1aa252.settings.result_description'), 'Profile does not have result_description');
$this->assertNotNull($test_profile->get('matchers.fc48c807-2a9c-44eb-b86b-7e134c1aa252.settings.metadata'), 'Profile have metadata');
}
}
uuid: 5600b759-8f91-49fa-88f0-a69f4c8ed440
langcode: en
status: true
dependencies:
config:
- filter.format.format_1
module:
- ckeditor
format: format_1
editor: ckeditor
settings:
toolbar:
rows:
-
-
name: Links
items:
- Linkit
plugins:
stylescombo:
styles: ''
language:
language_list: un
linkit:
linkit_profile: test_profile
uuid: ff6b71a0-5051-4857-a0a8-28a670b7317d
langcode: en
status: true
dependencies: { }
name: 'Format 1'
format: format_1
weight: 0
filters:
filter_html:
id: filter_html
provider: filter
status: true
weight: -10
settings:
allowed_html: '<a href hreflang data-entity-type data-entity-uuid> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type=''1 A I''> <li> <dl> <dt> <dd> <h2 id=''jump-*''> <h3 id> <h4 id> <h5 id> <h6 id>'
filter_html_help: true
filter_html_nofollow: false
<?php
/**
* @file
* Database fixture for testing the upgrade path for Linkit 4 to 5.
*
* Contains database additions to drupal-8.bare.standard.php.gz for testing the
* upgrade path for Linkit 4 to 5.
*/
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Database\Database;
$connection = Database::getConnection();
// Configuration for linkit profiles.
$config = Yaml::decode(file_get_contents(__DIR__ . '/linkit.linkit_profile.test_profile.yml'));
$connection->insert('config')
->fields([
'collection',
'name',
'data',
])
->values([
'collection' => '',
'name' => 'linkit.linkit_profile.' . $config['id'],
'data' => serialize($config),
])
->execute();
// Configuration for text formats.
$configs = [];
$configs[] = Yaml::decode(file_get_contents(__DIR__ . '/filter.format.format_1.yml'));
foreach ($configs as $config) {
$connection->insert('config')
->fields([
'collection',
'name',
'data',
])
->values([
'collection' => '',
'name' => 'filter.format.' . $config['format'],
'data' => serialize($config),
])
->execute();
}
// Configuration for editors.
$configs = [];
$configs[] = Yaml::decode(file_get_contents(__DIR__ . '/editor.editor.format_1.yml'));
foreach ($configs as $config) {
$connection->insert('config')
->fields([
'collection',
'name',
'data',
])
->values([
'collection' => '',
'name' => 'editor.editor.' . $config['format'],
'data' => serialize($config),
])
->execute();
}
uuid: e91c2255-2146-46fb-af58-1b391113c352
langcode: en
status: true
dependencies:
module:
- file
- node
id: test_profile
label: 'Test profile'
description: 'This is a test profile'
attributes:
target:
id: target
weight: 0
settings:
widget_type: simple_checkbox
title:
id: title
weight: 0
settings:
automatic_title: false
accesskey:
id: accesskey
weight: 0
settings: { }
matchers:
fc48c807-2a9c-44eb-b86b-7e134c1aa252:
uuid: fc48c807-2a9c-44eb-b86b-7e134c1aa252
id: 'entity:node'
weight: 0
settings:
result_description: 'by [node:author] | [node:created:medium]'
bundles: { }
group_by_bundle: false
include_unpublished: false
b8d6d672-6377-493f-b492-3cc69511cf17:
uuid: b8d6d672-6377-493f-b492-3cc69511cf17
id: 'entity:file'
weight: 0
settings:
result_description: '[file:path] [file:url]'
bundles: null
group_by_bundle: null
images:
show_dimensions: false
show_thumbnail: false
thumbnail_image_style: null
......@@ -2,10 +2,10 @@
/**
* @file
* Database fixture for testing the upgrade path for Linkit 4 to 5.
* Database fixture for testing the update hooks.
*
* Contains database additions to drupal-8.bare.standard.php.gz for testing the
* upgrade path for Linkit 4 to 5.
* update hooks.
*/
use Drupal\Component\Serialization\Yaml;
......
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