diff --git a/config/optional/linkit.linkit_profile.default.yml b/config/optional/linkit.linkit_profile.default.yml index 23af68b..fe06c78 100644 --- a/config/optional/linkit.linkit_profile.default.yml +++ b/config/optional/linkit.linkit_profile.default.yml @@ -15,4 +15,5 @@ matchers: result_description: 'by [node:author] | [node:created:medium]' bundles: { } group_by_bundle: false - include_unpublished: false \ No newline at end of file + include_unpublished: false + substitution_type: canonical diff --git a/linkit.install b/linkit.install index a2edbaf..ca94081 100644 --- a/linkit.install +++ b/linkit.install @@ -5,9 +5,6 @@ * Contains install and update functions for Linkit. */ -use Drupal\filter\Entity\FilterFormat; -use Drupal\linkit\Entity\Profile; - /** * Updates Linkit from 4 to 5. * @@ -130,30 +127,39 @@ function linkit_update_8500() { * Prepare anchor attributes for substitution plugins. */ function linkit_update_8501() { - // Update all filter formats that allow the data-entity-uuid attribute to also - // allow the data-entity-substitution attribute. - foreach (FilterFormat::loadMultiple() as $id => $filter) { - if ($filter_html = $filter->filters('filter_html')) { - $configuration = $filter_html->getConfiguration(); - - $allowed_html = $configuration['settings']['allowed_html']; - $allowed_html = str_replace('data-entity-uuid', 'data-entity-uuid data-entity-substitution', $allowed_html); - $configuration['settings']['allowed_html'] = $allowed_html; - - $filter_html->setConfiguration($configuration); - $filter->save(); + $config_factory = \Drupal::configFactory(); + + // Update filter formats that allow the data-entity-uuid attribute on link + // tags to also allow the data-entity-substitution attribute. + foreach ($config_factory->listAll('filter.format.') as $id) { + $config = $config_factory->getEditable($id); + + $allowed_html = $config->get('filters.filter_html.settings.allowed_html'); + if (!empty($allowed_html)) { + preg_match_all('/<([\w]+)[^>]*>/', $allowed_html, $out); + $current_mapping = array_combine($out[1], $out[0]); + + if (isset($current_mapping['a'])) { + $allowed_html = str_replace($current_mapping['a'], str_replace('data-entity-uuid', 'data-entity-uuid data-entity-substitution', $current_mapping['a']), $allowed_html); + $config->set('filters.filter_html.settings.allowed_html', $allowed_html); + $config->save(TRUE); + } } } - // Update all "file" matchers to the "file" substitution plugin, to maintain - // existing behavior out of the box. - foreach (\Drupal\linkit\Entity\Profile::loadMultiple() as $profile) { - foreach ($profile->getMatchers() as $matcher) { - if ($matcher->getPluginId() === 'entity:file') { - $configuration = $matcher->getConfiguration(); - $configuration['settings']['substitution_type'] = 'file'; - $matcher->setConfiguration($configuration); - $profile->save(); + + // Update all matchers to a substitution plugin, to maintain existing + // behavior out of the box. + foreach ($config_factory->listAll('linkit.linkit_profile.') as $id) { + $profile = $config_factory->getEditable($id); + foreach ($profile->get('matchers') as $instance_uuid => $matcher) { + if ($matcher['id'] === 'entity:file') { + $profile->set('matchers.' . $instance_uuid . '.settings.substitution_type', 'file'); + } + else { + $profile->set('matchers.' . $instance_uuid . '.settings.substitution_type', 'canonical'); } + + $profile->save(TRUE); } } } diff --git a/src/Plugin/Filter/LinkitFilter.php b/src/Plugin/Filter/LinkitFilter.php index 3dd71b1..fdf19f0 100644 --- a/src/Plugin/Filter/LinkitFilter.php +++ b/src/Plugin/Filter/LinkitFilter.php @@ -98,7 +98,7 @@ class LinkitFilter extends FilterBase implements ContainerFactoryPluginInterface // maintaining the previous hard-coded direct file link assumptions, // for content created before the substitution feature. if (!$substitution_type = $element->getAttribute('data-entity-substitution')) { - $substitution_type = $entity_type === 'file' ? 'file' : SubstitutionManagerInterface::DEFAULT_SUBSTITUTION; + $substitution_type = $entity_type === 'file' ? 'file' : SubstitutionManagerInterface::DEFAULT_SUBSTITUTION; } $entity = $this->entityRepository->loadEntityByUuid($entity_type, $uuid); @@ -106,6 +106,7 @@ class LinkitFilter extends FilterBase implements ContainerFactoryPluginInterface $entity = $this->entityRepository->getTranslationFromContext($entity, $langcode); + /** @var \Drupal\Core\GeneratedUrl $url */ $url = $this->substitutionManager ->createInstance($substitution_type) ->getUrl($entity); diff --git a/src/Plugin/Linkit/Substitution/Canonical.php b/src/Plugin/Linkit/Substitution/Canonical.php index 5e9fca0..10b1db8 100644 --- a/src/Plugin/Linkit/Substitution/Canonical.php +++ b/src/Plugin/Linkit/Substitution/Canonical.php @@ -23,11 +23,4 @@ class Canonical extends PluginBase implements SubstitutionInterface { return $entity->toUrl('canonical')->toString(TRUE); } - /** - * {@inheritdoc} - */ - public static function isApplicable(EntityInterface $entity) { - return TRUE; - } - } diff --git a/src/Plugin/Linkit/Substitution/File.php b/src/Plugin/Linkit/Substitution/File.php index deb7ed5..b74bc50 100644 --- a/src/Plugin/Linkit/Substitution/File.php +++ b/src/Plugin/Linkit/Substitution/File.php @@ -23,6 +23,7 @@ class File extends PluginBase implements SubstitutionInterface { */ public function getUrl(EntityInterface $entity) { $url = new GeneratedUrl(); + /** @var \Drupal\file\FileInterface $entity */ $url->setGeneratedUrl(file_create_url($entity->getFileUri())); $url->addCacheableDependency($entity); return $url; diff --git a/src/SubstitutionInterface.php b/src/SubstitutionInterface.php index 7454264..1760c0b 100644 --- a/src/SubstitutionInterface.php +++ b/src/SubstitutionInterface.php @@ -11,7 +11,7 @@ use Drupal\Core\Entity\EntityInterface; interface SubstitutionInterface extends PluginInspectionInterface { /** - * Get the URL asscoaited with a given entity. + * Get the URL associated with a given entity. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity to get a URL for. diff --git a/src/SubstitutionManager.php b/src/SubstitutionManager.php index 445725b..cd5fec5 100644 --- a/src/SubstitutionManager.php +++ b/src/SubstitutionManager.php @@ -2,9 +2,7 @@ namespace Drupal\linkit; -use Drupal\Component\Utility\SortArray; use Drupal\Core\Cache\CacheBackendInterface; -use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; diff --git a/src/SubstitutionManagerInterface.php b/src/SubstitutionManagerInterface.php index 61cc68f..b03a33c 100644 --- a/src/SubstitutionManagerInterface.php +++ b/src/SubstitutionManagerInterface.php @@ -3,7 +3,6 @@ namespace Drupal\linkit; use Drupal\Component\Plugin\PluginManagerInterface; -use Drupal\Core\Entity\EntityInterface; /** * An interface for the substitution manager. @@ -18,10 +17,10 @@ interface SubstitutionManagerInterface extends PluginManagerInterface { /** * Filter a list of plugin definitions by entity ID. * - * @param string $entity_id - * The entity ID to get applicable plugins for. * @param array $definitions * An array of plugin definitions. + * @param string $entity_id + * The entity ID to get applicable plugins for. * * @return array * The definitions appropriate for the given entity ID. @@ -33,6 +32,7 @@ interface SubstitutionManagerInterface extends PluginManagerInterface { * * @param string $entity_id * The entity ID. + * * @return array * An options list. */ diff --git a/src/Tests/Update/LinkitUpdate8501.php b/src/Tests/Update/LinkitUpdate8501.php new file mode 100644 index 0000000..cadbec6 --- /dev/null +++ b/src/Tests/Update/LinkitUpdate8501.php @@ -0,0 +1,58 @@ +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'])); + } + +} diff --git a/tests/fixtures/update/8501/editor.editor.format_1.yml b/tests/fixtures/update/8501/editor.editor.format_1.yml new file mode 100644 index 0000000..46a0f3f --- /dev/null +++ b/tests/fixtures/update/8501/editor.editor.format_1.yml @@ -0,0 +1,25 @@ +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 diff --git a/tests/fixtures/update/8501/filter.format.format_1.yml b/tests/fixtures/update/8501/filter.format.format_1.yml new file mode 100644 index 0000000..2874da3 --- /dev/null +++ b/tests/fixtures/update/8501/filter.format.format_1.yml @@ -0,0 +1,17 @@ +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: '
    1. ' + filter_html_help: true + filter_html_nofollow: false \ No newline at end of file diff --git a/tests/fixtures/update/8501/linkit-update-8501.php b/tests/fixtures/update/8501/linkit-update-8501.php new file mode 100644 index 0000000..d7bdc2b --- /dev/null +++ b/tests/fixtures/update/8501/linkit-update-8501.php @@ -0,0 +1,66 @@ +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(); +} diff --git a/tests/fixtures/update/8501/linkit.linkit_profile.test_profile.yml b/tests/fixtures/update/8501/linkit.linkit_profile.test_profile.yml new file mode 100644 index 0000000..bee4523 --- /dev/null +++ b/tests/fixtures/update/8501/linkit.linkit_profile.test_profile.yml @@ -0,0 +1,47 @@ +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