diff --git a/google_vision.module b/google_vision.module index 2c64af5..4737aac 100644 --- a/google_vision.module +++ b/google_vision.module @@ -10,6 +10,8 @@ use Drupal\field\FieldConfigInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\taxonomy\Entity\Vocabulary; +use Drupal\file\FileInterface; +use Drupal\Core\Field\FieldDefinitionInterface; /** * Implements hook_entity_presave(). @@ -20,6 +22,7 @@ function google_vision_entity_presave(EntityInterface $entity) { // If it's image. if (in_array($mimetype, ['image/jpeg', 'image/png', 'image/jpg'])) { // Try to find fields which should be used for Google labels. + /** @var FieldDefinitionInterface $field */ foreach ($entity->getFieldDefinitions() as $field) { // If this field is reference to taxonomy term. if ($field->getType() == 'entity_reference' @@ -32,7 +35,8 @@ function google_vision_entity_presave(EntityInterface $entity) { $vid = reset($field->getSettings()['handler_settings']['target_bundles']); google_vision_file_entity_add_labels($entity, $field, $vid); - // Use a new taxonomy vocabulary to group the objects by their dominant color. + // Use a new taxonomy vocabulary to group the objects by their + // dominant color. $vid = 'dominant_color'; $vocabulary = Vocabulary::load($vid); // Create a vocabulary if not already present. @@ -157,10 +161,22 @@ function google_vision_form_field_config_form_alt_text_builder($entity_type, Fie } /** - * Add the terms to the corresponding taxonomy vocabulary and save the - * values in files. + * Add the terms to the corresponding vocabulary and save the values in files. + * + * @param \Drupal\file\FileInterface $file + * The file entity. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field + * The field definition. + * @param string $vid + * The vocabulary machine name. + * @param array $labels + * An indexed array with the values to be added to the vocabulary. For each + * value received in $labels, this function will use the existing TID if the + * value already exists on the vocabulary, or will create a new term if it is + * new. All TIDs then that correspond to the $labels received will be stored + * into the $file entity passed in. */ -function google_vision_add_terms_to_vocabulary($file, $field, $vid, $labels) { +function google_vision_add_terms_to_vocabulary(FileInterface $file, FieldDefinitionInterface $field, $vid, array $labels) { // Get existing values from field. $values = $file->get($field->getName())->getValue(); @@ -193,7 +209,7 @@ function google_vision_add_terms_to_vocabulary($file, $field, $vid, $labels) { ->getStorage('taxonomy_term'); $label_term = $taxonomy_term_storage->create([ 'name' => $label, - 'vid' => $vid + 'vid' => $vid, ]); $label_term->enforceIsNew(); $label_term->save(); @@ -206,8 +222,15 @@ function google_vision_add_terms_to_vocabulary($file, $field, $vid, $labels) { /** * Try to get and add labels for the file entity. + * + * @param \Drupal\file\FileInterface $file + * The file entity. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field + * The field definition. + * @param string $vid + * The vocabulary machine name. */ -function google_vision_file_entity_add_labels($file, $field, $vid) { +function google_vision_file_entity_add_labels(FileInterface $file, FieldDefinitionInterface $field, $vid) { // Try to retrieve file URI. $file_uri = $file->getFileUri(); if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { @@ -227,8 +250,15 @@ function google_vision_file_entity_add_labels($file, $field, $vid) { /** * Try to get the dominant color. + * + * @param \Drupal\file\FileInterface $file + * The file entity. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field + * The field definition. + * @param string $vid + * The vocabulary machine name. */ -function google_vision_file_entity_dominant_color($file, $field, $vid) { +function google_vision_file_entity_dominant_color(FileInterface $file, FieldDefinitionInterface $field, $vid) { $file_uri = $file->getFileUri(); if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { $data = \Drupal::service('google_vision.api') @@ -240,21 +270,21 @@ function google_vision_file_entity_dominant_color($file, $field, $vid) { $green = $item['color']['green']; $blue = $item['color']['blue']; } - //Create an array of the colors along with their values. + // Create an array of the colors along with their values. $color = array( 'red' => $red, 'green' => $green, - 'blue' => $blue + 'blue' => $blue, ); if ($red == $green && $green == $blue) { $dominant_color = ['red', 'green', 'blue']; } else { $max_value = max($red, $green, $blue); - //Get the dominant color based on the maximum value. + // Get the dominant color based on the maximum value. $dominant_color = array_keys($color, $max_value); } - //Add the terms to vocabulary, and set the values to fields in files. + // Add the terms to vocabulary, and set the values to fields in files. google_vision_add_terms_to_vocabulary($file, $field, $vid, $dominant_color); } } @@ -262,15 +292,20 @@ function google_vision_file_entity_dominant_color($file, $field, $vid) { /** * 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. */ -function google_vision_set_alt_text($entity, $field) { +function google_vision_set_alt_text(FileInterface $file, FieldDefinitionInterface $field) { // Initialize the output. $output = ''; $settings = $field->getThirdPartySettings('google_vision'); if (!empty($settings['features'])) { $option = $settings['features']; - $file_uri = $entity->getFileUri(); + $file_uri = $file->getFileUri(); if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { $gv_service = \Drupal::service('google_vision.api'); switch ($option) { @@ -281,6 +316,7 @@ function google_vision_set_alt_text($entity, $field) { $output = $data['responses'][0]['labelAnnotations']; } break; + case 'landmark': $data = $gv_service->landmarkDetection($filepath); // If we have retrieved landmark. @@ -311,7 +347,7 @@ function google_vision_set_alt_text($entity, $field) { if (!empty($output)) { // Grab first value (most relevant) and use it. $value = reset($output)['description']; - $entity->set('field_image_alt_text', $value); + $file->set('field_image_alt_text', $value); } } }