diff --git a/google_vision.module b/google_vision.module index a4c567c..dea6244 100644 --- a/google_vision.module +++ b/google_vision.module @@ -7,6 +7,11 @@ 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; +use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Core\Field\FieldDefinitionInterface; /** * Implements hook_entity_presave(). @@ -49,18 +54,50 @@ 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_bundle_field_info_alter(). + */ +function google_vision_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) { + if ($entity_type->id() == 'node') { + foreach ($fields as $field) { + if ($field->getType() == 'image') { + $fields[$field->getName()]->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..9bf064d --- /dev/null +++ b/src/Plugin/Validation/Constraint/SafeSearchConstraint.php @@ -0,0 +1,18 @@ +googlevisionapi = $google_vision; + } + + /** + * {@inheritdoc} + */ + public function validate($data, Constraint $constraint) { + $field_def = $data->getFieldDefinition(); + $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->getValue('target_id'))) { + // Retrieve the file uri. + $file_uri = $data->entity->getFileUri(); + if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { + $result = $this->googlevisionapi->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); + } + } + } + } + } + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static ($container->get('google_vision.api') + ); + } +}