class FocalPointManager in Focal Point 8
Provides business logic related to focal point.
Hierarchy
- class \Drupal\focal_point\FocalPointManager implements FocalPointManagerInterface
Expanded class hierarchy of FocalPointManager
3 files declare their use of FocalPointManager
- FocalPointFieldWidgetTest.php in tests/
src/ Unit/ FieldWidgets/ FocalPointFieldWidgetTest.php - FocalPointManagerTest.php in tests/
src/ Unit/ FocalPointManagerTest.php - FocalPointUnitTestCase.php in tests/
src/ Unit/ FocalPointUnitTestCase.php
1 string reference to 'FocalPointManager'
1 service uses FocalPointManager
File
- src/
FocalPointManager.php, line 13
Namespace
Drupal\focal_pointView source
class FocalPointManager implements FocalPointManagerInterface {
/**
* Regular expression for focal point form value validation.
*
* @var string
*/
const FOCAL_POINT_VALIDATION_REGEXP = '/^(100|[0-9]{1,2})(,)(100|[0-9]{1,2})$/';
/**
* Crop entity storage.
*
* @var \Drupal\crop\CropStorageInterface
*/
protected $cropStorage;
/**
* Constructs FocalPointManager.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->cropStorage = $entity_type_manager
->getStorage('crop');
}
/**
* {@inheritdoc}
*/
public function validateFocalPoint($focal_point) {
return (bool) preg_match(static::FOCAL_POINT_VALIDATION_REGEXP, $focal_point);
}
/**
* {@inheritdoc}
*/
public function relativeToAbsolute($x, $y, $width, $height) {
// Focal point JS provides relative location while crop entity
// expects exact coordinate on the original image. Let's convert.
return [
'x' => (int) round($x / 100.0 * $width),
'y' => (int) round($y / 100.0 * $height),
];
}
/**
* {@inheritdoc}
*/
public function absoluteToRelative($x, $y, $width, $height) {
return [
'x' => $width ? (int) round($x / $width * 100) : 0,
'y' => $height ? (int) round($y / $height * 100) : 0,
];
}
/**
* {@inheritdoc}
*/
public function getCropEntity(FileInterface $file, $crop_type) {
if (Crop::cropExists($file
->getFileUri(), $crop_type)) {
/** @var \Drupal\crop\CropInterface $crop */
$crop = Crop::findCrop($file
->getFileUri(), $crop_type);
}
else {
$values = [
'type' => $crop_type,
'entity_id' => $file
->id(),
'entity_type' => 'file',
'uri' => $file
->getFileUri(),
];
$crop = $this->cropStorage
->create($values);
}
return $crop;
}
/**
* {@inheritdoc}
*/
public function saveCropEntity($x, $y, $width, $height, CropInterface $crop) {
$absolute = $this
->relativeToAbsolute($x, $y, $width, $height);
$anchor = $crop
->anchor();
if ($anchor['x'] != $absolute['x'] || $anchor['y'] != $absolute['y']) {
$crop
->setPosition($absolute['x'], $absolute['y']);
$crop
->save();
}
return $crop;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FocalPointManager:: |
protected | property | Crop entity storage. | |
FocalPointManager:: |
public | function |
Converts absolute focal point coordinates to relative coordinates. Overrides FocalPointManagerInterface:: |
|
FocalPointManager:: |
constant | Regular expression for focal point form value validation. | ||
FocalPointManager:: |
public | function |
Gets a crop entity for the given file. Overrides FocalPointManagerInterface:: |
|
FocalPointManager:: |
public | function |
Converts relative focal point coordinates to absolute coordinates. Overrides FocalPointManagerInterface:: |
|
FocalPointManager:: |
public | function |
Creates (or updates) a crop entity using relative focal point coordinates. Overrides FocalPointManagerInterface:: |
|
FocalPointManager:: |
public | function |
Validates focal point string representation. Overrides FocalPointManagerInterface:: |
|
FocalPointManager:: |
public | function | Constructs FocalPointManager. |