Skip to content
This repository was archived by the owner on Jan 5, 2018. It is now read-only.

Commit 0bab734

Browse files
gitslashrsm
authored andcommitted
Issue #2716201 by Denchev, slashrsm, CTaPByK: Add configurable access denied message "Download link" field formatter.
1 parent b9993dd commit 0bab734

File tree

3 files changed

+101
-21
lines changed

3 files changed

+101
-21
lines changed

config/schema/file_entity.schema.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ field.formatter.settings.file_download_link:
5757
type: mapping
5858
label: 'File download link display format settings'
5959
mapping:
60+
access_message:
61+
type: string
62+
label: 'Access message'
6063
text:
6164
type: string
6265
label: 'Link text'

src/Plugin/Field/FieldFormatter/FileDownloadLinkFormatter.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
<?php
22

3-
/**
4-
* @file
5-
* Contains Drupal\file_entity\Plugin\Field\FieldFormatter\FileDownloadLinkFormatter
6-
*/
7-
83
namespace Drupal\file_entity\Plugin\Field\FieldFormatter;
94

10-
115
use Drupal\Core\Extension\ModuleHandlerInterface;
126
use Drupal\Core\Field\FieldDefinitionInterface;
137
use Drupal\Core\Field\FieldItemListInterface;
@@ -107,6 +101,12 @@ public static function create(ContainerInterface $container, array $configuratio
107101
* {@inheritdoc}
108102
*/
109103
public function settingsForm(array $form, FormStateInterface $form_state) {
104+
$element['access_message'] = [
105+
'#type' => 'textfield',
106+
'#title' => t('No access message'),
107+
'#description' => t("This text is shown instead of the download link if the user doesn't have permission to download the file."),
108+
'#default_value' => $this->getSetting('access_message'),
109+
];
110110
$element['text'] = array(
111111
'#type' => 'textfield',
112112
'#title' => t('Link text'),
@@ -132,9 +132,10 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
132132
* {@inheritdoc}
133133
*/
134134
public static function defaultSettings() {
135-
return array(
135+
return [
136+
'access_message' => "You don't have access to download this file.",
136137
'text' => 'Download [file:name]',
137-
) + parent::defaultSettings();
138+
] + parent::defaultSettings();
138139
}
139140

140141
/**
@@ -159,17 +160,25 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
159160
// Set options as per anchor format described at
160161
// http://microformats.org/wiki/file-format-examples
161162
$download_url = $file->downloadUrl(array('attributes' => array('type' => $mime_type . '; length=' . $file->getSize())));
162-
$elements[$delta] = array(
163-
'#theme' => 'file_entity_download_link',
164-
'#file' => $file,
165-
'#download_link' => Link::fromTextAndUrl($link_text, $download_url),
166-
'#icon' => file_icon_class($mime_type),
167-
'#attributes' => $attributes,
168-
'#file_size' => format_size($file->getSize()),
169-
);
163+
if ($file->access('download')) {
164+
$elements[$delta] = [
165+
'#theme' => 'file_entity_download_link',
166+
'#file' => $file,
167+
'#download_link' => Link::fromTextAndUrl($link_text, $download_url),
168+
'#icon' => file_icon_class($mime_type),
169+
'#attributes' => $attributes,
170+
'#file_size' => format_size($file->getSize()),
171+
];
172+
}
173+
else {
174+
$elements[$delta] = [
175+
'#markup' => $this->getSetting('access_message'),
176+
];
177+
}
170178
$this->renderer->addCacheableDependency($elements[$delta], $file);
171179
}
172180

173181
return $elements;
174182
}
183+
175184
}

src/Tests/FileEntityAccessTest.php

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
<?php
22

3-
/**
4-
* @file
5-
* Contains \Drupal\file_entity\Tests\FileEntityAccessTest.
6-
*/
7-
83
namespace Drupal\file_entity\Tests;
94

105
use Drupal\file\FileInterface;
116
use Drupal\file_entity\FileEntityAccessControlHandler;
7+
use Drupal\node\Entity\Node;
128

139
/**
1410
* Tests the access aspects of file entity.
@@ -17,6 +13,13 @@
1713
*/
1814
class FileEntityAccessTest extends FileEntityTestBase {
1915

16+
/**
17+
* Modules to install.
18+
*
19+
* @var array
20+
*/
21+
public static $modules = ['node'];
22+
2023
/**
2124
* The File Entity access controller.
2225
*
@@ -225,4 +228,69 @@ function testFileEntityPrivateDownloadAccess() {
225228
}
226229
}
227230
}
231+
232+
/**
233+
* Tests file download access.
234+
*/
235+
public function testDownloadLinkAccess() {
236+
// Create content type with image field.
237+
$this->drupalCreateContentType([
238+
'name' => 'Article',
239+
'type' => 'article',
240+
]);
241+
\Drupal::entityTypeManager()->getStorage('field_storage_config')->create([
242+
'field_name' => 'image',
243+
'entity_type' => 'node',
244+
'type' => 'image',
245+
'cardinality' => 1,
246+
])->save();
247+
\Drupal::entityTypeManager()->getStorage('field_config')->create([
248+
'field_name' => 'image',
249+
'label' => 'Image',
250+
'entity_type' => 'node',
251+
'bundle' => 'article',
252+
])->save();
253+
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
254+
$form_display = \Drupal::entityTypeManager()->getStorage('entity_form_display')->load('node.article.default');
255+
$form_display->setComponent('image', [
256+
'type' => 'image_image',
257+
])->save();
258+
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
259+
$view_display = \Drupal::entityTypeManager()->getStorage('entity_view_display')->load('node.article.default');
260+
$view_display->setComponent('image', [
261+
'type' => 'file_download_link',
262+
])->save();
263+
264+
$account = $this->drupalCreateUser([
265+
'download any image files',
266+
]);
267+
$this->drupalLogin($account);
268+
$image = current($this->files['image']);
269+
$node = Node::create([
270+
'title' => 'Title',
271+
'type' => 'article',
272+
'image' => [
273+
'target_id' => $image->id(),
274+
'alt' => 'the image alternative',
275+
],
276+
]);
277+
$node->save();
278+
$this->drupalGet('node/' . $node->id());
279+
280+
$this->assertRaw('file/' . $image->id() . '/download', 'Download link available.');
281+
$this->assertLink('Download image-test.png');
282+
283+
$this->drupalLogout();
284+
$this->drupalGet('node/' . $node->id());
285+
$this->assertText("You don't have access to download this file.", 'No access message displays correctly.');
286+
$view_display->setComponent('image', [
287+
'type' => 'file_download_link',
288+
'settings' => [
289+
'access_message' => 'Another message.',
290+
],
291+
])->save();
292+
$this->drupalGet('node/' . $node->id());
293+
$this->assertText('Another message.', 'No access message updated.');
294+
}
295+
228296
}

0 commit comments

Comments
 (0)