diff --git a/google_vision.links.task.yml b/google_vision.links.task.yml index 5ee2624..98d41df 100644 --- a/google_vision.links.task.yml +++ b/google_vision.links.task.yml @@ -2,4 +2,4 @@ google_vision.related_content: route_name: google_vision.related_content base_route: entity.file.canonical title: 'Similar Contents' - weight: 10 \ No newline at end of file + weight: 10 diff --git a/google_vision.module b/google_vision.module index 26eac29..067a0b5 100644 --- a/google_vision.module +++ b/google_vision.module @@ -94,13 +94,14 @@ function google_vision_entity_presave(EntityInterface $entity) { // If this field is the alt text field. if ($field->getName() == 'field_image_alt_text') { $value = $entity->get($field->getName())->getValue(); + $fill_alt_text = \Drupal::service('google_vision.fill_alt_text'); //Set the Alt Text using Vision API. if (empty($value)) { - google_vision_set_alt_text($entity, $field); + $fill_alt_text->setAltText($entity, $field); } else { //Change the alt text if required. - google_vision_edit_alt_text($entity, $field, $value); + $fill_alt_text->editAltText($entity, $field, $value); } } } @@ -310,71 +311,3 @@ function google_vision_file_entity_dominant_color($file, $field, $vid) { } } } - -/** - * Set the value for the alternative text field of the image file. - */ -function google_vision_set_alt_text($entity, $field) { - // Initialize the output. - $output = ''; - $settings = $field->getThirdPartySettings('google_vision'); - if (!empty($settings['alt_auto_filling'])) { - $option = $settings['alt_auto_filling']; - - $file_uri = $entity->getFileUri(); - if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { - $gv_service = \Drupal::service('google_vision.api'); - switch ($option) { - case 'labels': - $data = $gv_service->labelDetection($filepath); - // If we have retrieved labels. - if (!empty($data['responses'][0]['labelAnnotations'])) { - $output = $data['responses'][0]['labelAnnotations']; - } - break; - case 'landmark': - $data = $gv_service->landmarkDetection($filepath); - // If we have retrieved landmark. - if (!empty($data['responses'][0]['landmarkAnnotations'])) { - $output = $data['responses'][0]['landmarkAnnotations']; - } - break; - - case 'logo': - $data = $gv_service->logoDetection($filepath); - // If we have retrieved logo. - if (!empty($data['responses'][0]['logoAnnotations'])) { - $output = $data['responses'][0]['logoAnnotations']; - } - break; - - case 'ocr': - $data = $gv_service->opticalCharacterRecognition($filepath); - // If we have retrieved character. - if (!empty($data['responses'][0]['textAnnotations'])) { - $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->getName(), $value); - } - } -} - -/** - * Edit the current value of the Alt Text field of the image file. - */ -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/google_vision.routing.yml b/google_vision.routing.yml index b901e53..95ab2e1 100644 --- a/google_vision.routing.yml +++ b/google_vision.routing.yml @@ -13,4 +13,4 @@ google_vision.related_content: _title: 'Similar Contents' requirements: _permission: 'administer files' - file: \d+ \ No newline at end of file + file: \d+ diff --git a/google_vision.services.yml b/google_vision.services.yml index be39f19..ba15783 100644 --- a/google_vision.services.yml +++ b/google_vision.services.yml @@ -1,4 +1,7 @@ services: google_vision.api: class: Drupal\google_vision\GoogleVisionAPI - arguments: ['@config.factory', '@http_client'] \ No newline at end of file + arguments: ['@config.factory', '@http_client'] + google_vision.fill_alt_text: + class: Drupal\google_vision\AutoAltTextFill + arguments: ['@file_system', '@google_vision.api'] diff --git a/src/AutoAltTextFill.php b/src/AutoAltTextFill.php new file mode 100644 index 0000000..7fcb41d --- /dev/null +++ b/src/AutoAltTextFill.php @@ -0,0 +1,128 @@ +fileSystem = $file_system; + $this->googlevisionapi = $google_vision_api; + } + + /** + * Set the value for the alternative text field of the image file. + * + * @param \Drupal\file\FileInterface $file + * The file entity. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field + * The field definition. + */ + public function setAltText($file, $field) { + // Initialize the output. + $output = ''; + $settings = $field->getThirdPartySettings('google_vision'); + if (!empty($settings['alt_auto_filling'])) { + $option = $settings['alt_auto_filling']; + + $file_uri = $file->getFileUri(); + if ($filepath = $this->fileSystem->realpath($file_uri)) { + switch ($option) { + case 'labels': + $data = $this->googlevisionapi->labelDetection($filepath); + // If we have retrieved labels. + if (!empty($data['responses'][0]['labelAnnotations'])) { + $output = $data['responses'][0]['labelAnnotations']; + } + break; + + case 'landmark': + $data = $this->googlevisionapi->landmarkDetection($filepath); + // If we have retrieved landmark. + if (!empty($data['responses'][0]['landmarkAnnotations'])) { + $output = $data['responses'][0]['landmarkAnnotations']; + } + break; + + case 'logo': + $data = $this->googlevisionapi->logoDetection($filepath); + // If we have retrieved logo. + if (!empty($data['responses'][0]['logoAnnotations'])) { + $output = $data['responses'][0]['logoAnnotations']; + } + break; + + case 'ocr': + $data = $this->googlevisionapi->opticalCharacterRecognition($filepath); + // If we have retrieved character. + if (!empty($data['responses'][0]['textAnnotations'])) { + $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']; + $file->set($field->getName(), $value); + } + } + } + + /** + * Edit the current value of the Alt Text field of the image file. + * + * @param \Drupal\file\FileInterface $file + * The file entity. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field + * The field definition. + * @param string $value + * The Alt Text field value. + */ + public function editAltText($file, $field, $value) { + //Set the new value to the Alt Text field. + $file->set($field->getName(), $value); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('google_vision.api'), + $container->get('file_system') + ); + } +}