diff --git a/google_vision.module b/google_vision.module index 51c50c5..26c0078 100755 --- a/google_vision.module +++ b/google_vision.module @@ -7,6 +7,9 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\field\FieldConfigInterface; use Drupal\google_vision\GoogleVisionAPI; +use Drupal\field\FieldStorageConfigInterface; +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityTypeInterface; /** * Implements hook_entity_presave(). @@ -49,18 +52,45 @@ function google_vision_form_field_config_edit_form_alter(&$form, \Drupal\Core\Fo '#default_value' => !empty($settings['google_vision']) ? $settings['google_vision'] : FALSE, ]; - $form['#entity_builders'][] = 'google_vision_form_field_config_form_builder'; + $form['#entity_builders'][] = 'google_vision_form_field_config_form_taxonomy_builder'; + } + + if($field_entity->getType() == 'image') { + $settings = $form_state->getFormObject()->getEntity()->getThirdPartySettings('google_vision'); + $form['safe_search'] = [ + '#type' => 'checkbox', + '#title' => t('Enable Safe Search'), + '#description' => t('Detects and avoids explicit contents.'), + '#default_value' => !empty($settings['google_vision']) ? $settings['google_vision'] : FALSE, + ]; + + $form['#entity_builders'][] = 'google_vision_form_field_config_form_image_builder'; } } /** - * Form builder to save the settings. + * Form builder to save the settings for entity reference. */ -function google_vision_form_field_config_form_builder($entity_type, FieldConfigInterface $type, &$form, FormStateInterface $form_state) { +function google_vision_form_field_config_form_taxonomy_builder($entity_type, FieldConfigInterface $type, &$form, FormStateInterface $form_state) { $type->setThirdPartySetting('google_vision', 'google_vision', $form_state->getValue('google_vision')); } /** + * Implements hook_entity_type_alter(). + */ +function google_vision_entity_type_alter(array &$entity_types) { + $node = $entity_types['node']; + $node->addConstraint('SafeSearch'); +} + +/** + * Form builder to save the settings for images. + */ +function google_vision_form_field_config_form_image_builder($entity_type, FieldConfigInterface $type, &$form, FormStateInterface $form_state) { + $type->setThirdPartySetting('google_vision', 'google_vision', $form_state->getValue('safe_search')); +} + +/** * Try to get and add labels for the file entity. */ function google_vision_file_entity_add_labels($file, $field, $vid) { diff --git a/src/Plugin/Validation/Constraint/SafeSearchConstraint.php b/src/Plugin/Validation/Constraint/SafeSearchConstraint.php new file mode 100755 index 0000000..aa40208 --- /dev/null +++ b/src/Plugin/Validation/Constraint/SafeSearchConstraint.php @@ -0,0 +1,18 @@ +getFieldDefinitions('node', $data->getType()) as $field_name => $field_def) { + // if field is image field. + if($field_def->getType() == 'image') { + $settings = $field_def->getThirdPartySettings('google_vision'); + // if the Safe Search detection is on. + if(!empty($settings['google_vision'])) { + // if the image is uploaded. + if(!empty($data->get($field_name)->target_id)) { + // Retrieve the file uri. + $file_uri = $data->get($field_name)->entity->getFileUri(); + if($filepath = \Drupal::service('file_system')->realpath($file_uri)) { + $result = \Drupal::service('google_vision.api')->safeSearchDetection($filepath); + if(!empty($result['responses'][0]['safeSearchAnnotation'])) { + $adult = $result['responses'][0]['safeSearchAnnotation']['adult']; + $likelihood = array('LIKELY', 'VERY_LIKELY'); + // if the image has explicit content. + if(in_array($adult, $likelihood)) { + $this->context->addViolation($constraint->message, array('%title' => $data->get($field_name)->entity->getFilename())); + } + } + } + } + } + } + } + } +}