{"action":"created","comment":{"url":"https://api.github.com/repos/drupal-media/image_widget_crop/pulls/comments/49716131","id":49716131,"diff_hunk":"@@ -203,3 +204,236 @@ function image_widget_crop_library_info_alter(&$libraries, $extension) {\n     }\n   }\n }\n+\n+/**\n+ * @param $form\n+ * @param \\Drupal\\Core\\Form\\FormStateInterface $form_state\n+ */\n+function image_widget_crop_form_submit(&$form, \\Drupal\\Core\\Form\\FormStateInterface $form_state) {\n+  // Get old values.\n+  $old_values = $form_state->getValues();\n+\n+  // Get values from js.\n+  $user_values = $form_state->getUserInput();\n+\n+  $crop_types = $user_values['crop_preview_wrapper'];\n+\n+  foreach ($crop_types as $crop_type_id => $crop_container) {\n+    $old_properties = $old_values['crop_preview_wrapper'][$crop_type_id]['crop_container']['values'];\n+    $new_properties = $user_values['crop_preview_wrapper'][$crop_type_id]['crop_container']['values'];\n+\n+    // If we don't have old values and not creating new crop then simply\n+    // continue.\n+    if ($old_properties['edit'] == FALSE && $new_properties['crop_applied'] == 0) {\n+      continue;\n+    }\n+\n+    $image_widget_crop_manager = \\Drupal::service('image_widget_crop.manager');\n+\n+    /** @var \\Drupal\\crop\\Entity\\CropType $crop_type */\n+    $crop_type = \\Drupal::entityManager()\n+      ->getStorage('crop_type')\n+      ->loadByProperties(['id' => $crop_type_id]);\n+\n+    // If the crop type needed is disabled or delete.\n+    if (!isset($crop_type[$crop_type_id]) || empty($crop_type[$crop_type_id])) {\n+      drupal_set_message(t(\"The CropType ('@cropType') is not active or not defined. Please verify configuration of image style or ImageWidgetCrop formatter configuration\", ['@cropType' => $crop_type['crop_type_name']]), 'error');\n+      return;\n+    }\n+\n+    if ($old_properties['edit'] == FALSE && $new_properties['crop_applied'] == 1) {\n+      if (!isset($crop_type) && empty($crop_type)) {\n+        drupal_set_message(t(\"The type of crop does not exist, please check the configuration of the CropType ('@cropType')\", ['@cropType' => $crop_type->label()]), 'error');\n+        return;\n+      }\n+      $image_widget_crop_manager->applyCrop($new_properties, $old_properties, $crop_type[$crop_type_id]);\n+    }\n+\n+    // Get all imagesStyle used this crop_type.\n+    // TODO this is currently not returning anything. Check.\n+    $image_styles = $image_widget_crop_manager->getImageStylesByCrop($crop_type[$crop_type_id]);\n+    $crops = $image_widget_crop_manager->loadImageStyleByCrop($image_styles, $crop_type[$crop_type_id], $old_properties['file-id']);\n+\n+    if ($old_properties['edit'] == TRUE) {\n+      if ($new_properties['crop_applied'] == 1) {\n+        if ($image_widget_crop_manager->cropHasChanged($new_properties, $old_properties)) {\n+          $image_widget_crop_manager->updateCrop($new_properties, $old_properties, $crop_type[$crop_type_id]);\n+        }\n+      }\n+      elseif ($new_properties['crop_applied'] == 0) {\n+        $image_widget_crop_manager->deleteCrop($old_properties['file-uri'], $crop_type[$crop_type_id], $old_properties['file-id']);\n+      }\n+    }\n+\n+  }\n+}\n+\n+/**\n+ * Implements hook_form_alter().\n+ */\n+function image_widget_crop_form_alter(&$form, \\Drupal\\Core\\Form\\FormStateInterface $form_state, $form_id) {\n+  // We only want crop ui on images.\n+  if (substr($form_id, 0, 5) == 'file_' && (substr($form_id, -10, 10) == '_edit_form' || substr($form_id, -17, 17) == '_inline_edit_form')) {\n+    $entity = $form_state->getFormObject()->getEntity();\n+    $mime_type = $entity->getMimeTypeType();\n+\n+    if ($mime_type !== 'image') {\n+      return;\n+    }\n+    $element['#theme'] = 'image_widget';\n+    $element['#attached']['library'][] = 'image/form';\n+    $form['#attached']['library'][] = 'image_widget_crop/cropper.integration';\n+\n+    $form['crop_preview_wrapper'] = [\n+      '#type' => 'details',\n+      '#title' => t('Crop image'),\n+      '#attributes' => ['class' => ['crop-wrapper']],\n+      '#weight' => 100,\n+      '#tree' => TRUE,\n+    ];\n+\n+    // Warn the user if the crop is used more than once.\n+    $usage_counter = 0;\n+    $file_usage = \\Drupal::service('file.usage')->listUsage($entity);","path":"image_widget_crop.module","position":102,"original_position":102,"commit_id":"ab31fba0a75efa79a77004b40a68549d4bbe6d19","original_commit_id":"ab31fba0a75efa79a77004b40a68549d4bbe6d19","user":{"login":"woprrr","id":8972139,"avatar_url":"https://avatars.githubusercontent.com/u/8972139?v=3","gravatar_id":"","url":"https://api.github.com/users/woprrr","html_url":"https://github.com/woprrr","followers_url":"https://api.github.com/users/woprrr/followers","following_url":"https://api.github.com/users/woprrr/following{/other_user}","gists_url":"https://api.github.com/users/woprrr/gists{/gist_id}","starred_url":"https://api.github.com/users/woprrr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/woprrr/subscriptions","organizations_url":"https://api.github.com/users/woprrr/orgs","repos_url":"https://api.github.com/users/woprrr/repos","events_url":"https://api.github.com/users/woprrr/events{/privacy}","received_events_url":"https://api.github.com/users/woprrr/received_events","type":"User","site_admin":false},"body":"I am fully agree of this ! in file_entity context users are conscient of impact all places where it finds the file.","created_at":"2016-01-14T11:37:47Z","updated_at":"2016-01-14T11:37:47Z","html_url":"https://github.com/drupal-media/image_widget_crop/pull/1#discussion_r49716131","pull_request_url":"https://api.github.com/repos/drupal-media/image_widget_crop/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/drupal-media/image_widget_crop/pulls/comments/49716131"},"html":{"href":"https://github.com/drupal-media/image_widget_crop/pull/1#discussion_r49716131"},"pull_request":{"href":"https://api.github.com/repos/drupal-media/image_widget_crop/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/drupal-media/image_widget_crop/pulls/1","id":56004833,"html_url":"https://github.com/drupal-media/image_widget_crop/pull/1","diff_url":"https://github.com/drupal-media/image_widget_crop/pull/1.diff","patch_url":"https://github.com/drupal-media/image_widget_crop/pull/1.patch","issue_url":"https://api.github.com/repos/drupal-media/image_widget_crop/issues/1","number":1,"state":"open","locked":false,"title":"Crop tool on file entity","user":{"login":"Dencev","id":2672319,"avatar_url":"https://avatars.githubusercontent.com/u/2672319?v=3","gravatar_id":"","url":"https://api.github.com/users/Dencev","html_url":"https://github.com/Dencev","followers_url":"https://api.github.com/users/Dencev/followers","following_url":"https://api.github.com/users/Dencev/following{/other_user}","gists_url":"https://api.github.com/users/Dencev/gists{/gist_id}","starred_url":"https://api.github.com/users/Dencev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dencev/subscriptions","organizations_url":"https://api.github.com/users/Dencev/orgs","repos_url":"https://api.github.com/users/Dencev/repos","events_url":"https://api.github.com/users/Dencev/events{/privacy}","received_events_url":"https://api.github.com/users/Dencev/received_events","type":"User","site_admin":false},"body":"On d.o https://www.drupal.org/node/2617856\r\n\r\nA few notes about the pr:\r\n1. After getting some notes on improving this I will work on helper functions.\r\n2. Currently in the submit handler I'm checking if we have any crop of that type to call applyCrop not if its new. The is new for the file is always returning false AFAIK.\r\n3. Also hardcoded image style(not a good option). Do we maybe want some dropdown somewhere?","created_at":"2016-01-14T08:26:40Z","updated_at":"2016-01-14T11:37:47Z","closed_at":null,"merged_at":null,"merge_commit_sha":"478c1d3512476b780b79de0076e0a9925c84de4e","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/drupal-media/image_widget_crop/pulls/1/commits","review_comments_url":"https://api.github.com/repos/drupal-media/image_widget_crop/pulls/1/comments","review_comment_url":"https://api.github.com/repos/drupal-media/image_widget_crop/pulls/comments{/number}","comments_url":"https://api.github.com/repos/drupal-media/image_widget_crop/issues/1/comments","statuses_url":"https://api.github.com/repos/drupal-media/image_widget_crop/statuses/ab31fba0a75efa79a77004b40a68549d4bbe6d19","head":{"label":"Dencev:crop_tool_on_file_entity","ref":"crop_tool_on_file_entity","sha":"ab31fba0a75efa79a77004b40a68549d4bbe6d19","user":{"login":"Dencev","id":2672319,"avatar_url":"https://avatars.githubusercontent.com/u/2672319?v=3","gravatar_id":"","url":"https://api.github.com/users/Dencev","html_url":"https://github.com/Dencev","followers_url":"https://api.github.com/users/Dencev/followers","following_url":"https://api.github.com/users/Dencev/following{/other_user}","gists_url":"https://api.github.com/users/Dencev/gists{/gist_id}","starred_url":"https://api.github.com/users/Dencev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dencev/subscriptions","organizations_url":"https://api.github.com/users/Dencev/orgs","repos_url":"https://api.github.com/users/Dencev/repos","events_url":"https://api.github.com/users/Dencev/events{/privacy}","received_events_url":"https://api.github.com/users/Dencev/received_events","type":"User","site_admin":false},"repo":{"id":49568941,"name":"image_widget_crop","full_name":"Dencev/image_widget_crop","owner":{"login":"Dencev","id":2672319,"avatar_url":"https://avatars.githubusercontent.com/u/2672319?v=3","gravatar_id":"","url":"https://api.github.com/users/Dencev","html_url":"https://github.com/Dencev","followers_url":"https://api.github.com/users/Dencev/followers","following_url":"https://api.github.com/users/Dencev/following{/other_user}","gists_url":"https://api.github.com/users/Dencev/gists{/gist_id}","starred_url":"https://api.github.com/users/Dencev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dencev/subscriptions","organizations_url":"https://api.github.com/users/Dencev/orgs","repos_url":"https://api.github.com/users/Dencev/repos","events_url":"https://api.github.com/users/Dencev/events{/privacy}","received_events_url":"https://api.github.com/users/Dencev/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Dencev/image_widget_crop","description":"Image widget crop module - https://www.drupal.org/sandbox/woprrr/2571403","fork":true,"url":"https://api.github.com/repos/Dencev/image_widget_crop","forks_url":"https://api.github.com/repos/Dencev/image_widget_crop/forks","keys_url":"https://api.github.com/repos/Dencev/image_widget_crop/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Dencev/image_widget_crop/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Dencev/image_widget_crop/teams","hooks_url":"https://api.github.com/repos/Dencev/image_widget_crop/hooks","issue_events_url":"https://api.github.com/repos/Dencev/image_widget_crop/issues/events{/number}","events_url":"https://api.github.com/repos/Dencev/image_widget_crop/events","assignees_url":"https://api.github.com/repos/Dencev/image_widget_crop/assignees{/user}","branches_url":"https://api.github.com/repos/Dencev/image_widget_crop/branches{/branch}","tags_url":"https://api.github.com/repos/Dencev/image_widget_crop/tags","blobs_url":"https://api.github.com/repos/Dencev/image_widget_crop/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Dencev/image_widget_crop/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Dencev/image_widget_crop/git/refs{/sha}","trees_url":"https://api.github.com/repos/Dencev/image_widget_crop/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Dencev/image_widget_crop/statuses/{sha}","languages_url":"https://api.github.com/repos/Dencev/image_widget_crop/languages","stargazers_url":"https://api.github.com/repos/Dencev/image_widget_crop/stargazers","contributors_url":"https://api.github.com/repos/Dencev/image_widget_crop/contributors","subscribers_url":"https://api.github.com/repos/Dencev/image_widget_crop/subscribers","subscription_url":"https://api.github.com/repos/Dencev/image_widget_crop/subscription","commits_url":"https://api.github.com/repos/Dencev/image_widget_crop/commits{/sha}","git_commits_url":"https://api.github.com/repos/Dencev/image_widget_crop/git/commits{/sha}","comments_url":"https://api.github.com/repos/Dencev/image_widget_crop/comments{/number}","issue_comment_url":"https://api.github.com/repos/Dencev/image_widget_crop/issues/comments{/number}","contents_url":"https://api.github.com/repos/Dencev/image_widget_crop/contents/{+path}","compare_url":"https://api.github.com/repos/Dencev/image_widget_crop/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Dencev/image_widget_crop/merges","archive_url":"https://api.github.com/repos/Dencev/image_widget_crop/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Dencev/image_widget_crop/downloads","issues_url":"https://api.github.com/repos/Dencev/image_widget_crop/issues{/number}","pulls_url":"https://api.github.com/repos/Dencev/image_widget_crop/pulls{/number}","milestones_url":"https://api.github.com/repos/Dencev/image_widget_crop/milestones{/number}","notifications_url":"https://api.github.com/repos/Dencev/image_widget_crop/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Dencev/image_widget_crop/labels{/name}","releases_url":"https://api.github.com/repos/Dencev/image_widget_crop/releases{/id}","created_at":"2016-01-13T11:12:08Z","updated_at":"2016-01-13T11:12:09Z","pushed_at":"2016-01-14T10:46:37Z","git_url":"git://github.com/Dencev/image_widget_crop.git","ssh_url":"git@github.com:Dencev/image_widget_crop.git","clone_url":"https://github.com/Dencev/image_widget_crop.git","svn_url":"https://github.com/Dencev/image_widget_crop","homepage":null,"size":150,"stargazers_count":0,"watchers_count":0,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"8.x-1.x"}},"base":{"label":"drupal-media:8.x-1.x","ref":"8.x-1.x","sha":"6d802e9019650b4cd51725b033476d1acb3af37f","user":{"login":"drupal-media","id":7276699,"avatar_url":"https://avatars.githubusercontent.com/u/7276699?v=3","gravatar_id":"","url":"https://api.github.com/users/drupal-media","html_url":"https://github.com/drupal-media","followers_url":"https://api.github.com/users/drupal-media/followers","following_url":"https://api.github.com/users/drupal-media/following{/other_user}","gists_url":"https://api.github.com/users/drupal-media/gists{/gist_id}","starred_url":"https://api.github.com/users/drupal-media/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drupal-media/subscriptions","organizations_url":"https://api.github.com/users/drupal-media/orgs","repos_url":"https://api.github.com/users/drupal-media/repos","events_url":"https://api.github.com/users/drupal-media/events{/privacy}","received_events_url":"https://api.github.com/users/drupal-media/received_events","type":"Organization","site_admin":false},"repo":{"id":48803901,"name":"image_widget_crop","full_name":"drupal-media/image_widget_crop","owner":{"login":"drupal-media","id":7276699,"avatar_url":"https://avatars.githubusercontent.com/u/7276699?v=3","gravatar_id":"","url":"https://api.github.com/users/drupal-media","html_url":"https://github.com/drupal-media","followers_url":"https://api.github.com/users/drupal-media/followers","following_url":"https://api.github.com/users/drupal-media/following{/other_user}","gists_url":"https://api.github.com/users/drupal-media/gists{/gist_id}","starred_url":"https://api.github.com/users/drupal-media/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drupal-media/subscriptions","organizations_url":"https://api.github.com/users/drupal-media/orgs","repos_url":"https://api.github.com/users/drupal-media/repos","events_url":"https://api.github.com/users/drupal-media/events{/privacy}","received_events_url":"https://api.github.com/users/drupal-media/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/drupal-media/image_widget_crop","description":"Image widget crop module - https://www.drupal.org/sandbox/woprrr/2571403","fork":false,"url":"https://api.github.com/repos/drupal-media/image_widget_crop","forks_url":"https://api.github.com/repos/drupal-media/image_widget_crop/forks","keys_url":"https://api.github.com/repos/drupal-media/image_widget_crop/keys{/key_id}","collaborators_url":"https://api.github.com/repos/drupal-media/image_widget_crop/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/drupal-media/image_widget_crop/teams","hooks_url":"https://api.github.com/repos/drupal-media/image_widget_crop/hooks","issue_events_url":"https://api.github.com/repos/drupal-media/image_widget_crop/issues/events{/number}","events_url":"https://api.github.com/repos/drupal-media/image_widget_crop/events","assignees_url":"https://api.github.com/repos/drupal-media/image_widget_crop/assignees{/user}","branches_url":"https://api.github.com/repos/drupal-media/image_widget_crop/branches{/branch}","tags_url":"https://api.github.com/repos/drupal-media/image_widget_crop/tags","blobs_url":"https://api.github.com/repos/drupal-media/image_widget_crop/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/drupal-media/image_widget_crop/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/drupal-media/image_widget_crop/git/refs{/sha}","trees_url":"https://api.github.com/repos/drupal-media/image_widget_crop/git/trees{/sha}","statuses_url":"https://api.github.com/repos/drupal-media/image_widget_crop/statuses/{sha}","languages_url":"https://api.github.com/repos/drupal-media/image_widget_crop/languages","stargazers_url":"https://api.github.com/repos/drupal-media/image_widget_crop/stargazers","contributors_url":"https://api.github.com/repos/drupal-media/image_widget_crop/contributors","subscribers_url":"https://api.github.com/repos/drupal-media/image_widget_crop/subscribers","subscription_url":"https://api.github.com/repos/drupal-media/image_widget_crop/subscription","commits_url":"https://api.github.com/repos/drupal-media/image_widget_crop/commits{/sha}","git_commits_url":"https://api.github.com/repos/drupal-media/image_widget_crop/git/commits{/sha}","comments_url":"https://api.github.com/repos/drupal-media/image_widget_crop/comments{/number}","issue_comment_url":"https://api.github.com/repos/drupal-media/image_widget_crop/issues/comments{/number}","contents_url":"https://api.github.com/repos/drupal-media/image_widget_crop/contents/{+path}","compare_url":"https://api.github.com/repos/drupal-media/image_widget_crop/compare/{base}...{head}","merges_url":"https://api.github.com/repos/drupal-media/image_widget_crop/merges","archive_url":"https://api.github.com/repos/drupal-media/image_widget_crop/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/drupal-media/image_widget_crop/downloads","issues_url":"https://api.github.com/repos/drupal-media/image_widget_crop/issues{/number}","pulls_url":"https://api.github.com/repos/drupal-media/image_widget_crop/pulls{/number}","milestones_url":"https://api.github.com/repos/drupal-media/image_widget_crop/milestones{/number}","notifications_url":"https://api.github.com/repos/drupal-media/image_widget_crop/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/drupal-media/image_widget_crop/labels{/name}","releases_url":"https://api.github.com/repos/drupal-media/image_widget_crop/releases{/id}","created_at":"2015-12-30T14:21:12Z","updated_at":"2016-01-13T15:56:33Z","pushed_at":"2016-01-14T10:46:38Z","git_url":"git://github.com/drupal-media/image_widget_crop.git","ssh_url":"git@github.com:drupal-media/image_widget_crop.git","clone_url":"https://github.com/drupal-media/image_widget_crop.git","svn_url":"https://github.com/drupal-media/image_widget_crop","homepage":null,"size":146,"stargazers_count":0,"watchers_count":0,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":0,"default_branch":"8.x-1.x"}},"_links":{"self":{"href":"https://api.github.com/repos/drupal-media/image_widget_crop/pulls/1"},"html":{"href":"https://github.com/drupal-media/image_widget_crop/pull/1"},"issue":{"href":"https://api.github.com/repos/drupal-media/image_widget_crop/issues/1"},"comments":{"href":"https://api.github.com/repos/drupal-media/image_widget_crop/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/drupal-media/image_widget_crop/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/drupal-media/image_widget_crop/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/drupal-media/image_widget_crop/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/drupal-media/image_widget_crop/statuses/ab31fba0a75efa79a77004b40a68549d4bbe6d19"}}}}