diff --git a/focal_point.module b/focal_point.module index b95023d..787f78e 100644 --- a/focal_point.module +++ b/focal_point.module @@ -11,8 +11,6 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\StringTranslation\TranslatableMarkup; -use Drupal\Core\Url; use Drupal\focal_point\Plugin\Field\FieldWidget\FocalPointImageWidget; /** @@ -71,7 +69,7 @@ function focal_point_field_widget_form_alter(&$element, FormStateInterface $form * Implements hook_form_file_image_edit_form_alter(). * File entity integration. */ -function focal_point_form_file_image_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) { +function focal_point_form_file_image_edit_form_alter(&$form, FormStateInterface $form_state) { $file_entity_edit_form = $form_state->getBuildInfo()['callback_object']; $file = $file_entity_edit_form->getEntity(); @@ -94,7 +92,7 @@ function focal_point_form_file_image_edit_form_alter(&$form, \Drupal\Core\Form\F '#width' => $preview_image_render['width'], '#height' => $preview_image_render['height'], '#style_name' => $preview_image_render['style_name'], - '#uri' => $preview_image_render['uri'] + '#uri' => $preview_image_render['uri'], ]; $form = FocalPointImageWidget::attachFocalPoint($form, $file->id(), \Drupal::service('focal_point.manager')->getFocalPointFromFile($file)); @@ -104,7 +102,7 @@ function focal_point_form_file_image_edit_form_alter(&$form, \Drupal\Core\Form\F /** * Custom submit handler for the file entity image edit form. */ -function focal_point_form_file_image_edit_form_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) { +function focal_point_form_file_image_edit_form_submit(&$form, FormStateInterface $form_state) { $file_entity_edit_form = $form_state->getBuildInfo()['callback_object']; $entity = $file_entity_edit_form->getEntity(); $focal_point_new_value = $form_state->getValue('focal_point'); @@ -120,10 +118,16 @@ function focal_point_form_file_image_edit_form_submit(&$form, \Drupal\Core\Form\ } } -function focal_point_form_file_image_inline_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) { +/** + * Implements hook_form_file_image_inline_edit_form_alter(). + */ +function focal_point_form_file_image_inline_edit_form_alter(&$form, FormStateInterface $form_state) { focal_point_form_file_image_edit_form_alter($form, $form_state); } -function focal_point_form_file_image_inline_edit_form_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) { +/** + * Implements hook_form_file_image_inline_edit_form_submit(). + */ +function focal_point_form_file_image_inline_edit_form_submit(&$form, FormStateInterface $form_state) { focal_point_form_file_image_edit_form_submit($form, $form_state); } diff --git a/src/FocalPointManager.php b/src/FocalPointManager.php index 504da9e..976e898 100644 --- a/src/FocalPointManager.php +++ b/src/FocalPointManager.php @@ -107,19 +107,19 @@ class FocalPointManager implements FocalPointManagerInterface { /** * {@inheritdoc} */ - public function getFocalPointFromFile(FileInterface $file, $width = NULL, $height = NULL) { + public function getFocalPointFromFile(FileInterface $file) { $crop_type = \Drupal::config('focal_point.settings')->get('crop_type'); $crop = Crop::findCrop($file->getFileUri(), $crop_type); + if ($crop) { - if ($width == NULL || $height == NULL) { - $image = \Drupal::service('image.factory')->get($file->getFileUri()); - if ($image->isValid()) { - $width = $image->getWidth(); - $height = $image->getHeight(); - } + $image = \Drupal::service('image.factory')->get($file->getFileUri()); + if ($image->isValid()) { + // Because we are loading this image directly from the database, as + // opposed to passing it in as an argument during the image effect + // workflow, we can rely on the width and heights being accurate. + $anchor = $this->absoluteToRelative($crop->x->value, $crop->y->value, $image->getWidth(), $image->getHeight()); + return "{$anchor['x']},{$anchor['y']}"; } - $anchor = $this->absoluteToRelative($crop->x->value, $crop->y->value, $width, $height); - return "{$anchor['x']},{$anchor['y']}"; } return \Drupal::config('focal_point.settings')->get('default_value'); } diff --git a/src/FocalPointManagerInterface.php b/src/FocalPointManagerInterface.php index c1e533c..e7aa534 100644 --- a/src/FocalPointManagerInterface.php +++ b/src/FocalPointManagerInterface.php @@ -96,19 +96,16 @@ interface FocalPointManagerInterface { public function saveCropEntity($x, $y, $width, $height, CropInterface $crop); /** - * Gets the focal point for a given file, returns a default value if the file - * has no focal point. + * Gets the focal point for a given file. + * + * Returns a default value if the file has no focal point. * * @param \Drupal\file\FileInterface $file * The file. - * @param int $width - * Width of the original image. - * @param int $height - * Height of the original image. * * @return string * The focal point in the format x,y. */ - public function getFocalPointFromFile(FileInterface $file, $width = NULL, $height = NULL); + public function getFocalPointFromFile(FileInterface $file); } diff --git a/src/Plugin/Field/FieldWidget/FocalPointImageWidget.php b/src/Plugin/Field/FieldWidget/FocalPointImageWidget.php index 729a5bf..85a008b 100644 --- a/src/Plugin/Field/FieldWidget/FocalPointImageWidget.php +++ b/src/Plugin/Field/FieldWidget/FocalPointImageWidget.php @@ -46,63 +46,35 @@ class FocalPointImageWidget extends ImageWidget { } /** - * @param $element - * @return mixed + * Add the necessary form elements to support focal point. + * + * @param array $element + * The image form element to which the focal point is being attached. + * @param int $fid + * The file id for the image in question. If no image has been set, this + * will empty. + * @param string $focal_point + * The current focal point value. + * + * @return array + * The altered form element. */ public static function attachFocalPoint($element, $fid, $focal_point) { + $element_selector = 'focal-point'; if (isset($element['#parents'])) { - $element_selector = 'focal-point-' . implode('-', $element['#parents']); - } - else { - $element_selector = 'focal-point'; + $element_selector .= '-' . implode('-', $element['#parents']); } // Add the focal point indicator to preview. if (isset($element['preview'])) { - $indicator = array( - '#theme_wrappers' => array('container'), - '#attributes' => array( - 'class' => array('focal-point-indicator'), - 'data-selector' => $element_selector, - 'data-delta' => $element['#delta'], - ), - '#markup' => '', - ); - $preview = array( - 'indicator' => $indicator, + 'indicator' => static::buildIndicatorFormElement($element, $element_selector), 'thumbnail' => $element['preview'], ); $display_preview_link = \Drupal::config('focal_point.preview')->get('display_link'); - if ($display_preview_link && !empty($fid)) { - // Replace comma (,) with an x to make javascript handling easier. - $preview_focal_point_value = str_replace(',', 'x', $focal_point); - - // Create a token to be used during an access check on the preview page. - $token = self::getPreviewToken(); - - $preview_link = [ - '#type' => 'link', - '#title' => t('Preview'), - '#url' => new Url('focal_point.preview', - [ - 'fid' => $fid, - 'focal_point_value' => $preview_focal_point_value, - ], - [ - 'query' => array('focal_point_token' => $token), - ]), - '#attributes' => [ - 'class' => array('focal-point-preview-link'), - 'data-selector' => $element_selector, - 'data-field-name' => $element['#field_name'], - 'target' => '_blank', - ], - ]; - - $preview['preview_link'] = $preview_link; + $preview['preview_link'] = static::buildPreviewLinkFormElement($element, $element_selector, $focal_point, $fid); } // Use the existing preview weight value so that the focal point indicator @@ -114,22 +86,7 @@ class FocalPointImageWidget extends ImageWidget { } // Add the focal point field. - $element_selector = 'focal-point-' . implode('-', $element['#parents']); - $element['focal_point'] = array( - '#type' => 'textfield', - '#title' => 'Focal point', - '#description' => new TranslatableMarkup('Specify the focus of this image in the form "leftoffset,topoffset" where offsets are in percents. Ex: 25,75'), - '#default_value' => $focal_point, - '#element_validate' => array('\Drupal\focal_point\Plugin\Field\FieldWidget\FocalPointImageWidget::validateFocalPoint'), - '#attributes' => array( - 'class' => array('focal-point', $element_selector), - 'data-selector' => $element_selector, - 'data-field-name' => $element['#field_name'], - ), - '#attached' => array( - 'library' => array('focal_point/drupal.focal_point'), - ), - ); + $element['focal_point'] = buildFocalPointFormElement($element, $element_selector, $focal_point); return $element; } @@ -151,8 +108,10 @@ class FocalPointImageWidget extends ImageWidget { $file = \Drupal::service('entity_type.manager') ->getStorage('file') ->load($return['target_id']); + + // @todo: consider moving this logic to getFocalPointFromFile if ($file) { - $return['focal_point'] = \Drupal::service('focal_point.manager')->getFocalPointFromFile($file, $return['width'], $return['height']); + $return['focal_point'] = \Drupal::service('focal_point.manager')->getFocalPointFromFile($file); } else { \Drupal::logger('focal_point')->notice("Attempted to get a focal point value for an invalid or temporary file."); @@ -201,4 +160,106 @@ class FocalPointImageWidget extends ImageWidget { return \Drupal::csrfToken()->validate($token, self::PREVIEW_TOKEN_NAME); } + /** + * Builds the form element for the focal point indicator. + * + * @param array $element + * The image form element to which the focal point is being attached. + * @param string $element_selector + * The data selector for the focal point form elements. + * + * @return array + * The indicator form element. + */ + protected static function buildIndicatorFormElement($element, $element_selector) { + $indicator = array( + '#theme_wrappers' => array('container'), + '#attributes' => array( + 'class' => array('focal-point-indicator'), + 'data-selector' => $element_selector, + 'data-delta' => $element['#delta'], + ), + '#markup' => '', + ); + + return $indicator; + } + + /** + * Builds the form element for the focal point preview. + * + * @param array $element + * The image form element to which the focal point is being attached. + * @param string $element_selector + * The data selector for the focal point form elements. + * @param string $focal_point + * The current focal point value. + * @param int $fid + * The file id for the image in question. + * + * @return array + * The indicator form element. + */ + protected static function buildPreviewLinkFormElement($element, $element_selector, $focal_point, $fid) { + // Replace comma (,) with an x to make javascript handling easier. + $preview_focal_point_value = str_replace(',', 'x', $focal_point); + + // Create a token to be used during an access check on the preview page. + $token = self::getPreviewToken(); + + $preview_link = [ + '#type' => 'link', + '#title' => t('Preview'), + '#url' => new Url('focal_point.preview', + [ + 'fid' => $fid, + 'focal_point_value' => $preview_focal_point_value, + ], + [ + 'query' => array('focal_point_token' => $token), + ]), + '#attributes' => [ + 'class' => array('focal-point-preview-link'), + 'data-selector' => $element_selector, + 'data-field-name' => $element['#field_name'], + 'target' => '_blank', + ], + ]; + + return $preview_link; + } + + /** + * Builds the form element for the focal point field. + * + * @param array $element + * The image form element to which the focal point is being attached. + * @param string $element_selector + * The data selector for the focal point form elements. + * @param string $focal_point + * The current focal point value. + * + * @return array + * The indicator form element. + */ + protected static function buildFocalPointFormElement($element, $element_selector, $focal_point) { + $focal_point_element = array( + '#type' => 'textfield', + '#title' => 'Focal point', + '#description' => new TranslatableMarkup('Specify the focus of this image in the form "leftoffset,topoffset" where offsets are in percents. Ex: 25,75'), + '#default_value' => $focal_point, + '#element_validate' => array('\Drupal\focal_point\Plugin\Field\FieldWidget\FocalPointImageWidget::validateFocalPoint'), + '#attributes' => array( + 'class' => array('focal-point', $element_selector), + 'data-selector' => $element_selector, + 'data-field-name' => $element['#field_name'], + ), + '#attached' => array( + 'library' => array('focal_point/drupal.focal_point'), + ), + ); + + return $focal_point_element; + } + }