diff --git a/google_vision.module b/google_vision.module index b5b4609..ba83995 100644 --- a/google_vision.module +++ b/google_vision.module @@ -33,6 +33,21 @@ function google_vision_entity_presave(Drupal\Core\Entity\EntityInterface $entity // Take first referenced vocabulary. $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. + $new_vid = "dominant_color"; + $name = "Dominant Color"; + $vocabularies = \Drupal\taxonomy\Entity\Vocabulary::loadMultiple(); + // Create a vocabulary if not already present. + if (!isset($vocabularies[$new_vid])) { + $vocabulary = \Drupal\taxonomy\Entity\Vocabulary::create(array( + 'vid' => $new_vid, + 'machine_name' => $new_vid, + 'name' => $name, + )); + $vocabulary->save(); + } + google_vision_file_entity_dominant_color($entity, $field, $new_vid); } } } @@ -96,6 +111,52 @@ function google_vision_entity_bundle_field_info_alter(&$fields, EntityTypeInterf function google_vision_form_field_config_form_image_builder($entity_type, FieldConfigInterface $type, &$form, FormStateInterface $form_state) { $type->setThirdPartySetting('google_vision', 'safe_search', $form_state->getValue('safe_search')); } +/** + * Add the terms to the corresponding taxonomy vocabulary and save the + * values in files. + */ +function google_vision_add_terms_to_vocabulary($file, $field, $vid, $labels) { + // Get existing values from field. + $values = $file->get($field->getName())->getValue(); + $taxonomy_term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); + + // Try to find an existing terms by these labels. + $query = \Drupal::database()->select('taxonomy_term_field_data', 'tfd'); + $query->fields('tfd', ['tid', 'name']); + $query->condition('tfd.vid', $vid); + $query->condition('tfd.name', $labels, 'IN'); + $existing_terms = $query->execute()->fetchAllKeyed(); + + // Handle retrieved labels. + foreach ($labels as $label) { + // Use existing term. + if ($existing_tid = array_search($label, $existing_terms)) { + $already_have = FALSE; + // If we already have this label in this field. Just skip. + foreach ($values as $value) { + if ($value['target_id'] == $existing_tid) { + $already_have = TRUE; + } + } + // Add existing label into field if we haven't it yet. + if (!$already_have) { + $values[] = ['target_id' => $existing_tid]; + } + } + // Create new term and add into field. + else { + $label_term = $taxonomy_term_storage->create([ + 'name' => $label, + 'vid' => $vid + ]); + $label_term->enforceIsNew(); + $label_term->save(); + $values[] = ['target_id' => $label_term->id()]; + } + } + // Save collected values. + $file->set($field->getName(), $values); +} /** * Try to get and add labels for the file entity. @@ -112,46 +173,44 @@ function google_vision_file_entity_add_labels($file, $field, $vid) { $labels[] = $item['description']; } - // Get existing values from field. - $values = $file->get($field->getName())->getValue(); - $taxonomy_term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); - - // Try to find an existing terms by these labels. - $query = \Drupal::database()->select('taxonomy_term_field_data', 'tfd'); - $query->fields('tfd', ['tid', 'name']); - $query->condition('tfd.vid', $vid); - $query->condition('tfd.name', $labels, 'IN'); - $existing_terms = $query->execute()->fetchAllKeyed(); - - // Handle retrieved labels. - foreach ($labels as $label) { - // Use existing term. - if ($existing_tid = array_search($label, $existing_terms)) { - $already_have = FALSE; - // If we already have this label in this field. Just skip. - foreach ($values as $value) { - if ($value['target_id'] == $existing_tid) { - $already_have = TRUE; - } - } - // Add existing label into field if we haven't it yet. - if (!$already_have) { - $values[] = ['target_id' => $existing_tid]; - } - } - // Create new term and add into field. - else { - $label_term = $taxonomy_term_storage->create([ - 'name' => $label, - 'vid' => $vid - ]); - $label_term->enforceIsNew(); - $label_term->save(); - $values[] = ['target_id' => $label_term->id()]; - } + //Add the terms to vocabulary, and set the values to fields in files. + google_vision_add_terms_to_vocabulary($file, $field, $vid, $labels); + } + } +} + +/** + * Try to get the dominant color. + */ +function google_vision_file_entity_dominant_color($file, $field, $vid) { + $file_uri = $file->getFileUri(); + if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { + $data = \Drupal::service('google_vision.api')->imageAttributesDetection($filepath); + // If we have retrieved labels. + if (!empty($data['responses'][0]['imagePropertiesAnnotation'])) { + foreach ($data['responses'][0]['imagePropertiesAnnotation']['dominantColors']['colors'] as $item) { + $red = $item['color']['red']; + $green = $item['color']['green']; + $blue = $item['color']['blue']; + } + //Create an array of the colors along with their values. + $color = array( + 'red' => $red, + 'green' => $green, + 'blue' => $blue + ); + if($red == $green && $green == $blue) { + $dominant_color = ['red', 'green', 'blue']; } - // Save collected values. - $file->set($field->getName(), $values); + else { + $max_value = max($red, $green, $blue); + //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. + google_vision_add_terms_to_vocabulary($file, $field, $vid, $dominant_color); + + $file->set('field_image_title_text', 'rgb(' . $red . ',' . $green . ',' . $blue . ')'); } } } diff --git a/google_vision.routing.yml b/google_vision.routing.yml index ef3434c..b6274d3 100644 --- a/google_vision.routing.yml +++ b/google_vision.routing.yml @@ -5,3 +5,11 @@ google_vision.settings: _title: 'Google vision settings' requirements: _permission: 'administer google vision' + +google_vision.related_content: + path: '/node/{node}/relatedcontent' + defaults: + _controller: '\Drupal\google_vision\Controller\RelatedContentController::fetchContent' + requirements: + _permission: 'administer google vision' + node: \d+ diff --git a/src/Controller/RelatedContentController.php b/src/Controller/RelatedContentController.php new file mode 100644 index 0000000..f5df8c7 --- /dev/null +++ b/src/Controller/RelatedContentController.php @@ -0,0 +1,73 @@ +id(); + //$current_node = Node::load($node_id); + + //Get an array of just term ids. + $query = \Drupal::entityQuery('taxonomy_term'); + $query->condition('vid', "dominant_color"); + $tids = $query->execute(); + $terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids); + $term_id = array_keys($terms); + + // Get the file id associated with the node. + $query = \Drupal::database()->select('file_usage', 'fu'); + $query->fields('fu', ['fid']); + $query->condition('fu.id', $node_id); + $file_id = $query->execute()->fetchField(); + + // Get the list of dominant colors per file. + $dominant_color = []; + foreach ($term_id as $key => $value) { + $query = \Drupal::database()->select('file__field_labels', 'ffl'); + $query->fields('ffl', ['field_labels_target_id']); + $query->condition('ffl.entity_id', $file_id); + $query->condition('ffl.field_labels_target_id', $value); + $dominant_color[] = $query->execute()->fetchField(); + } + + //Get all the file ids which have one or more dominant colors in common with $dominant_color. + $query = \Drupal::database()->select('file__field_labels', 'ffl'); + $query->fields('ffl', ['entity_id', 'field_labels_target_id']); + $query->condition('field_labels_target_id', $dominant_color, 'IN'); + $file = $query->execute()->fetchAllKeyed(); + + //Get the respective nodes. + $nodes = []; + foreach ($file as $key => $value) { + $query = \Drupal::database()->select('file_usage', 'fu'); + $query->fields('fu', ['id']); + $query->condition('fu.fid', $key); + $nodes[] = $query->execute()->fetchField(); + } + + $build = array(); + + foreach ($nodes as $key => $value) { + $build[$key] = []; + $build[$key] = [ + '#type' => 'link', + '#url' => 'admin/node/' . $value, + ]; + } + return $build; + } +} diff --git a/src/GoogleVisionAPI.php b/src/GoogleVisionAPI.php index 4232e6c..7118e6c 100644 --- a/src/GoogleVisionAPI.php +++ b/src/GoogleVisionAPI.php @@ -338,7 +338,7 @@ class GoogleVisionAPI { 'requests' => [ [ 'image' => [ - 'content' => $encoded_imageьфшьть + 'content' => $encoded_image ], 'features' => [ [ @@ -357,4 +357,4 @@ class GoogleVisionAPI { return FALSE; } -} \ No newline at end of file +}