diff --git a/config/schema/google_vision.schema.yml b/config/schema/google_vision.schema.yml index e85e0b1..bc0888e 100644 --- a/config/schema/google_vision.schema.yml +++ b/config/schema/google_vision.schema.yml @@ -24,6 +24,6 @@ field.field.*.*.*.third_party.google_vision: emotion_detect: type: boolean label: 'Emotion Detection' - features: + alt_auto_filling: type: string label: 'Fill Alt Text' diff --git a/google_vision.module b/google_vision.module index 269d597..a7f45f1 100644 --- a/google_vision.module +++ b/google_vision.module @@ -33,7 +33,15 @@ function google_vision_entity_presave(EntityInterface $entity) { } // If this field is the alt text field. if ($field->getName() == 'field_image_alt_text') { - google_vision_set_alt_text($entity, $field); + $value = $entity->get($field->getName())->getValue(); + //Set the Alt Text using Vision API. + if (empty($value)) { + google_vision_set_alt_text($entity, $field); + } + else { + //Change the alt text if required. + google_vision_edit_alt_text($entity, $field, $value); + } } } } @@ -87,17 +95,18 @@ function google_vision_form_field_config_edit_form_alter(&$form, \Drupal\Core\Fo '#title' => t('Fill Alt Text by'), '#open' => TRUE, ]; - $form['fill_alt']['features'] = [ + $form['fill_alt']['alt_auto_filling'] = [ '#type' => 'radios', - '#title' => t('Features'), + '#title' => t('Detection Type'), '#title_display' => 'invisible', '#options' => [ 'labels' => t('Label Detection'), 'landmark' => t('Landmark Detection'), 'logo' => t('Logo Detection'), 'ocr' => t('Optical Character Detection'), + 'none' => t('None'), ], - '#default_value' => !empty($settings['features']) ? $settings['features'] : FALSE, + '#default_value' => !empty($settings['alt_auto_filling']) ? $settings['alt_auto_filling'] : FALSE, ]; $form['#entity_builders'][] = 'google_vision_form_field_config_form_alt_text_builder'; @@ -135,7 +144,7 @@ function google_vision_form_field_config_form_image_builder($entity_type, FieldC * Form builder to save the settings for the alt text of image file. */ function google_vision_form_field_config_form_alt_text_builder($entity_type, FieldConfigInterface $type, &$form, FormStateInterface $form_state) { - $type->setThirdPartySetting('google_vision', 'features', $form_state->getValue('features')); + $type->setThirdPartySetting('google_vision', 'alt_auto_filling', $form_state->getValue('alt_auto_filling')); } /** @@ -199,13 +208,15 @@ function google_vision_file_entity_add_labels($file, $field, $vid) { /** * Set the value for the alternative text field of the image file. + * + * @todo move to the service if there will be more helper functions like this. */ function google_vision_set_alt_text($entity, $field) { // Initialize the output. $output = ''; $settings = $field->getThirdPartySettings('google_vision'); - if (!empty($settings['features'])) { - $option = $settings['features']; + if (!empty($settings['alt_auto_filling'])) { + $option = $settings['alt_auto_filling']; $file_uri = $entity->getFileUri(); if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { @@ -241,14 +252,27 @@ function google_vision_set_alt_text($entity, $field) { $output = $data['responses'][0]['textAnnotations']; } break; + + case 'none': + // If none is selected, do nothing. + break; } } - // If we have some data. if (!empty($output)) { // Grab first value (most relevant) and use it. $value = reset($output)['description']; - $entity->set('field_image_alt_text', $value); + $entity->set($field->getName(), $value); } } } + +/** + * Edit the current value of the Alt Text field of the image file. + * + * @todo move to the service if there will be more helper functions like this. + */ +function google_vision_edit_alt_text($entity, $field, $value) { + //Set the new value to the Alt Text field. + $entity->set($field->getName(), $value); +} diff --git a/src/Tests/FillAltTextTest.php b/src/Tests/FillAltTextTest.php index 95ca2a4..7cf1de8 100644 --- a/src/Tests/FillAltTextTest.php +++ b/src/Tests/FillAltTextTest.php @@ -73,12 +73,23 @@ class FillAltTextTest extends WebTestBase { } /** + * Test to ensure that the Alt Text remains empty by default. + */ + public function testNoOptionSelected() { + // Create an image file. + $file_id = $this->uploadImageFile(); + // Ensure that the Alt Text is empty. + $this->drupalGet('file/' . $file_id . '/edit'); + $this->assertFieldByName('field_image_alt_text[0][value]', '', 'Alt Text is empty'); + } + + /** * Test to ensure that Label Detection feature fills the Alt Text correctly. */ public function testFillAltTextByLabels() { // Set the Label Detection option to fill the alt text. $edit = [ - 'features' => 'labels' + 'alt_auto_filling' => 'labels' ]; $this->drupalPostForm('admin/structure/file-types/manage/image/edit/fields/file.image.field_image_alt_text', $edit, t('Save settings')); //Ensure that Label Detection option is set. @@ -96,7 +107,7 @@ class FillAltTextTest extends WebTestBase { public function testFillAltTextByLandmark() { // Set the Landmark Detection option to fill the alt text. $edit = [ - 'features' => 'landmark' + 'alt_auto_filling' => 'landmark' ]; $this->drupalPostForm('admin/structure/file-types/manage/image/edit/fields/file.image.field_image_alt_text', $edit, t('Save settings')); //Ensure that Landmark Detection option is set. @@ -114,7 +125,7 @@ class FillAltTextTest extends WebTestBase { public function testFillAltTextByLogo() { // Set the Logo Detection option to fill the alt text. $edit = [ - 'features' => 'logo' + 'alt_auto_filling' => 'logo' ]; $this->drupalPostForm('admin/structure/file-types/manage/image/edit/fields/file.image.field_image_alt_text', $edit, t('Save settings')); //Ensure that Logo Detection option is set. @@ -132,7 +143,7 @@ class FillAltTextTest extends WebTestBase { public function testFillAltTextByOCR() { // Set the Optical Character Detection option to fill the alt text. $edit = [ - 'features' => 'ocr' + 'alt_auto_filling' => 'ocr' ]; $this->drupalPostForm('admin/structure/file-types/manage/image/edit/fields/file.image.field_image_alt_text', $edit, t('Save settings')); //Ensure that Optical Character Detection option is set. @@ -145,13 +156,20 @@ class FillAltTextTest extends WebTestBase { } /** - * Test to ensure that the Alt Text remains empty if no option is selected. + * Test to ensure that Alt Text is empty when "None" is selected. */ - public function testNoOptionSelected() { + public function testFillAltTextByNone() { + // Set the None option to fill the alt text. + $edit = [ + 'alt_auto_filling' => 'none' + ]; + $this->drupalPostForm('admin/structure/file-types/manage/image/edit/fields/file.image.field_image_alt_text', $edit, t('Save settings')); + //Ensure that None option is set. + $this->drupalGet('admin/structure/file-types/manage/image/edit/fields/file.image.field_image_alt_text'); // Create an image file. $file_id = $this->uploadImageFile(); - // Ensure that the Alt Text is empty. + //Ensure that the Alt Text is empty. $this->drupalGet('file/' . $file_id . '/edit'); $this->assertFieldByName('field_image_alt_text[0][value]', '', 'Alt Text is empty'); } -} \ No newline at end of file +}