diff --git a/README.md b/README.md
index fb547c8..ab431fa 100644
--- a/README.md
+++ b/README.md
@@ -64,6 +64,11 @@ If automatic titles is enabled in the Linkit filter settings, and
*title* to the `` tag in the *Allowed HTML* tags (i.e.
``).
+* (optional) **Automatically set the "download" attribute for Media entities**
+If this option is enabled in the Linkit filter settings, and
+**Limit allowed HTML tags and correct faulty HTML** is enabled, add
+*download* to the `` tag in the *Allowed HTML* tags (i.e.
+``).
CONFIGURATION
-------------
diff --git a/config/schema/linkit.schema.yml b/config/schema/linkit.schema.yml
index f9eef21..36d1803 100644
--- a/config/schema/linkit.schema.yml
+++ b/config/schema/linkit.schema.yml
@@ -98,6 +98,9 @@ filter_settings.linkit:
title:
type: boolean
label: 'Automatically set the "title" attribute'
+ download:
+ type: boolean
+ label: 'Automatically set the "download" attribute for Media entities'
# Plugin \Drupal\ckeditor\Plugin\CKEditorPlugin\DrupalLink
# Linkit alters the plugin to save Linkit specific information used by the
diff --git a/src/Plugin/Filter/LinkitFilter.php b/src/Plugin/Filter/LinkitFilter.php
index 87c2c15..8c92ffd 100644
--- a/src/Plugin/Filter/LinkitFilter.php
+++ b/src/Plugin/Filter/LinkitFilter.php
@@ -9,6 +9,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Utility\Error;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
+use Drupal\linkit\Plugin\Linkit\Substitution\Media;
use Drupal\linkit\SubstitutionManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -131,6 +132,15 @@ class LinkitFilter extends FilterBase implements ContainerFactoryPluginInterface
continue;
}
+ // Check if we need to add the download attribute to Media files.
+ if ($this->settings['download'] && $this->substitutionManager
+ ->createInstance($substitution_type) instanceof Media) {
+ $media_type = $entity->get('bundle')->entity;
+ if (!empty($media_type->getSource()->getConfiguration()['source_field'])) {
+ $element->setAttribute('download', $entity->label());
+ }
+ }
+
// Parse link href as url, extract query and fragment from it.
$href_url = parse_url($element->getAttribute('href'));
if (!empty($href_url["fragment"])) {
@@ -187,6 +197,11 @@ class LinkitFilter extends FilterBase implements ContainerFactoryPluginInterface
'#type' => 'checkbox',
'#title' => $this->t('Automatically set the title attribute to that of the (translated) referenced content'),
'#default_value' => $this->settings['title'],
+ ];
+ $form['download'] = [
+ '#type' => 'checkbox',
+ '#title' => $this->t('Automatically set the download attribute to Media entities'),
+ '#default_value' => (!empty($this->settings['download'])) ? 1 : 0,
];
return $form;
}
diff --git a/tests/src/Kernel/LinkitFilterEntityTest.php b/tests/src/Kernel/LinkitFilterEntityTest.php
index df51c50..9b82764 100644
--- a/tests/src/Kernel/LinkitFilterEntityTest.php
+++ b/tests/src/Kernel/LinkitFilterEntityTest.php
@@ -85,7 +85,7 @@ class LinkitFilterEntityTest extends LinkitKernelTestBase {
$entity_with_access->save();
// Automatically set the title.
- $this->filter->setConfiguration(['settings' => ['title' => 1]]);
+ $this->filter->setConfiguration(['settings' => ['title' => 1, 'download' => 0]]);
// Make sure the title is not included.
$input = 'Link text';
@@ -115,14 +115,14 @@ class LinkitFilterEntityTest extends LinkitKernelTestBase {
$this->createPathAlias($url, '/' . $this->randomMachineName(), 'fi');
// Disable the automatic title attribute.
- $this->filter->setConfiguration(['settings' => ['title' => 0]]);
+ $this->filter->setConfiguration(['settings' => ['title' => 0, 'download' => 0]]);
/** @var \Drupal\Core\Language\Language $language */
foreach ($entity->getTranslationLanguages() as $language) {
$this->assertLinkitFilter($entity->getTranslation($language->getId()), $language->getId());
}
// Enable the automatic title attribute.
- $this->filter->setConfiguration(['settings' => ['title' => 1]]);
+ $this->filter->setConfiguration(['settings' => ['title' => 1, 'download' => 0]]);
/** @var \Drupal\Core\Language\Language $language */
foreach ($entity->getTranslationLanguages() as $language) {
$this->assertLinkitFilterWithTitle($entity->getTranslation($language->getId()), $language->getId());
@@ -143,11 +143,11 @@ class LinkitFilterEntityTest extends LinkitKernelTestBase {
$file->save();
// Disable the automatic title attribute.
- $this->filter->setConfiguration(['settings' => ['title' => 0]]);
+ $this->filter->setConfiguration(['settings' => ['title' => 0, 'download' => 0]]);
$this->assertLinkitFilter($file);
// Automatically set the title.
- $this->filter->setConfiguration(['settings' => ['title' => 1]]);
+ $this->filter->setConfiguration(['settings' => ['title' => 1, 'download' => 0]]);
$this->assertLinkitFilterWithTitle($file);
}
@@ -160,7 +160,7 @@ class LinkitFilterEntityTest extends LinkitKernelTestBase {
$entity->save();
// Automatically set the title.
- $this->filter->setConfiguration(['settings' => ['title' => 1]]);
+ $this->filter->setConfiguration(['settings' => ['title' => 1, 'download' => 0]]);
// Make sure the title is not overwritten.
$input = 'Link text';