function focal_point_entity_update in Focal Point 8
Implements hook_entity_update().
Saves the focal point value for the image file entity about to be saved.
1 call to focal_point_entity_update()
- focal_point_entity_insert in ./
focal_point.module - Implements hook_entity_insert().
File
- ./
focal_point.module, line 61 - Allow users to specify a focal point on content images.
Code
function focal_point_entity_update(EntityInterface $entity) {
// Only worry about entities that are fieldable.
if ($entity instanceof FieldableEntityInterface) {
// Loop all the fields and save focal point values for images.
foreach ($entity
->getFieldDefinitions() as $key => $field) {
if ($field
->getType() == 'image' && $entity
->hasField($field
->getName())) {
$crop_type = \Drupal::config('focal_point.settings')
->get('crop_type');
// Loop through all values for this field. Its cardinality might be > 1.
foreach ($entity->{$field
->getName()} as $item) {
/** @var \Drupal\focal_point\FocalPointManagerInterface $focal_point_manager */
$focal_point_manager = \Drupal::service('focal_point.manager');
if (!$item->entity instanceof FileInterface) {
continue;
}
$crop = $focal_point_manager
->getCropEntity($item->entity, $crop_type);
$focal_point = NULL;
// Use the default focal point on new crop entities.
if ($crop
->isNew()) {
$focal_point = \Drupal::config('focal_point.settings')
->get('default_value');
}
// Use the focal point set over the UI.
if (!empty($item->focal_point)) {
$focal_point = $item->focal_point;
// Keep the original focal_point value to be able to use it during hooks.
if (!$crop
->get('x')
->isEmpty() && !$crop
->get('y')
->isEmpty()) {
$x = $crop
->get('x')->value;
$y = $crop
->get('y')->value;
$focal_point_original = $focal_point_manager
->absoluteToRelative($x, $y, $item->width, $item->height);
$entity->{$field
->getName()}->focal_point_original = join(',', $focal_point_original);
}
}
if ($focal_point) {
list($x, $y) = explode(',', $focal_point);
$focal_point_manager
->saveCropEntity($x, $y, $item->width, $item->height, $crop);
}
}
}
}
}
}