You are here

class FocalPointManager in Focal Point 8

Provides business logic related to focal point.

Hierarchy

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'
focal_point.services.yml in ./focal_point.services.yml
focal_point.services.yml
1 service uses FocalPointManager
focal_point.manager in ./focal_point.services.yml
Drupal\focal_point\FocalPointManager

File

src/FocalPointManager.php, line 13

Namespace

Drupal\focal_point
View 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

Namesort descending Modifiers Type Description Overrides
FocalPointManager::$cropStorage protected property Crop entity storage.
FocalPointManager::absoluteToRelative public function Converts absolute focal point coordinates to relative coordinates. Overrides FocalPointManagerInterface::absoluteToRelative
FocalPointManager::FOCAL_POINT_VALIDATION_REGEXP constant Regular expression for focal point form value validation.
FocalPointManager::getCropEntity public function Gets a crop entity for the given file. Overrides FocalPointManagerInterface::getCropEntity
FocalPointManager::relativeToAbsolute public function Converts relative focal point coordinates to absolute coordinates. Overrides FocalPointManagerInterface::relativeToAbsolute
FocalPointManager::saveCropEntity public function Creates (or updates) a crop entity using relative focal point coordinates. Overrides FocalPointManagerInterface::saveCropEntity
FocalPointManager::validateFocalPoint public function Validates focal point string representation. Overrides FocalPointManagerInterface::validateFocalPoint
FocalPointManager::__construct public function Constructs FocalPointManager.