diff --git a/file_entity.file.inc b/file_entity.file.inc index 74feaa6..2c8ec55 100644 --- a/file_entity.file.inc +++ b/file_entity.file.inc @@ -166,6 +166,7 @@ function file_entity_file_mimetype_mapping_alter(&$mapping) { * Implements hook_file_load(). */ function file_entity_file_load($files) { + // Add alt and title text to images. $alt = variable_get('file_entity_alt', '[file:field_file_image_alt_text]'); $title = variable_get('file_entity_title', '[file:field_file_image_title_text]'); @@ -179,17 +180,35 @@ function file_entity_file_load($files) { // Load alt and title text from fields. if (!empty($alt)) { - $file->alt = token_replace($alt, array('file' => $file), $replace_options); + $output = token_replace($alt, array('file' => $file), $replace_options); + + // @todo Remove once https://www.drupal.org/node/1713164 is fixed. + // There is currently no way to get the raw alt text returned from the + // token so we revert the encoding done during tokenization. + $file->alt = decode_entities($output; } if (!empty($title)) { - $file->title = token_replace($title, array('file' => $file), $replace_options); + $output = token_replace($title, array('file' => $file), $replace_options); + + // @todo Remove once https://www.drupal.org/node/1713164 is fixed. + // There is currently no way to get the raw title text returned from the + // token so we revert the encoding done during tokenization. + $file->title = decode_entities($output; } } // Load and unserialize metadata. $results = db_query("SELECT * FROM {file_metadata} WHERE fid IN (:fids)", array(':fids' => array_keys($files))); + foreach ($results as $result) { - $files[$result->fid]->metadata[$result->name] = unserialize($result->value); + $name = $result->name; + + // image.module required height and width to be properties of the file. + if ($name == 'height' || $name == 'width') { + $files[$result->fid]->$name = unserialize($result->value); + } + + $files[$result->fid]->metadata[$name] = unserialize($result->value); } } diff --git a/file_entity.info b/file_entity.info index 62f966f..7e60c74 100644 --- a/file_entity.info +++ b/file_entity.info @@ -2,10 +2,14 @@ name = File entity description = "Extends Drupal file entities to be fieldable and viewable." package = Media core = 7.x + dependencies[] = field dependencies[] = file dependencies[] = ctools dependencies[] = system (>=7.9) + +test_dependencies[] = token + files[] = views/views_handler_argument_file_type.inc files[] = views/views_handler_field_file_rendered.inc files[] = views/views_handler_field_file_type.inc @@ -20,6 +24,7 @@ files[] = views/views_handler_field_file_link_usage.inc files[] = views/views_plugin_row_file_rss.inc files[] = views/views_plugin_row_file_view.inc files[] = file_entity.test + configure = admin/config/media/file-settings ; We have to add a fake version so Git checkouts do not fail Media dependencies diff --git a/file_entity.test b/file_entity.test index 259c98e..2739aef 100644 --- a/file_entity.test +++ b/file_entity.test @@ -610,6 +610,83 @@ class FileEntityAdminTestCase extends FileEntityTestHelper { } /** + * Tests image alt and title text. + */ +class FileEntityAltTitleTestCase extends FileEntityTestHelper { + public static function getInfo() { + return array( + 'name' => 'File entity alt and title text', + 'description' => 'Create an image file with alt and title text.', + 'group' => 'File entity', + 'dependencies' => array('token'), + ); + } + + function setUp() { + parent::setUp('token'); + + $web_user = $this->drupalCreateUser(array('create files', 'edit own image files')); + $this->drupalLogin($web_user); + } + + /** + * Create an "image" file and verify its associated alt and title text. + */ + function testFileEntityAltTitle() { + $test_file = $this->getTestFile('image'); + $alt_field_name = 'field_file_image_alt_text'; // Name of the default alt text field added to the image file type. + $title_field_name = 'field_file_image_title_text'; // Name of the default title text field added to the image file type. + + // Create a file. + $edit = array(); + $edit['files[upload]'] = drupal_realpath($test_file->uri); + $this->drupalPost('file/add', $edit, t('Next')); + + // Step 2: Scheme selection. + if ($this->xpath('//input[@name="scheme"]')) { + $this->drupalPost(NULL, array(), t('Next')); + } + + // Step 3: Attached fields. + $alt = 'Quote" Amp& ' . 'Файл для тестирования ' . $this->randomName(); // Generate alt text containing HTML entities, spaces and non-latin characters. + $title = 'Quote" Amp& ' . 'Файл для тестирования ' . $this->randomName(); // Generate title text containing HTML entities, spaces and non-latin characters. + + $edit = array(); + $edit[$alt_field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $alt; + $edit[$title_field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $title; + $this->drupalPost(NULL, $edit, t('Save')); + + // Check that the image file has been uploaded. + $this->assertRaw(t('!type %name was uploaded.', array('!type' => 'Image', '%name' => $test_file->filename)), t('Image file uploaded.')); + + // Check that the file exists in the database. + $file = $this->getFileByFilename($test_file->filename); + $this->assertTrue($file, t('File found in database.')); + + // Check that the alt and title text was loaded from the fields. + $this->assertEqual($file->alt, $alt, t('Alt text was stored as file metadata.')); + $this->assertEqual($file->title, $title, t('Title text was stored as file metadata.')); + + // Verify that the alt and title text is present on the page. + $image_info = array( + 'path' => $file->uri, + 'alt' => $alt, + 'title' => $title, + 'width' => $file->width, + 'height' => $file->height, + ); + $default_output = theme('image', $image_info); + $this->assertRaw($default_output, 'Image displayed using user supplied alt and title attributes.'); + + // Verify that the alt and title text fields are found on the file edit + // page. + $this->drupalGet('file/' . $file->fid . '/edit'); + $this->assertFieldByName($alt_field_name . '[' . LANGUAGE_NONE . '][0][value]'); + $this->assertFieldByName($title_field_name . '[' . LANGUAGE_NONE . '][0][value]'); + } +} + +/** * Tests replacing the file associated with a file entity. */ class FileEntityReplaceTestCase extends FileEntityTestHelper {