diff --git a/src/Controller/SimilarContentController.php b/src/Controller/SimilarContentController.php
index 8eda922..9ce74e5 100644
--- a/src/Controller/SimilarContentController.php
+++ b/src/Controller/SimilarContentController.php
@@ -29,23 +29,6 @@ class SimilarContentController extends ControllerBase {
}
/**
- * Get the file title.
- *
- * @param integer $fid .
- * The file id.
- *
- * @return string $title.
- * The title of the file id passed.
- */
- public function getFileTitle($fid) {
- $query = $this->connection->select('file_managed', 'fm');
- $query->fields('fm', ['filename']);
- $query->condition('fm.fid', $fid);
- $title = $query->execute()->fetchField();
- return $title;
- }
-
- /**
* Returns the list of image links which share the same dominant color.
*
* @param \Drupal\file\FileInterface $file .
@@ -82,18 +65,24 @@ class SimilarContentController extends ControllerBase {
$query = $this->connection->select('file__field_labels', 'ffl');
$query->fields('ffl', ['entity_id', 'field_labels_target_id']);
$query->condition('field_labels_target_id', $dominant_color, 'IN');
+ $file_ids = array_keys($query->execute()->fetchAllKeyed());
+
+ // Get the file names linked with the file ids.
+ $query = $this->connection->select('file_managed', 'fm');
+ $query->fields('fm', ['fid', 'filename']);
+ $query->condition('fm.fid', $file_ids, 'IN');
$files = $query->execute()->fetchAllKeyed();
- $build['#prefix'] = '
';
- $build['#suffix'] = '
';
+ $build['list'] = [
+ '#theme' => 'item_list',
+ '#items' => [],
+ ];
foreach ($files as $key => $value) {
- $build[$key] = [
- '#prefix' => '',
+ $build['list']['#items'][$key] = [
'#type' => 'link',
- '#title' => $this->getFileTitle($key),
+ '#title' => $value,
'#url' => Url::fromRoute('entity.file.canonical', ['file' => $key]),
- '#suffix' => '',
];
}
}
diff --git a/src/Tests/SimilarContentsTest.php b/src/Tests/SimilarContentsTest.php
index f1ea4e2..5ba4221 100644
--- a/src/Tests/SimilarContentsTest.php
+++ b/src/Tests/SimilarContentsTest.php
@@ -9,6 +9,7 @@ use Drupal\field\Entity\FieldConfig;
use Drupal\Core\Language\LanguageInterface;
use Drupal\taxonomy\Entity\Vocabulary;
+
/**
* Tests whether the similar contents are displayed for the image files or not.
*
@@ -25,6 +26,7 @@ class SimilarContentsTest extends WebTestBase {
'field_ui',
'field',
'file',
+ 'image',
'taxonomy',
'entity_reference',
];
@@ -70,13 +72,15 @@ class SimilarContentsTest extends WebTestBase {
public function uploadImageFile($count) {
$images = $this->drupalGetTestFiles('image');
$edit = [
- 'files[upload]' => \Drupal::service('file_system')
- ->realpath($images[$count]->uri),
+ 'files[upload]' => \Drupal::service('file_system')->realpath($images[$count]->uri),
];
$this->drupalPostForm('file/add', $edit, t('Next'));
$this->drupalPostForm(NULL, array(), t('Next'));
$this->drupalPostForm(NULL, array(), t('Save'));
- return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();
+ // Get the file id of the created file and return it.
+ $query = \Drupal::database()->select('file_managed', 'fm');
+ $query->addExpression('MAX(fid)');
+ return (int) $query->execute()->fetchField();
}
/**
@@ -119,16 +123,16 @@ class SimilarContentsTest extends WebTestBase {
/**
* Creates and returns a new vocabulary.
*
- * @param string $name .
+ * @param string $name.
* The name for the created vocabulary.
*
- * @param string $vid .
+ * @param string $vid.
* The vocabulary id.
*
* @return \Drupal\taxonomy\Entity\Vocabulary $vocabulary.
* The vocabulary.
*/
- function createTaxonomyVocabulary($name, $vid) {
+ public function createTaxonomyVocabulary($name, $vid) {
$vocabulary = Vocabulary::create([
'name' => $name,
'description' => t('Stores the dominant color of the images.'),
@@ -146,26 +150,28 @@ class SimilarContentsTest extends WebTestBase {
$name = 'Dominant Color';
$vid = 'dominant_color';
$count = 0;
+ // Create a taxonomy vocabulary.
$vocabulary = $this->createTaxonomyVocabulary($name, $vid);
- //Check whether the vocabulary is created.
+ // Check whether the vocabulary is created.
$this->drupalGet(Url::fromRoute('entity.taxonomy_vocabulary.collection'));
$this->assertResponse(200);
- //Create a taxonomy reference field.
+ // Create a taxonomy reference field.
$this->createEntityReferenceField($vocabulary);
$edit = [
'google_vision' => 1,
'settings[handler_settings][auto_create]' => 1,
];
$this->drupalPostForm('admin/structure/file-types/manage/image/edit/fields/file.image.field_labels', $edit, t('Save settings'));
- //Ensure that the Dominant Color option is selected.
+ // Ensure that the Dominant Color option is selected.
$this->drupalGet('admin/structure/file-types/manage/image/edit/fields/file.image.field_labels');
// Upload an image file.
$file_id = $this->uploadImageFile($count);
- //Create multiple images to be displayed in the similar contents link.
- for ($count = 1; $count < 3; $count) {
+ // Create multiple images to be displayed in the similar contents link.
+ for ($count = 1; $count < 3; $count++) {
$id[$count] = $this->uploadImageFile($count);
}
+ // Display the similar contents together.
$this->drupalGet('file/' . $file_id . '/similarcontent');
$this->assertResponse(200);
$this->assertNoText('No items found.', 'Similar Contents are displayed.');
@@ -178,28 +184,30 @@ class SimilarContentsTest extends WebTestBase {
$name = 'Labels';
$vid = 'other_labels';
$count = 0;
+ // Create a taxonomy vocabulary.
$vocabulary = $this->createTaxonomyVocabulary($name, $vid);
- //Check whether the vocabulary is created.
+ // Check whether the vocabulary is created.
$this->drupalGet(Url::fromRoute('entity.taxonomy_vocabulary.collection'));
$this->assertResponse(200);
- //Create a taxonomy reference field.
+ // Create a taxonomy reference field.
$this->createEntityReferenceField($vocabulary);
$edit = [
'google_vision' => 1,
'settings[handler_settings][auto_create]' => 1,
];
$this->drupalPostForm('admin/structure/file-types/manage/image/edit/fields/file.image.field_labels', $edit, t('Save settings'));
- //Ensure that the Dominant Color option is not selected.
+ // Ensure that the Dominant Color option is not selected.
$this->drupalGet('admin/structure/file-types/manage/image/edit/fields/file.image.field_labels');
// Upload an image file.
$file_id = $this->uploadImageFile($count);
- //Create multiple images to be displayed in the similar contents link.
- for ($count = 1; $count < 3; $count) {
+ // Create multiple images to be displayed in the similar contents link.
+ for ($count = 1; $count < 3; $count++) {
$id[$count] = $this->uploadImageFile($count);
}
$this->drupalGet('file/' . $file_id . '/similarcontent');
$this->assertResponse(200);
+ // Assert that no similar items are available.
$this->assertText('No items found.', 'No items found message is displayed.');
}
}