Commit 765b197a authored by mpp's avatar mpp Committed by anon

Issue #2997546 by mpp, a-fro, anon: Add download attribute for Media with a...

Issue #2997546 by mpp, a-fro, anon: Add download attribute for Media with a direct url to the Media file entity
parent 877614d4
......@@ -36,6 +36,13 @@ If automatic titles is enabled in the Linkit filter settings, and
*title* to the `<a>` tag in the *Allowed HTML* tags (i.e.
`<a href data-entity-type data-entity-uuid title>`).
* (optional) **Automatically set the "download" attribute for Media entities**
_Only for Media entity links that has the substitution type "Direct URL to
media file entity"._ 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 `<a>` tag in the *Allowed HTML* tags (i.e.
`<a href data-entity-type data-entity-uuid download>`).
Configuration
------------
......
......@@ -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
......
......@@ -41,8 +41,8 @@
rule.required.tags = ['a'];
rule.allowed.tags = ['a'];
// Attributes.
rule.required.attributes = ['data-entity-substitution', 'data-entity-type', 'data-entity-uuid', 'title'];
rule.allowed.attributes = ['data-entity-substitution', 'data-entity-type', 'data-entity-uuid', 'title'];
rule.required.attributes = ['data-entity-substitution', 'data-entity-type', 'data-entity-uuid', 'title', 'download'];
rule.allowed.attributes = ['data-entity-substitution', 'data-entity-type', 'data-entity-uuid', 'title', 'download'];
linkitFeature.addHTMLRule(rule);
return linkitFeature;
......
......@@ -8,6 +8,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\linkit\Plugin\Linkit\Substitution\Media;
use Drupal\linkit\SubstitutionManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -100,13 +101,19 @@ class LinkitFilter extends FilterBase implements ContainerFactoryPluginInterface
$entity = $this->entityRepository->loadEntityByUuid($entity_type, $uuid);
if ($entity) {
$entity = $this->entityRepository->getTranslationFromContext($entity, $langcode);
/** @var \Drupal\Core\GeneratedUrl $url */
$url = $this->substitutionManager
->createInstance($substitution_type)
->getUrl($entity);
$substitution = $this->substitutionManager->createInstance($substitution_type);
$url = $substitution->getUrl($entity);
// Check if we need to add the download attribute to Media files.
if ($this->settings['download'] && $substitution instanceof Media) {
$media_type = $entity->get('bundle')->entity;
if (!empty($media_type->getSource()->getConfiguration()['source_field'])) {
$element->setAttribute('download', $entity->label());
}
}
$element->setAttribute('href', $url->getGeneratedUrl());
$access = $entity->access('view', NULL, TRUE);
......@@ -146,10 +153,13 @@ class LinkitFilter extends FilterBase implements ContainerFactoryPluginInterface
'#type' => 'checkbox',
'#title' => $this->t('Automatically set the <code>title</code> attribute to that of the (translated) referenced content'),
'#default_value' => $this->settings['title'],
'#attached' => [
'library' => ['linkit/linkit.filter_html.admin'],
],
];
$form['download'] = [
'#type' => 'checkbox',
'#title' => $this->t('Automatically set the <code>download</code> attribute to Media entities'),
'#default_value' => (!empty($this->settings['download'])) ? 1 : 0,
];
$form['#attached']['library'][] = 'linkit/linkit.filter_html.admin';
return $form;
}
......
......@@ -44,15 +44,15 @@ class LinkitFormatAdminTest extends WebDriverTestBase {
$page->findField('filters[filter_html][status]')->check();
$javascript = "(function (){ return jQuery('p.editor-update-message > strong').text(); })()";
$this->assertNotContains('<a href hreflang data-entity-substitution data-entity-type data-entity-uuid title>', $session->evaluateScript($javascript));
$this->assertNotContains('<a href hreflang data-entity-substitution data-entity-type data-entity-uuid title download>', $session->evaluateScript($javascript));
// Enable the 'Linkit filter' filter.
$page->findField('filters[linkit][status]')->check();
$this->assertContains('<a href hreflang data-entity-substitution data-entity-type data-entity-uuid title>', $session->evaluateScript($javascript));
$this->assertContains('<a href hreflang data-entity-substitution data-entity-type data-entity-uuid title download>', $session->evaluateScript($javascript));
// Disable the 'Linkit filter' filter.
$page->findField('filters[linkit][status]')->uncheck();
$this->assertNotContains('<a href hreflang data-entity-substitution data-entity-type data-entity-uuid title>', $session->evaluateScript($javascript));
$this->assertNotContains('<a href hreflang data-entity-substitution data-entity-type data-entity-uuid title download>', $session->evaluateScript($javascript));
}
}
......@@ -60,7 +60,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 = '<a data-entity-type="' . $entity_no_access->getEntityTypeId() . '" data-entity-uuid="' . $entity_no_access->uuid() . '">Link text</a>';
......@@ -93,14 +93,14 @@ class LinkitFilterEntityTest extends LinkitKernelTestBase {
$path_alias_storage->save($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());
......@@ -121,11 +121,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);
}
......@@ -138,7 +138,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 = '<a data-entity-type="' . $entity->getEntityTypeId() . '" data-entity-uuid="' . $entity->uuid() . '" title="Do not override">Link text</a>';
......
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