diff --git a/focal_point.info.yml b/focal_point.info.yml index 59a2d6f..24a2a47 100644 --- a/focal_point.info.yml +++ b/focal_point.info.yml @@ -1,8 +1,14 @@ name: Focal Point type: module description: Allows users to specify the focal point of an image for use during cropping. -core: 8.x +# core: 8.x package: Images -version: VERSION +# version: VERSION dependencies: - image + +# Information added by Drupal.org packaging script on 2015-05-13 +version: '8.x-1.x-dev' +core: '8.x' +project: 'focal_point' +datestamp: 1431553395 diff --git a/src/Plugin/Field/FieldWidget/FocalPointImageWidget.php b/src/Plugin/Field/FieldWidget/FocalPointImageWidget.php index 54b8a8c..94aca09 100644 --- a/src/Plugin/Field/FieldWidget/FocalPointImageWidget.php +++ b/src/Plugin/Field/FieldWidget/FocalPointImageWidget.php @@ -67,7 +67,10 @@ class FocalPointImageWidget extends ImageWidget { '#title' => 'Focal point', '#description' => t('Specify the focus of this image in the form "leftoffset,topoffset" where offsets are in percents. Ex: 25,75'), '#default_value' => isset($item['focal_point']) ? $item['focal_point'] : FocalPoint::DEFAULT_VALUE, - '#element_validate' => array('\Drupal\focal_point\Plugin\Field\FieldWidget\FocalPointImageWidget::validateFocalPoint'), + //'#element_validate' => array('\Drupal\focal_point\Plugin\Field\FieldWidget\FocalPointImageWidget::validateFocalPoint'), + '#element_validate' => array( + array(get_called_class(), 'validateFocalPoint'), + ), '#attributes' => array( 'class' => array('focal-point', 'focal-point-' . $element['#field_name'] . '-' . $element['#delta']), 'data-delta' => $element['#delta'], @@ -113,13 +116,36 @@ class FocalPointImageWidget extends ImageWidget { * {@inheritDocs} * * Validate callback for the focal point field. + * @todo : Here the focal points are saved when theres no error. Should be + * happening at another point after saving the file? */ public static function validateFocalPoint($element, FormStateInterface $form_state) { $field_name = array_pop($element['#parents']); - $focal_point_value = $form_state->getValue($field_name); + $focal_point_value = $element['#value']; + + // This is the value: + // $element['#value'] + + // This is the parent: + // $element['#array_parents'] // + // Field: $element['#array_parents'][0] + // index: $element['#array_parents'][2] + + // Get the Values. + $state = $form_state->getValues(); + + // Check if the fid exists. + if(!empty($state[$element['#array_parents'][0]][$element['#array_parents'][2]]['fids'][0])) { + $fid = $state[$element['#array_parents'][0]][$element['#array_parents'][2]]['fids'][0]; + } if (!is_null($focal_point_value) && !FocalPoint::validate($focal_point_value)) { - \Drupal::formBuilder()->setError($element, $form_state, t('The !title field should be in the form "leftoffset,topoffset" where offsets are in percents. Ex: 25,75.', array('!title' => $element['#title']))); + $form_state->setError($element, $form_state, t('The !title field should be in the form "leftoffset,topoffset" where offsets are in percents. Ex: 25,75.', array('!title' => $element['#title']))); + } elseif (isset($fid)) { + // Save it. + $myfocalpoint = new FocalPoint(\Drupal\file\Entity\File::load($fid)); + $myfocalpoint->setFocalPoint($element['#value']); + $myfocalpoint->save(); } }