diff --git a/google_vision.module b/google_vision.module index 3d722fa..bc854ec 100644 --- a/google_vision.module +++ b/google_vision.module @@ -31,6 +31,10 @@ function google_vision_entity_presave(EntityInterface $entity) { google_vision_file_entity_add_labels($entity, $field, $vid); } } + // If this field is the alt text field. + if ($field->getName() == 'field_image_alt_text') { + google_vision_set_alt_text($entity, $field); + } } } } @@ -69,9 +73,31 @@ function google_vision_form_field_config_edit_form_alter(&$form, \Drupal\Core\Fo '#default_value' => !empty($settings['emotion_detect']) ? $settings['emotion_detect'] : FALSE, ]; - $form['#entity_builders'][] = 'google_vision_form_field_config_form_image_builder'; } + + if ($field_entity->getName() == 'field_image_alt_text') { + $settings = $form_state->getFormObject()->getEntity()->getThirdPartySettings('google_vision'); + $form['fill_alt'] = [ + '#type' => 'details', + '#title' => t('Fill Alt Text by'), + '#open' => TRUE, + ]; + $form['fill_alt']['features'] = [ + '#type' => 'radios', + '#title' => t('Features'), + '#title_display' => 'invisible', + '#options' => [ + 'labels' => t('Label Detection'), + 'landmark' => t('Landmark Detection'), + 'logo' => t('Logo Detection'), + 'ocr' => t('Optical Character Detection'), + ], + '#default_value' => !empty($settings['features']) ? $settings['features'] : FALSE, + ]; + + $form['#entity_builders'][] = 'google_vision_form_field_config_form_alt_text_builder'; + } } /** @@ -102,6 +128,12 @@ 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')); +} +/** * Try to get and add labels for the file entity. */ function google_vision_file_entity_add_labels($file, $field, $vid) { @@ -159,3 +191,66 @@ function google_vision_file_entity_add_labels($file, $field, $vid) { } } } + +/** + * Set the value for the alternative text field of the image file. + */ +function google_vision_set_alt_text($entity, $field) { + $settings = $field->getThirdPartySettings('google_vision'); + if (!empty($settings['features'])) { + $option = $settings['features']; + switch ($option) { + case 'labels': + $file_uri = $entity->getFileUri(); + if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { + $data = \Drupal::service('google_vision.api')->labelDetection($filepath); + // If we have retrieved labels. + if (!empty($data['responses'][0]['labelAnnotations'])) { + foreach ($data['responses'][0]['labelAnnotations'] as $item) { + $output = $item['description']; + } + } + } + break; + case 'landmark': + $file_uri = $entity->getFileUri(); + if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { + $data = \Drupal::service('google_vision.api')->landmarkDetection($filepath); + // If we have retrieved landmark. + if (!empty($data['responses'][0]['landmarkAnnotations'])) { + foreach ($data['responses'][0]['landmarkAnnotations'] as $item) { + $output = $item['description']; + } + } + } + break; + + case 'logo': + $file_uri = $entity->getFileUri(); + if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { + $data = \Drupal::service('google_vision.api')->logoDetection($filepath); + // If we have retrieved landmark. + if (!empty($data['responses'][0]['logoAnnotations'])) { + foreach ($data['responses'][0]['logoAnnotations'] as $item) { + $output = $item['description']; + } + } + } + break; + + case 'ocr': + $file_uri = $entity->getFileUri(); + if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { + $data = \Drupal::service('google_vision.api')->opticalCharacterRecognition($filepath); + // If we have retrieved landmark. + if (!empty($data['responses'][0]['textAnnotations'])) { + foreach ($data['responses'][0]['textAnnotations'] as $item) { + $output = $item['description']; + } + } + } + break; + } + $entity->set('field_image_alt_text', $output); + } +}