diff --git a/config/schema/google_vision.schema.yml b/config/schema/google_vision.schema.yml index 884df2a..76f0c1a 100644 --- a/config/schema/google_vision.schema.yml +++ b/config/schema/google_vision.schema.yml @@ -21,3 +21,6 @@ field.field.*.*.*.third_party.google_vision: safe_search: type: boolean label: 'Safe Search' + emotion_detect: + type: boolean + label: 'Emotion Detection' diff --git a/google_vision.module b/google_vision.module index 5048265..915ba1e 100644 --- a/google_vision.module +++ b/google_vision.module @@ -65,6 +65,12 @@ function google_vision_form_field_config_edit_form_alter(&$form, \Drupal\Core\Fo '#description' => t('Detects and avoids explicit contents.'), '#default_value' => !empty($settings['safe_search']) ? $settings['safe_search'] : FALSE, ]; + $form['emotion_detect'] = [ + '#type' => 'checkbox', + '#title' => t('Enable Emotion Detection'), + '#description' => t('Detects and notifies if the image is a happy one.'), + '#default_value' => !empty($settings['emotion_detect']) ? $settings['emotion_detect'] : FALSE, + ]; $form['#entity_builders'][] = 'google_vision_form_field_config_form_image_builder'; } @@ -84,6 +90,7 @@ function google_vision_entity_bundle_field_info_alter(&$fields, EntityTypeInterf foreach ($fields as $field) { if ($field->getType() == 'image') { $fields[$field->getName()]->addConstraint('SafeSearch'); + $fields[$field->getName()]->addConstraint('UserEmotion'); } } } @@ -93,6 +100,7 @@ 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')); + $type->setThirdPartySetting('google_vision', 'emotion_detect', $form_state->getValue('emotion_detect')); } /** diff --git a/src/Plugin/Validation/Constraint/UserEmotionConstraint.php b/src/Plugin/Validation/Constraint/UserEmotionConstraint.php new file mode 100644 index 0000000..cd16489 --- /dev/null +++ b/src/Plugin/Validation/Constraint/UserEmotionConstraint.php @@ -0,0 +1,18 @@ +googleVisionAPI = $google_vision; + $this->fileSystem = $file_system; + } + + /** + * {@inheritdoc} + */ + public function validate($data, Constraint $constraint) { + $field_def = $data->getFieldDefinition(); + $settings = $field_def->getThirdPartySettings('google_vision'); + // If the Emotion detection is on. + if (!empty($settings['emotion_detect'])) { + $value = $data->getValue('target_id'); + if (!empty($value)) { + // Retrieve the file uri. + $file_uri = $data->entity->getFileUri(); + if ($filepath = $this->fileSystem->realpath($file_uri)) { + $result = $this->googleVisionAPI->faceDetection($filepath); + if (!empty($result['responses'][0]['faceAnnotations'])) { + $joy = $result['responses'][0]['faceAnnotations'][0]['joyLikelihood']; + $likelihood = array('UNLIKELY', 'VERY_UNLIKELY'); + // If the image is not a happy one. + if (in_array($joy, $likelihood)) { + drupal_set_message($constraint->message, 'warning'); + } + } + } + } + } + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static ( + $container->get('google_vision.api'), + $container->get('file_system') + ); + } +}