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

Commit 3cfd9e2

Browse files
gitslashrsm
authored andcommitted
Issue #2697817 by Denchev, slashrsm, Berdir, CTaPByK: ALT text and TITLe text not showing on images
1 parent 29204bc commit 3cfd9e2

File tree

4 files changed

+195
-12
lines changed

4 files changed

+195
-12
lines changed

config/schema/file_entity.schema.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ file_entity.settings:
4949

5050
field.formatter.settings.file_image:
5151
type: field.formatter.settings.image
52+
label: 'Image formatter for file entity settings'
53+
mapping:
54+
title:
55+
type: string
56+
label: 'Title field'
57+
alt:
58+
type: string
59+
label: 'Alt field'
5260

5361
field.formatter.settings.file_size:
5462
type: mapping

src/Plugin/Field/FieldFormatter/FileImageFormatter.php

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

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

105
use Drupal\Core\Cache\Cache;
11-
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
6+
use Drupal\Core\Entity\EntityFieldManagerInterface;
7+
use Drupal\Core\Entity\EntityStorageInterface;
8+
use Drupal\Core\Field\FieldConfigInterface;
9+
use Drupal\Core\Field\FieldDefinitionInterface;
1210
use Drupal\Core\Field\FieldItemListInterface;
1311
use Drupal\Core\Form\FormStateInterface;
12+
use Drupal\Core\Session\AccountInterface;
1413
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
14+
use Symfony\Component\DependencyInjection\ContainerInterface;
1515

1616
/**
1717
* Implementation of the 'image' formatter for the file_entity files.
@@ -26,6 +26,89 @@
2626
*/
2727
class FileImageFormatter extends ImageFormatter {
2828

29+
/**
30+
* The entity field manager.
31+
*
32+
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
33+
*/
34+
protected $entityFieldManager;
35+
36+
/**
37+
* Constructs on FileImageFormatter object.
38+
*
39+
* @param string $plugin_id
40+
* The plugin_id for the formatter.
41+
* @param mixed $plugin_definition
42+
* The plugin implementation definition.
43+
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
44+
* The definition of the field to which the formatter is associated.
45+
* @param array $settings
46+
* The formatter settings.
47+
* @param string $label
48+
* The formatter label display setting.
49+
* @param string $view_mode
50+
* The view mode.
51+
* @param array $third_party_settings
52+
* Any third party settings settings.
53+
* @param \Drupal\Core\Session\AccountInterface $current_user
54+
* The current user.
55+
* @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
56+
* The image style entity storage class.
57+
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
58+
* The entity field manager.
59+
*/
60+
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityStorageInterface $image_style_storage, EntityFieldManagerInterface $entity_field_manager) {
61+
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage);
62+
$this->entityFieldManager = $entity_field_manager;
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
69+
return new static(
70+
$plugin_id,
71+
$plugin_definition,
72+
$configuration['field_definition'],
73+
$configuration['settings'],
74+
$configuration['label'],
75+
$configuration['view_mode'],
76+
$configuration['third_party_settings'],
77+
$container->get('current_user'),
78+
$container->get('entity.manager')->getStorage('image_style'),
79+
$container->get('entity_field.manager')
80+
);
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public static function defaultSettings() {
87+
return [
88+
'title' => 'field_image_title_text',
89+
'alt' => 'field_image_alt_text',
90+
] + parent::defaultSettings();
91+
}
92+
93+
/**
94+
* {@inheritdoc}
95+
*/
96+
public function settingsSummary() {
97+
$summary = parent::settingsSummary();
98+
if ($this->getSetting('title') == '_none') {
99+
$summary[] = $this->t('Title attribute is hidden.');
100+
} else {
101+
$summary[] = $this->t('Field used for the image title attribute: @title', ['@title' => $this->getSetting('title')]);
102+
}
103+
if ($this->getSetting('alt') == '_none') {
104+
$summary[] = $this->t('Alt attribute is hidden.');
105+
} else {
106+
$summary[] = $this->t('Field used for the image alt attribute: @alt', ['@alt' => $this->getSetting('alt')]);
107+
}
108+
109+
return $summary;
110+
}
111+
29112
/**
30113
* {@inheritdoc}
31114
*/
@@ -60,7 +143,12 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
60143
'tags' => $cache_tags,
61144
],
62145
];
63-
146+
foreach (['title', 'alt'] as $element_name) {
147+
$field_name = $this->getSetting($element_name);
148+
if ($field_name !== '_none' && $file->hasField($field_name)) {
149+
$elements[0]['#' . $element_name] = $file->$field_name->value;
150+
}
151+
}
64152
return $elements;
65153
}
66154

@@ -75,6 +163,34 @@ public function prepareView(array $entities_items) {}
75163
public function settingsForm(array $form, FormStateInterface $form_state) {
76164
$element = parent::settingsForm($form, $form_state);
77165
unset($element['image_link']);
166+
$available_fields = $this->entityFieldManager->getFieldDefinitions(
167+
$form['#entity_type'],
168+
$form['#bundle']
169+
);
170+
$options = [];
171+
foreach ($available_fields as $field_name => $field_definition) {
172+
if ($field_definition instanceof FieldConfigInterface && $field_definition->getType() == 'string') {
173+
$options[$field_name] = $field_definition->label();
174+
}
175+
}
176+
$element['title'] = [
177+
'#title' => $this->t('Image title field'),
178+
'#description' => $this->t('The field that is used as source for the image title attribute.'),
179+
'#type' => 'select',
180+
'#options' => $options,
181+
'#default_value' => $this->getSetting('title'),
182+
'#empty_option' => $this->t('No title attribute'),
183+
'#empty_value' => '_none',
184+
];
185+
$element['alt'] = [
186+
'#title' => $this->t('Image alt field'),
187+
'#description' => $this->t('The field that is used as source for the image alt attribute.'),
188+
'#type' => 'select',
189+
'#options' => $options,
190+
'#default_value' => $this->getSetting('alt'),
191+
'#empty_option' => $this->t('No alt attribute'),
192+
'#empty_value' => '_none',
193+
];
78194
return $element;
79195
}
80196

src/Tests/FileEntityCreationTest.php

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

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

105
use Drupal\Core\Language\LanguageInterface;
@@ -130,6 +125,10 @@ public function testImageAltTitleFields() {
130125
$edit[$field . '[0][value]'] = $value;
131126
}
132127
$this->drupalPostForm(NULL, $edit, t('Save'));
128+
$file = $this->getFileByFilename('image-test.png');
129+
$this->drupalGet('file/' . $file->id());
130+
$this->assertRaw('alt="A test image"', 'Alt attribute is shown and has the correct value.');
131+
$this->assertRaw('title="My image"', 'Title attribute is shown and has the correct value.');
133132

134133
// Make sure the field values are saved.
135134
$created_file = FileEntity::load(1)->getTranslation(LanguageInterface::LANGCODE_DEFAULT);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Drupal\file_entity\Tests;
4+
5+
/**
6+
* Tests file entity settings.
7+
*
8+
* @group file_entity
9+
*/
10+
class FileEntitySettingsTest extends FileEntityTestBase {
11+
12+
/**
13+
* Modules to install.
14+
*
15+
* @var array
16+
*/
17+
public static $modules = ['field_ui'];
18+
19+
/**
20+
* Tests file image formatter settings.
21+
*/
22+
public function testFileImageFormatterSettings() {
23+
$account = $this->drupalCreateUser([
24+
'administer file display'
25+
]);
26+
$this->drupalLogin($account);
27+
$this->drupalGet('admin/structure/file-types/manage/image/edit/display');
28+
$this->assertText('Field used for the image title attribute: field_image_title_text', 'Settings summary for title field is displayed correctly.');
29+
$this->assertText('Field used for the image title attribute: field_image_title_text', 'Settings summary for alt field is displayed correctly.');
30+
31+
$this->drupalPostAjaxForm(NULL, [], 'uri_settings_edit');
32+
$this->assertRaw('fields[uri][settings_edit_form][settings][title]', 'Field for setting title field is available.');
33+
$this->assertRaw('fields[uri][settings_edit_form][settings][alt]', 'Field for setting alt field is available.');
34+
35+
$edit = [
36+
'fields[uri][settings_edit_form][settings][title]' => '_none',
37+
'fields[uri][settings_edit_form][settings][alt]' => '_none',
38+
];
39+
$this->drupalPostAjaxForm(NULL, $edit, ['uri_plugin_settings_update' => t('Update')]);
40+
$this->drupalPostForm(NULL, [], t('Save'));
41+
$this->assertText('Title attribute is hidden.');
42+
$this->assertText('Alt attribute is hidden.');
43+
44+
$this->drupalLogin($this->drupalCreateUser(['create files']));
45+
$test_file = $this->getTestFile('image');
46+
$this->drupalGet('file/add');
47+
$edit = [
48+
'files[upload]' => $this->container->get('file_system')->realpath($test_file->uri),
49+
];
50+
$this->drupalPostForm(NULL, $edit, t('Next'));
51+
$this->drupalPostForm(NULL, [], t('Next'));
52+
$edit = [
53+
'field_image_alt_text[0][value]' => 'Alt text',
54+
'field_image_title_text[0][value]' => 'Title text',
55+
];
56+
$this->drupalPostForm(NULL, $edit, t('Save'));
57+
$this->assertNoRaw('alt="Alt text"', 'Alt attribute is hidden.');
58+
$this->assertNoRaw('title="Title text"', 'Title attribute is hidden.');
59+
}
60+
}

0 commit comments

Comments
 (0)